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
60 lines (55 loc) · 1.72 KB
/
template-no-autofocus-attribute.js
File metadata and controls
60 lines (55 loc) · 1.72 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
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow autofocus attribute',
category: 'Accessibility',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-autofocus-attribute.md',
},
fixable: 'code',
schema: [],
messages: {
noAutofocus:
'Avoid using autofocus attribute. Autofocusing elements can cause usability issues for sighted and non-sighted users.',
},
},
create(context) {
return {
GlimmerElementNode(node) {
const autofocusAttr = node.attributes?.find((attr) => attr.name === 'autofocus');
if (autofocusAttr) {
context.report({
node: autofocusAttr,
messageId: 'noAutofocus',
fix(fixer) {
const sourceCode = context.sourceCode;
const text = sourceCode.getText();
const attrStart = autofocusAttr.range[0];
const attrEnd = autofocusAttr.range[1];
let removeStart = attrStart;
while (removeStart > 0 && /\s/.test(text[removeStart - 1])) {
removeStart--;
}
return fixer.removeRange([removeStart, attrEnd]);
},
});
}
},
GlimmerMustacheStatement(node) {
if (!node.hash || !node.hash.pairs) {
return;
}
const autofocusPair = node.hash.pairs.find((pair) => pair.key === 'autofocus');
if (autofocusPair) {
context.report({
node: autofocusPair,
messageId: 'noAutofocus',
});
}
},
};
},
};