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-unused-block-params.js
More file actions
179 lines (161 loc) · 4.85 KB
/
template-no-unused-block-params.js
File metadata and controls
179 lines (161 loc) · 4.85 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
function collectChildNodes(n) {
const children = [];
if (n.program) {
children.push(n.program);
}
if (n.inverse) {
children.push(n.inverse);
}
if (n.params) {
children.push(...n.params);
}
if (n.hash?.pairs) {
children.push(...n.hash.pairs.map((p) => p.value));
}
if (n.body) {
children.push(...n.body);
}
if (n.path) {
children.push(n.path);
}
if (n.attributes) {
children.push(...n.attributes.map((a) => a.value));
}
if (n.children) {
children.push(...n.children);
}
// GlimmerPathExpression also has 'parts', so make sure we're not treating
// concat'd path string parts as AST nodes.
if (n.type === 'GlimmerConcatStatement' && n.parts) {
children.push(...n.parts);
}
return children;
}
function markParamIfUsed(name, blockParams, usedParams, shadowedParams) {
const firstPart = name.split('.')[0];
if (blockParams.includes(firstPart) && !shadowedParams.has(firstPart)) {
usedParams.add(firstPart);
}
}
function isPartialStatement(n) {
return (
(n.type === 'GlimmerMustacheStatement' || n.type === 'GlimmerBlockStatement') &&
n.path?.original === 'partial'
);
}
function buildShadowedSet(shadowedParams, innerBlockParams, outerBlockParams) {
const newShadowed = new Set(shadowedParams);
for (const p of innerBlockParams) {
if (outerBlockParams.includes(p)) {
newShadowed.add(p);
}
}
return newShadowed;
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow unused block parameters in templates',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-unused-block-params.md',
templateMode: 'both',
},
schema: [],
messages: {
unusedBlockParam: "'{{param}}' is defined but never used",
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-unused-block-params.js',
docs: 'docs/rule/no-unused-block-params.md',
tests: 'test/unit/rules/no-unused-block-params-test.js',
},
},
create(context) {
return {
GlimmerBlockStatement(node) {
const blockParams = node.program?.blockParams || [];
if (blockParams.length === 0) {
return;
}
const usedParams = new Set();
function checkNode(n, shadowedParams) {
if (!n) {
return;
}
if (n.type === 'GlimmerPathExpression') {
markParamIfUsed(n.original, blockParams, usedParams, shadowedParams);
}
if (n.type === 'GlimmerElementNode') {
markParamIfUsed(n.tag, blockParams, usedParams, shadowedParams);
}
if (isPartialStatement(n)) {
for (const p of blockParams) {
if (!shadowedParams.has(p)) {
usedParams.add(p);
}
}
}
// When entering a nested block, add its blockParams to the shadowed set
if (n.type === 'GlimmerBlockStatement' && n.program?.blockParams?.length > 0) {
const newShadowed = buildShadowedSet(
shadowedParams,
n.program.blockParams,
blockParams
);
checkBlockParts(n, blockParams, usedParams, shadowedParams, newShadowed, checkNode);
return;
}
// Recursively check children
for (const child of collectChildNodes(n)) {
checkNode(child, shadowedParams);
}
}
checkNode(node.program, new Set());
// Find the last index of a used param
let lastUsedIndex = -1;
for (let i = blockParams.length - 1; i >= 0; i--) {
if (usedParams.has(blockParams[i])) {
lastUsedIndex = i;
break;
}
}
// Only report trailing unused params (after the last used one)
const unusedTrailing = blockParams.slice(lastUsedIndex + 1);
const firstUnusedTrailing = unusedTrailing[0];
if (firstUnusedTrailing) {
context.report({
node,
messageId: 'unusedBlockParam',
data: { param: firstUnusedTrailing },
});
}
},
};
},
};
function checkBlockParts(n, blockParams, usedParams, shadowedParams, newShadowed, checkNodeFn) {
// Check the path/params of the block statement itself with current scope
if (n.path) {
checkNodeFn(n.path, shadowedParams);
}
if (n.params) {
for (const param of n.params) {
checkNodeFn(param, shadowedParams);
}
}
if (n.hash?.pairs) {
for (const pair of n.hash.pairs) {
checkNodeFn(pair.value, shadowedParams);
}
}
// Check the program body with the updated shadowed set
if (n.program) {
checkNodeFn(n.program, newShadowed);
}
if (n.inverse) {
checkNodeFn(n.inverse, newShadowed);
}
}