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-unsupported-role-attributes.js
More file actions
171 lines (143 loc) · 4.82 KB
/
template-no-unsupported-role-attributes.js
File metadata and controls
171 lines (143 loc) · 4.82 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
const { roles, elementRoles } = require('aria-query');
function createUnsupportedAttributeErrorMessage(attribute, role, element) {
if (element) {
return `The attribute ${attribute} is not supported by the element ${element} with the implicit role of ${role}`;
}
return `The attribute ${attribute} is not supported by the role ${role}`;
}
function getImplicitRole(tagName, typeAttribute) {
if (tagName === 'input') {
for (const key of elementRoles.keys()) {
if (key.name === tagName && key.attributes) {
for (const attribute of key.attributes) {
if (attribute.name === 'type' && attribute.value === typeAttribute) {
return elementRoles.get(key)[0];
}
}
}
}
}
const key = [...elementRoles.keys()].find((entry) => entry.name === tagName);
const implicitRoles = key && elementRoles.get(key);
return implicitRoles && implicitRoles[0];
}
function getExplicitRole(node) {
const roleAttr = node.attributes?.find((attr) => attr.name === 'role');
if (roleAttr && roleAttr.value?.type === 'GlimmerTextNode') {
return roleAttr.value.chars.trim();
}
return null;
}
function getTypeAttribute(node) {
const typeAttr = node.attributes?.find((attr) => attr.name === 'type');
if (typeAttr && typeAttr.value?.type === 'GlimmerTextNode') {
return typeAttr.value.chars.trim();
}
return null;
}
function removeRangeWithAdjacentWhitespace(sourceText, range) {
let [start, end] = range;
if (sourceText[end - 1] === ' ') {
return [start, end];
}
if (sourceText[start - 1] === ' ') {
start -= 1;
} else if (sourceText[end] === ' ') {
end += 1;
}
return [start, end];
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow ARIA attributes that are not supported by the element role',
category: 'Accessibility',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-unsupported-role-attributes.md',
templateMode: 'both',
},
fixable: 'code',
schema: [],
messages: {
unsupportedExplicit: 'The attribute {{attribute}} is not supported by the role {{role}}',
unsupportedImplicit:
'The attribute {{attribute}} is not supported by the element {{element}} with the implicit role of {{role}}',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-unsupported-role-attributes.js',
docs: 'docs/rule/no-unsupported-role-attributes.md',
tests: 'test/unit/rules/no-unsupported-role-attributes-test.js',
},
},
create(context) {
const sourceCode = context.sourceCode;
function reportUnsupported(node, invalidNode, attribute, role, element) {
const messageId = element ? 'unsupportedImplicit' : 'unsupportedExplicit';
context.report({
node,
messageId,
data: element ? { attribute, role, element } : { attribute, role },
fix(fixer) {
const [start, end] = removeRangeWithAdjacentWhitespace(
sourceCode.getText(),
invalidNode.range
);
return fixer.removeRange([start, end]);
},
});
}
return {
GlimmerElementNode(node) {
let role = getExplicitRole(node);
let element;
if (!role) {
element = node.tag;
const typeAttribute = getTypeAttribute(node);
role = getImplicitRole(element, typeAttribute);
}
if (!role) {
return;
}
const roleDefinition = roles.get(role);
if (!roleDefinition) {
return;
}
const supportedProps = Object.keys(roleDefinition.props);
for (const attr of node.attributes || []) {
if (attr.type !== 'GlimmerAttrNode' || !attr.name?.startsWith('aria-')) {
continue;
}
if (!supportedProps.includes(attr.name)) {
reportUnsupported(node, attr, attr.name, role, element);
}
}
},
GlimmerMustacheStatement(node) {
if (!node.hash || !node.hash.pairs) {
return;
}
const rolePair = node.hash.pairs.find((pair) => pair.key === 'role');
if (!rolePair || rolePair.value?.type !== 'GlimmerStringLiteral') {
return;
}
const role = rolePair.value.value;
if (!role) {
return;
}
const roleDefinition = roles.get(role);
if (!roleDefinition) {
return;
}
const supportedProps = Object.keys(roleDefinition.props);
const ariaPairs = node.hash.pairs.filter((pair) => pair.key.startsWith('aria-'));
for (const pair of ariaPairs) {
if (!supportedProps.includes(pair.key)) {
reportUnsupported(node, pair, pair.key, role);
}
}
},
};
},
};