-
-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathtemplate-no-unsupported-role-attributes.js
More file actions
210 lines (183 loc) · 6.28 KB
/
template-no-unsupported-role-attributes.js
File metadata and controls
210 lines (183 loc) · 6.28 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
const { roles, elementRoles } = require('aria-query');
function getStaticAttrValue(node, name) {
const attr = node.attributes?.find((a) => a.name === name);
if (!attr) {
return undefined;
}
if (!attr.value || attr.value.type !== 'GlimmerTextNode') {
// Presence with dynamic value — treat as "set" but unknown string.
return '';
}
return attr.value.chars.trim();
}
function nodeSatisfiesAttributeConstraint(node, attrSpec) {
const value = getStaticAttrValue(node, attrSpec.name);
const isSet = value !== undefined;
if (attrSpec.constraints?.includes('set')) {
return isSet;
}
if (attrSpec.constraints?.includes('undefined')) {
return !isSet;
}
if (attrSpec.value !== undefined) {
// HTML enumerated attribute values are ASCII case-insensitive
// (HTML common-microsyntaxes §2.3.3). aria-query's attrSpec.value is
// already lowercase, so lowercase the node's value for comparison.
return isSet && value.toLowerCase() === attrSpec.value;
}
// No constraint listed — just require presence.
return isSet;
}
function keyMatchesNode(node, key) {
if (key.name !== node.tag) {
return false;
}
if (!key.attributes || key.attributes.length === 0) {
return true;
}
return key.attributes.every((attrSpec) => nodeSatisfiesAttributeConstraint(node, attrSpec));
}
function getImplicitRole(node) {
// Honor aria-query's attribute constraints when mapping element -> implicit role.
// Each elementRoles entry lists attributes that must match (with optional
// constraints "set" / "undefined"); pick the most specific entry whose
// attribute spec is fully satisfied by the node.
//
// Heuristic: "specificity = attribute-constraint count". aria-query exports
// elementRoles as an unordered Map and does not document how consumers
// should resolve multi-match cases; this count-based tiebreak is an
// inference from the data shape. It resolves the motivating bugs:
// - <input type="text"> without `list` → textbox, not combobox
// (the combobox entry requires `list=set`, a stricter 2-attr match;
// the textbox entry's 1-attr type=text wins when `list` is absent).
// - <input type="password"> → no role (no elementRoles entry matches).
// If aria-query ever publishes a resolution order, switch to that.
let bestKey;
let bestSpecificity = -1;
for (const key of elementRoles.keys()) {
if (!keyMatchesNode(node, key)) {
continue;
}
const specificity = key.attributes?.length ?? 0;
if (specificity > bestSpecificity) {
bestKey = key;
bestSpecificity = specificity;
}
}
if (!bestKey) {
return undefined;
}
return elementRoles.get(bestKey)[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 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;
role = getImplicitRole(node);
}
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);
}
}
},
};
},
};