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-duplicate-id.js
More file actions
252 lines (230 loc) · 7.3 KB
/
template-no-duplicate-id.js
File metadata and controls
252 lines (230 loc) · 7.3 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
const IGNORE_IDS = new Set(['{{unique-id}}', '{{(unique-id)}}']);
function isControlFlowHelper(node) {
if (node.type === 'GlimmerBlockStatement' && node.path?.type === 'GlimmerPathExpression') {
return ['if', 'unless', 'each', 'each-in', 'let', 'with'].includes(node.path.original);
}
return false;
}
function isIfUnless(node) {
if (node.type === 'GlimmerBlockStatement' && node.path?.type === 'GlimmerPathExpression') {
return ['if', 'unless'].includes(node.path.original);
}
return false;
}
// Walk up the parent chain to find an ancestor element/block whose blockParams
// include headName. Used to make block-param-derived IDs unique per invocation.
function findBlockParamAncestor(node, headName) {
if (!headName) {
return null;
}
let p = node.parent;
while (p) {
if (p.type === 'GlimmerElementNode' && p.blockParams?.includes(headName)) {
return p;
}
if (p.type === 'GlimmerBlockStatement' && p.program?.blockParams?.includes(headName)) {
return p;
}
p = p.parent;
}
return null;
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow duplicate id attributes',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-duplicate-id.md',
templateMode: 'both',
},
schema: [],
messages: { duplicate: 'ID attribute values must be unique' },
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-duplicate-id.js',
docs: 'docs/rule/no-duplicate-id.md',
tests: 'test/unit/rules/no-duplicate-id-test.js',
},
},
create(context) {
const sourceCode = context.getSourceCode();
let seenIdStack = [];
let conditionalStack = [];
function enterTemplate() {
seenIdStack = [new Set()];
conditionalStack = [];
}
function isDuplicateId(id) {
for (const seenIds of seenIdStack) {
if (seenIds.has(id)) {
return true;
}
}
return false;
}
function addId(id) {
seenIdStack.at(-1).add(id);
if (conditionalStack.length > 0) {
conditionalStack.at(-1).add(id);
}
}
function enterConditional() {
conditionalStack.push(new Set());
}
function exitConditional() {
const idsInConditional = conditionalStack.pop();
if (conditionalStack.length > 0) {
for (const id of idsInConditional) {
conditionalStack.at(-1).add(id);
}
} else {
seenIdStack.push(idsInConditional);
}
}
function enterConditionalBranch() {
seenIdStack.push(new Set());
}
function exitConditionalBranch() {
seenIdStack.pop();
}
// For a GlimmerMustacheStatement whose path is a PathExpression, return a
// location-aware key so that the same expression in different component
// invocations (e.g. {{inputProperties.id}} in two <MyComponent> blocks) gets
// a distinct key, while two occurrences inside the SAME block get the same key.
function getMustachePathKey(valueNode) {
const sourceText = sourceCode.getText(valueNode);
if (valueNode.path?.type === 'GlimmerPathExpression') {
const headName = valueNode.path.original.split('.')[0];
const ancestor = findBlockParamAncestor(valueNode, headName);
if (ancestor) {
const loc = ancestor.loc?.start;
const tag = ancestor.tag ?? ancestor.path?.original ?? '';
return `${sourceText}${tag}${loc ? `${loc.line}:${loc.column}` : ''}`;
}
}
return sourceText;
}
function resolveIdValue(valueNode) {
if (!valueNode) {
return null;
}
switch (valueNode.type) {
case 'GlimmerTextNode': {
return valueNode.chars || null;
}
case 'GlimmerStringLiteral': {
return valueNode.value || null;
}
case 'GlimmerMustacheStatement': {
if (valueNode.path?.type === 'GlimmerStringLiteral') {
return valueNode.path.value;
}
return getMustachePathKey(valueNode);
}
case 'GlimmerConcatStatement': {
if (valueNode.parts) {
return valueNode.parts
.map((part) => {
if (part.type === 'GlimmerTextNode') {
return part.chars;
}
if (
part.type === 'GlimmerMustacheStatement' &&
part.path?.type === 'GlimmerStringLiteral'
) {
return part.path.value;
}
if (part.type === 'GlimmerMustacheStatement') {
return getMustachePathKey(part);
}
return sourceCode.getText(part);
})
.join('');
}
return sourceCode.getText(valueNode);
}
default: {
return sourceCode.getText(valueNode);
}
}
}
function logIfDuplicate(reportNode, id) {
if (!id) {
return;
}
if (IGNORE_IDS.has(id)) {
return;
}
if (isDuplicateId(id)) {
context.report({ node: reportNode, messageId: 'duplicate' });
} else {
addId(id);
}
}
return {
GlimmerTemplate() {
enterTemplate();
},
'GlimmerTemplate:exit'() {
seenIdStack = [new Set()];
conditionalStack = [];
},
GlimmerElementNode(node) {
// Note: no blockParams scoping here. Elements with block params (e.g.
// <MyComponent as |foo|>) must not isolate their static IDs — a static
// "shared-id" inside and outside such an element ARE duplicates.
const idAttrNames = new Set(['id', '@id', '@elementId']);
for (const attr of node.attributes || []) {
if (idAttrNames.has(attr.name)) {
const id = resolveIdValue(attr.value);
logIfDuplicate(attr, id);
}
}
},
// Handle hash pairs in mustache/block statements (e.g., {{input elementId="foo"}})
GlimmerMustacheStatement(node) {
if (node.hash && node.hash.pairs) {
for (const pair of node.hash.pairs) {
if (['elementId', 'id'].includes(pair.key)) {
if (pair.value?.type === 'GlimmerStringLiteral') {
logIfDuplicate(pair, pair.value.value);
}
}
}
}
},
GlimmerBlockStatement(node) {
if (isControlFlowHelper(node)) {
enterConditional();
} else if (node.hash && node.hash.pairs) {
for (const pair of node.hash.pairs) {
if (['elementId', 'id'].includes(pair.key)) {
if (pair.value?.type === 'GlimmerStringLiteral') {
logIfDuplicate(pair, pair.value.value);
}
}
}
}
},
'GlimmerBlockStatement:exit'(node) {
if (isControlFlowHelper(node)) {
exitConditional();
}
},
GlimmerBlock(node) {
const parent = node.parent;
if (parent && isIfUnless(parent)) {
enterConditionalBranch();
}
},
'GlimmerBlock:exit'(node) {
const parent = node.parent;
if (parent && isIfUnless(parent)) {
exitConditionalBranch();
}
},
};
},
};