forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-no-invalid-link-text.js
More file actions
285 lines (279 loc) · 9.37 KB
/
template-no-invalid-link-text.js
File metadata and controls
285 lines (279 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const rule = require('../../../lib/rules/template-no-invalid-link-text');
const RuleTester = require('eslint').RuleTester;
const ruleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});
ruleTester.run('template-no-invalid-link-text', rule, {
valid: [
// Link with component child — content is opaque, can't validate.
// Mirrors upstream no-invalid-link-text.js L53-56 ("do not flag when link contains additional dynamic (non-text) children").
{ filename: 'test.gjs', code: '<template><a href="/x"><MyComponent /></a></template>' },
{ filename: 'test.gjs', code: '<template><a href="/x">prefix <MyComponent /></a></template>' },
{ filename: 'test.gjs', code: '<template><a href="/about">About Us</a></template>' },
{
filename: 'test.gjs',
code: '<template><a href="https://myurl.com">Click here to read more about this amazing adventure</a></template>',
},
{
filename: 'test.gjs',
code: '<template><a href="https://myurl.com" aria-labelledby="some-id"></a></template>',
},
{
filename: 'test.gjs',
code: '<template><a href="https://myurl.com" aria-label="click here to read about our company"></a></template>',
},
{
filename: 'test.gjs',
code: '<template><a href="https://myurl.com" aria-hidden="true"></a></template>',
},
{ filename: 'test.gjs', code: '<template><a href="https://myurl.com" hidden></a></template>' },
{
filename: 'test.gjs',
code: '<template><a href="#" aria-label={{this.anAriaLabel}}>A link with a variable as aria-label</a></template>',
},
// In GJS, LinkTo without an import from @ember/routing is not Ember's router link
{ filename: 'test.gjs', code: '<template><LinkTo>click here</LinkTo></template>' },
{ filename: 'test.gjs', code: '<template><LinkTo></LinkTo></template>' },
// Imported LinkTo with valid text
{
filename: 'test.gjs',
code: "import { LinkTo } from '@ember/routing'; <template><LinkTo>About Us</LinkTo></template>",
},
// allowEmptyLinks: true — empty <a> is valid
{
filename: 'test.gjs',
code: '<template><a href="https://myurl.com"></a></template>',
options: [{ allowEmptyLinks: true }],
},
// Dynamic content — can't validate
{
filename: 'test.gjs',
code: "import { LinkTo } from '@ember/routing'; <template><LinkTo>{{foo}} more</LinkTo></template>",
},
{
filename: 'test.gjs',
code: "import { LinkTo } from '@ember/routing'; <template><LinkTo aria-label={{t 'some-translation'}}>A link with translation</LinkTo></template>",
},
{
filename: 'test.gjs',
code: '<template><a href="/play">{{if game.inProgress "Continue" "Start"}}</a></template>',
},
{
filename: 'test.gjs',
code: `
import { LinkTo } from '@ember/routing';
<template>
<LinkTo>
{{#if game.inProgress}}
Continue
{{else}}
Start
{{/if}}
</LinkTo>
</template>
`,
},
{
filename: 'test.gjs',
code: '<template><a href="/play">{{#if game.inProgress}}Continue{{else}}Start{{/if}}</a></template>',
},
],
invalid: [
{
filename: 'test.gjs',
code: '<template><a href="/page">Click here</a></template>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
filename: 'test.gjs',
code: '<template><a href="/page">More info</a></template>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
filename: 'test.gjs',
code: '<template><a href="/page">Read more</a></template>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
// standalone "more" is disallowed
filename: 'test.gjs',
code: '<template><a href="/page">more</a></template>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
filename: 'test.gjs',
code: '<template><a href="https://myurl.com"></a></template>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
filename: 'test.gjs',
code: '<template><a href="https://myurl.com"> </a></template>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
filename: 'test.gjs',
code: '<template><a aria-labelledby="" href="https://myurl.com">Click here</a></template>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
filename: 'test.gjs',
code: '<template><a aria-label="Click here" href="https://myurl.com">Click here</a></template>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
// Imported LinkTo with disallowed text
filename: 'test.gjs',
code: "import { LinkTo } from '@ember/routing'; <template><LinkTo>click here</LinkTo></template>",
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
// Aliased LinkTo import must still be flagged
filename: 'test.gjs',
code: "import { LinkTo as RouterLink } from '@ember/routing'; <template><RouterLink>click here</RouterLink></template>",
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
// Imported LinkTo — empty
filename: 'test.gjs',
code: "import { LinkTo } from '@ember/routing'; <template><LinkTo></LinkTo></template>",
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
// aria-label with disallowed text overrides content check
filename: 'test.gjs',
code: 'import { LinkTo } from \'@ember/routing\'; <template><LinkTo aria-label="click here">About Us</LinkTo></template>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
filename: 'test.gjs',
code: '<template><MyLink>click here</MyLink></template>',
output: null,
options: [{ linkComponents: ['MyLink'] }],
errors: [{ messageId: 'invalidText' }],
},
],
});
const hbsRuleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser/hbs'),
parserOptions: {
ecmaVersion: 2022,
sourceType: 'module',
},
});
hbsRuleTester.run('template-no-invalid-link-text (hbs)', rule, {
valid: [
'<a href="https://myurl.com">Click here to read more about this amazing adventure</a>',
'{{#link-to}} click here to read more about our company{{/link-to}}',
'<LinkTo>Read more about ways semantic HTML can make your code more accessible.</LinkTo>',
'<LinkTo>{{foo}} more</LinkTo>',
'<a href="https://myurl.com" aria-labelledby="some-id"></a>',
'<a href="https://myurl.com" aria-label="click here to read about our company"></a>',
'<a href="https://myurl.com" aria-hidden="true"></a>',
'<a href="https://myurl.com" hidden></a>',
'<LinkTo aria-label={{t "some-translation"}}>A link with translation</LinkTo>',
'<a href="#" aria-label={{this.anAriaLabel}}>A link with a variable as aria-label</a>',
// allowEmptyLinks: true — empty links are valid
{ code: '<a href="https://myurl.com"></a>', options: [{ allowEmptyLinks: true }] },
],
invalid: [
{
code: '<a href="https://myurl.com">click here</a>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
// standalone "more" is disallowed
code: '<a href="/page">more</a>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: '<LinkTo>click here</LinkTo>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: '{{#link-to}}click here{{/link-to}}',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: '<a href="https://myurl.com"></a>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: '<a href="https://myurl.com"> </a>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: `<a href="https://myurl.com">
</a>`,
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: '<a aria-labelledby="" href="https://myurl.com">Click here</a>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: '<a aria-labelledby=" " href="https://myurl.com">Click here</a>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: '<a aria-label="Click here" href="https://myurl.com">Click here</a>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
// aria-label with disallowed value on LinkTo (text content is valid but aria-label is not)
code: '<LinkTo aria-label="click here">About Us</LinkTo>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: '<LinkTo></LinkTo>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: `<LinkTo>
</LinkTo>`,
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: '{{#link-to}}{{/link-to}}',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: `{{#link-to}}
{{/link-to}}`,
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: '<MyLink>click here</MyLink>',
output: null,
options: [{ linkComponents: ['MyLink'] }],
errors: [{ messageId: 'invalidText' }],
},
],
});