Skip to content

Commit 3e664f5

Browse files
Merge pull request #2408 from NullVoxPopuli/nvp/template-lint-extract-rule-template-no-autofocus-attribute
Extract rule: template-no-autofocus-attribute
2 parents 2a8b954 + 2da18d5 commit 3e664f5

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ rules in templates can be disabled with eslint directives with mustache or html
185185
| [template-no-accesskey-attribute](docs/rules/template-no-accesskey-attribute.md) | disallow accesskey attribute | | 🔧 | |
186186
| [template-no-aria-hidden-body](docs/rules/template-no-aria-hidden-body.md) | disallow aria-hidden on body element | | 🔧 | |
187187
| [template-no-aria-unsupported-elements](docs/rules/template-no-aria-unsupported-elements.md) | disallow ARIA roles, states, and properties on elements that do not support them | | | |
188+
| [template-no-autofocus-attribute](docs/rules/template-no-autofocus-attribute.md) | disallow autofocus attribute | | 🔧 | |
188189

189190
### Best Practices
190191

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# ember/template-no-autofocus-attribute
2+
3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
5+
<!-- end auto-generated rule header -->
6+
7+
Disallows the use of `autofocus` attribute on elements.
8+
9+
The `autofocus` attribute can cause usability issues for both sighted and non-sighted users by disrupting expected behavior and screen reader announcements.
10+
11+
## Examples
12+
13+
Examples of **incorrect** code for this rule:
14+
15+
```gjs
16+
<template>
17+
<input type="text" autofocus />
18+
</template>
19+
```
20+
21+
```gjs
22+
<template>
23+
<textarea autofocus></textarea>
24+
</template>
25+
```
26+
27+
Examples of **correct** code for this rule:
28+
29+
```gjs
30+
<template>
31+
<input type="text" />
32+
</template>
33+
```
34+
35+
```gjs
36+
<template>
37+
<textarea></textarea>
38+
</template>
39+
```
40+
41+
## When Not To Use It
42+
43+
If you need to autofocus for specific accessibility or UX requirements and have thoroughly tested with assistive technologies, you may disable this rule for those specific cases.
44+
45+
## References
46+
47+
- [eslint-plugin-ember template-no-autofocus-attribute](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-autofocus-attribute.md)
48+
- [MDN autofocus attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autofocus)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'suggestion',
5+
docs: {
6+
description: 'disallow autofocus attribute',
7+
category: 'Accessibility',
8+
strictGjs: true,
9+
strictGts: true,
10+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-autofocus-attribute.md',
11+
},
12+
fixable: 'code',
13+
schema: [],
14+
messages: {
15+
noAutofocus:
16+
'Avoid using autofocus attribute. Autofocusing elements can cause usability issues for sighted and non-sighted users.',
17+
},
18+
},
19+
20+
create(context) {
21+
return {
22+
GlimmerElementNode(node) {
23+
const autofocusAttr = node.attributes?.find((attr) => attr.name === 'autofocus');
24+
25+
if (autofocusAttr) {
26+
context.report({
27+
node: autofocusAttr,
28+
messageId: 'noAutofocus',
29+
fix(fixer) {
30+
// Remove the attribute including preceding whitespace
31+
const sourceCode = context.sourceCode;
32+
const text = sourceCode.getText();
33+
const attrStart = autofocusAttr.range[0];
34+
const attrEnd = autofocusAttr.range[1];
35+
36+
let removeStart = attrStart;
37+
while (removeStart > 0 && /\s/.test(text[removeStart - 1])) {
38+
removeStart--;
39+
}
40+
41+
return fixer.removeRange([removeStart, attrEnd]);
42+
},
43+
});
44+
}
45+
},
46+
};
47+
},
48+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//------------------------------------------------------------------------------
2+
// Requirements
3+
//------------------------------------------------------------------------------
4+
5+
const rule = require('../../../lib/rules/template-no-autofocus-attribute');
6+
const RuleTester = require('eslint').RuleTester;
7+
8+
//------------------------------------------------------------------------------
9+
// Tests
10+
//------------------------------------------------------------------------------
11+
12+
const ruleTester = new RuleTester({
13+
parser: require.resolve('ember-eslint-parser'),
14+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
15+
});
16+
17+
ruleTester.run('template-no-autofocus-attribute', rule, {
18+
valid: [
19+
`<template>
20+
<input type="text" />
21+
</template>`,
22+
`<template>
23+
<button>Click me</button>
24+
</template>`,
25+
],
26+
27+
invalid: [
28+
{
29+
code: `<template>
30+
<input type="text" autofocus />
31+
</template>`,
32+
output: `<template>
33+
<input type="text"/>
34+
</template>`,
35+
errors: [
36+
{
37+
message:
38+
'Avoid using autofocus attribute. Autofocusing elements can cause usability issues for sighted and non-sighted users.',
39+
type: 'GlimmerAttrNode',
40+
},
41+
],
42+
},
43+
{
44+
code: `<template>
45+
<textarea autofocus></textarea>
46+
</template>`,
47+
output: `<template>
48+
<textarea></textarea>
49+
</template>`,
50+
errors: [
51+
{
52+
type: 'GlimmerAttrNode',
53+
},
54+
],
55+
},
56+
],
57+
});

0 commit comments

Comments
 (0)