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-autofocus-attribute.js
More file actions
280 lines (273 loc) · 6.79 KB
/
template-no-autofocus-attribute.js
File metadata and controls
280 lines (273 loc) · 6.79 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
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require('../../../lib/rules/template-no-autofocus-attribute');
const RuleTester = require('eslint').RuleTester;
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});
ruleTester.run('template-no-autofocus-attribute', rule, {
valid: [
`<template>
<input type="text" />
</template>`,
`<template>
<button>Click me</button>
</template>`,
// Mustache boolean-literal forms render NO attribute when the literal
// is false — these are the statically-known opt-outs that align with
// HTML boolean-attribute semantics.
`<template>
<input autofocus={{false}} />
</template>`,
`<template>
{{input autofocus=false}}
</template>`,
// Dialog exception (MDN): autofocus on <dialog> is recommended.
`<template>
<dialog autofocus></dialog>
</template>`,
// Dialog descendants are also exempt (angular-eslint parity).
`<template>
<dialog>
<button autofocus>Close</button>
</dialog>
</template>`,
`<template>
<dialog>
<div>
<input autofocus />
</div>
</dialog>
</template>`,
// Dialog exception also applies to the classic mustache form
// (`{{input autofocus=true}}`) — whether direct child or nested.
`<template>
<dialog>
{{input autofocus=true}}
</dialog>
</template>`,
`<template>
<dialog>
<div>
{{input autofocus=true}}
</div>
</dialog>
</template>`,
// Custom helpers / components taking an `autofocus` prop are opaque —
// we can't know whether the prop forwards to a native <input autofocus>
// or is used for something else. Narrow to {{input}} / {{component
// "input"}} which deterministically render native inputs.
'<template>{{my-wrapper autofocus=true}}</template>',
'<template>{{some-component autofocus=true name="foo"}}</template>',
'<template>{{component "some-other-helper" autofocus=true}}</template>',
],
invalid: [
{
code: `<template>
<input type="text" autofocus />
</template>`,
output: `<template>
<input type="text"/>
</template>`,
errors: [
{
message:
'Avoid using autofocus attribute. Autofocusing elements can cause usability issues for sighted and non-sighted users.',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<textarea autofocus></textarea>
</template>`,
output: `<template>
<textarea></textarea>
</template>`,
errors: [
{
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
{{input type="text" autofocus=true}}
</template>`,
output: null,
errors: [
{
messageId: 'noAutofocus',
type: 'GlimmerHashPair',
},
],
},
{
code: `<template>
{{component "input" type="text" autofocus=true}}
</template>`,
output: null,
errors: [
{
messageId: 'noAutofocus',
type: 'GlimmerHashPair',
},
],
},
{
code: `<template>
<div autofocus>
</div>
</template>`,
output: `<template>
<div>
</div>
</template>`,
errors: [
{
messageId: 'noAutofocus',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<h1 autofocus>
</h1>
</template>`,
output: `<template>
<h1>
</h1>
</template>`,
errors: [
{
messageId: 'noAutofocus',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<input autofocus="autofocus" />
</template>`,
output: `<template>
<input />
</template>`,
errors: [
{
messageId: 'noAutofocus',
type: 'GlimmerAttrNode',
},
],
},
// Value-aware: truthy literals and any dynamic value still flag.
{
code: `<template>
<input autofocus="true" />
</template>`,
output: `<template>
<input />
</template>`,
errors: [
{
messageId: 'noAutofocus',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<input autofocus={{true}} />
</template>`,
output: `<template>
<input />
</template>`,
errors: [
{
messageId: 'noAutofocus',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<input autofocus={{"true"}} />
</template>`,
output: `<template>
<input />
</template>`,
errors: [
{
messageId: 'noAutofocus',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<input autofocus={{this.shouldFocus}} />
</template>`,
output: `<template>
<input />
</template>`,
errors: [
{
messageId: 'noAutofocus',
type: 'GlimmerAttrNode',
},
],
},
// Dialog exception only applies within <dialog>; siblings elsewhere still flag.
{
code: `<template>
<section>
<button autofocus>Focus</button>
</section>
</template>`,
output: `<template>
<section>
<button>Focus</button>
</section>
</template>`,
errors: [
{
messageId: 'noAutofocus',
type: 'GlimmerAttrNode',
},
],
},
// Per HTML boolean-attribute semantics, the string "false" / mustache
// string "false" / hash-pair string "false" are all TRUTHY. Only the
// mustache boolean-literal {{false}} renders no attribute.
{
code: `<template>
<input autofocus="false" />
</template>`,
output: `<template>
<input />
</template>`,
errors: [{ messageId: 'noAutofocus', type: 'GlimmerAttrNode' }],
},
{
code: `<template>
<input autofocus={{"false"}} />
</template>`,
output: `<template>
<input />
</template>`,
errors: [{ messageId: 'noAutofocus', type: 'GlimmerAttrNode' }],
},
{
code: `<template>
{{input autofocus="false"}}
</template>`,
output: null,
errors: [{ messageId: 'noAutofocus' }],
},
],
});