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-invalid-interactive.js
More file actions
261 lines (230 loc) · 6.93 KB
/
template-no-invalid-interactive.js
File metadata and controls
261 lines (230 loc) · 6.93 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
function hasAttr(node, name) {
return node.attributes?.some((a) => a.name === name);
}
function getTextAttr(node, name) {
const attr = node.attributes?.find((a) => a.name === name);
if (attr?.value?.type === 'GlimmerTextNode') {
return attr.value.chars;
}
return undefined;
}
const DISALLOWED_DOM_EVENTS = new Set([
// Mouse events:
'click',
'dblclick',
'mousedown',
'mousemove',
'mouseover',
'mouseout',
'mouseup',
// Keyboard events:
'keydown',
'keypress',
'keyup',
]);
const ELEMENT_ALLOWED_EVENTS = {
form: new Set(['submit', 'reset', 'change']),
img: new Set(['load', 'error']),
};
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow non-interactive elements with interactive handlers',
category: 'Accessibility',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-invalid-interactive.md',
},
schema: [
{
type: 'object',
properties: {
additionalInteractiveTags: { type: 'array', items: { type: 'string' } },
ignoredTags: { type: 'array', items: { type: 'string' } },
ignoreTabindex: { type: 'boolean' },
ignoreUsemap: { type: 'boolean' },
},
additionalProperties: false,
},
],
messages: {
noInvalidInteractive:
'Non-interactive element <{{tagName}}> should not have interactive handler "{{handler}}".',
},
},
create(context) {
const options = context.options[0] || {};
const additionalInteractiveTags = new Set(options.additionalInteractiveTags || []);
const ignoredTags = new Set(options.ignoredTags || []);
const ignoreTabindex = options.ignoreTabindex || false;
const ignoreUsemap = options.ignoreUsemap || false;
const NATIVE_INTERACTIVE_ELEMENTS = new Set([
'a',
'button',
'canvas',
'details',
'embed',
'iframe',
'input',
'label',
'select',
'textarea',
]);
const INTERACTIVE_ROLES = new Set([
'button',
'checkbox',
'link',
'menuitem',
'menuitemcheckbox',
'menuitemradio',
'option',
'radio',
'searchbox',
'slider',
'spinbutton',
'switch',
'tab',
'textbox',
'combobox',
'gridcell',
]);
function isInteractive(node) {
const tag = node.tag?.toLowerCase();
if (!tag) {
return false;
}
if (additionalInteractiveTags.has(tag)) {
return true;
}
if (NATIVE_INTERACTIVE_ELEMENTS.has(tag)) {
// Hidden input is not interactive
if (tag === 'input') {
const type = getTextAttr(node, 'type');
if (type === 'hidden') {
return false;
}
}
return true;
}
// Check role
const role = getTextAttr(node, 'role');
if (role && INTERACTIVE_ROLES.has(role)) {
return true;
}
// Check tabindex
if (!ignoreTabindex && hasAttr(node, 'tabindex')) {
return true;
}
// Check contenteditable
const ce = getTextAttr(node, 'contenteditable');
if (ce && ce !== 'false') {
return true;
}
// Check usemap
if (!ignoreUsemap && hasAttr(node, 'usemap')) {
return true;
}
return false;
}
return {
// eslint-disable-next-line complexity
GlimmerElementNode(node) {
const tag = node.tag?.toLowerCase();
if (!tag) {
return;
}
if (ignoredTags.has(tag)) {
return;
}
// Skip if element is interactive
if (isInteractive(node)) {
return;
}
// Skip components (PascalCase)
if (/^[A-Z]/.test(node.tag)) {
return;
}
const allowedEvents = ELEMENT_ALLOWED_EVENTS[tag];
// Check attributes
for (const attr of node.attributes || []) {
const attrName = attr.name?.toLowerCase();
if (!attrName || attrName.startsWith('@')) {
continue;
}
const isDynamic =
attr.value?.type === 'GlimmerMustacheStatement' ||
attr.value?.type === 'GlimmerConcatStatement';
if (!isDynamic) {
continue;
}
const isOnAttr = attrName.startsWith('on') && attrName.length > 2;
const event = isOnAttr ? attrName.slice(2) : null;
// Allow element-specific events (e.g. submit/reset/change on form, load/error on img)
if (isOnAttr && event && allowedEvents?.has(event)) {
continue;
}
const isActionHelper =
attr.value?.type === 'GlimmerMustacheStatement' &&
attr.value.path?.original === 'action';
// Flag {{action}} helper used in any attribute on a non-interactive element
if (isActionHelper) {
context.report({
node,
messageId: 'noInvalidInteractive',
data: { tagName: tag, handler: attrName },
});
continue;
}
// Flag disallowed DOM events (click, mousedown, keydown, etc.) with dynamic values
if (isOnAttr && DISALLOWED_DOM_EVENTS.has(event)) {
context.report({
node,
messageId: 'noInvalidInteractive',
data: { tagName: tag, handler: attrName },
});
}
}
// Check modifiers
for (const mod of node.modifiers || []) {
const modName = mod.path?.original;
if (modName === 'on') {
const eventParam = mod.params?.[0];
const event =
eventParam?.type === 'GlimmerStringLiteral' ? eventParam.value : undefined;
// Allow element-specific events
if (event && allowedEvents?.has(event)) {
continue;
}
// Allow non-disallowed events (scroll, copy, toggle, pause, etc.)
if (event && !DISALLOWED_DOM_EVENTS.has(event)) {
continue;
}
context.report({
node,
messageId: 'noInvalidInteractive',
data: { tagName: tag, handler: '{{on}}' },
});
} else if (modName === 'action') {
// Determine the event from on= hash param (default: 'click')
let event = 'click';
const onPair = mod.hash?.pairs?.find((p) => p.key === 'on');
if (onPair) {
event = onPair.value?.value || onPair.value?.original || 'click';
}
// Allow element-specific events
if (allowedEvents?.has(event)) {
continue;
}
context.report({
node,
messageId: 'noInvalidInteractive',
data: { tagName: tag, handler: '{{action}}' },
});
}
}
},
};
},
};