@discourse/lint-configs 2.42.0 → 2.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/eslint-rules/no-unnecessary-tracked.mjs +242 -0
- package/eslint.mjs +3 -0
- package/package.json +1 -1
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// key: the mutable argument name
|
|
2
|
+
// value array: component names
|
|
3
|
+
const MUTATING_COMPONENTS = {
|
|
4
|
+
"@value": [
|
|
5
|
+
"Input",
|
|
6
|
+
"Textarea",
|
|
7
|
+
"TextField",
|
|
8
|
+
"DatePicker",
|
|
9
|
+
"ChatChannelChooser",
|
|
10
|
+
],
|
|
11
|
+
"@checked": ["Input", "PreferenceCheckbox"],
|
|
12
|
+
"@selection": ["RadioButton", "InstallThemeItem", "ChatToTopicSelector"],
|
|
13
|
+
"@postAction": ["AdminPenaltyPostAction"],
|
|
14
|
+
"@reason": ["AdminPenaltyReason"],
|
|
15
|
+
"@tags": ["TagChooser", "ChatToTopicSelector"],
|
|
16
|
+
"@capsLockOn": ["PasswordField"],
|
|
17
|
+
"@message": ["FlagActionType"],
|
|
18
|
+
"@isConfirmed": ["FlagActionType"],
|
|
19
|
+
"@topicTitle": ["ChatToTopicSelector"],
|
|
20
|
+
"@categoryId": ["ChatToTopicSelector"],
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function getImportIdentifier(node, source, namedImportIdentifier = null) {
|
|
24
|
+
if (node.source.value !== source) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return node.specifiers
|
|
29
|
+
.filter((specifier) => {
|
|
30
|
+
return (
|
|
31
|
+
(specifier.type === "ImportSpecifier" &&
|
|
32
|
+
specifier.imported.name === namedImportIdentifier) ||
|
|
33
|
+
(!namedImportIdentifier && specifier.type === "ImportDefaultSpecifier")
|
|
34
|
+
);
|
|
35
|
+
})
|
|
36
|
+
.map((specifier) => specifier.local.name)
|
|
37
|
+
.pop();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isComponentClass(node, componentNames) {
|
|
41
|
+
return (
|
|
42
|
+
node.superClass?.type === "Identifier" &&
|
|
43
|
+
componentNames.has(node.superClass.name)
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function getAssignedPropertyName(node) {
|
|
48
|
+
if (
|
|
49
|
+
node?.type !== "MemberExpression" ||
|
|
50
|
+
node.object?.type !== "ThisExpression"
|
|
51
|
+
) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!node.computed && node.property?.type === "Identifier") {
|
|
56
|
+
return node.property.name;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (node.computed && node.property?.type === "Literal") {
|
|
60
|
+
return node.property.value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function hasTrackedDecorator(node) {
|
|
65
|
+
return node.decorators.some((decorator) => {
|
|
66
|
+
return (
|
|
67
|
+
decorator.expression.type === "Identifier" &&
|
|
68
|
+
decorator.expression.name === "tracked"
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export default {
|
|
74
|
+
meta: {
|
|
75
|
+
type: "suggestion",
|
|
76
|
+
docs: {
|
|
77
|
+
description:
|
|
78
|
+
"Component properties should be @tracked only when they're reassigned at some point.",
|
|
79
|
+
},
|
|
80
|
+
schema: [], // no options
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
create(context) {
|
|
84
|
+
const componentNames = new Set();
|
|
85
|
+
const selectKitComponents = new Set();
|
|
86
|
+
let currentComponent;
|
|
87
|
+
|
|
88
|
+
function markAssigned(name) {
|
|
89
|
+
if (currentComponent && name) {
|
|
90
|
+
currentComponent.assigned.add(name);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function handleTrackedProperty(node) {
|
|
95
|
+
if (!currentComponent || node.static || !node.decorators?.length) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (!hasTrackedDecorator(node) || node.key?.type !== "Identifier") {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
currentComponent.trackedProps.set(node.key.name, node);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function handleGlimmerSubExpression(node) {
|
|
107
|
+
if (!currentComponent || !node.path?.head) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (node.path.head.type !== "VarHead" || node.path.head.name !== "mut") {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const firstParam = node.params?.[0];
|
|
116
|
+
if (firstParam.type !== "GlimmerPathExpression") {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (firstParam.head.type === "ThisHead" && firstParam.tail.length) {
|
|
121
|
+
currentComponent.mutUses.add(firstParam.tail[0]);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function handleGlimmerElementNode(node) {
|
|
126
|
+
if (!currentComponent) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const componentName = node.tag || node.name;
|
|
131
|
+
if (!componentName) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const attributes = node.attributes || [];
|
|
136
|
+
for (const attr of attributes) {
|
|
137
|
+
if (attr.type !== "GlimmerAttrNode") {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const isMutatingAttr =
|
|
142
|
+
MUTATING_COMPONENTS[attr.name]?.includes(componentName) ||
|
|
143
|
+
(attr.name === "@value" && selectKitComponents.has(componentName));
|
|
144
|
+
if (!isMutatingAttr) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!attr.value || attr.value.type !== "GlimmerMustacheStatement") {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const path = attr.value.path;
|
|
153
|
+
if (
|
|
154
|
+
path?.type === "GlimmerPathExpression" &&
|
|
155
|
+
path.head?.type === "ThisHead" &&
|
|
156
|
+
path.tail?.length
|
|
157
|
+
) {
|
|
158
|
+
currentComponent.valueUses.add(path.tail[0]);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function handleClass(node) {
|
|
164
|
+
if (isComponentClass(node, componentNames)) {
|
|
165
|
+
currentComponent = {
|
|
166
|
+
node,
|
|
167
|
+
trackedProps: new Map(),
|
|
168
|
+
assigned: new Set(),
|
|
169
|
+
mutUses: new Set(),
|
|
170
|
+
valueUses: new Set(),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function handleClassExit(node) {
|
|
176
|
+
if (currentComponent?.node !== node) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
for (const [name, propNode] of currentComponent.trackedProps) {
|
|
181
|
+
const reassigned = currentComponent.assigned.has(name);
|
|
182
|
+
const hasMutUse = currentComponent.mutUses.has(name);
|
|
183
|
+
const hasValueUse = currentComponent.valueUses.has(name);
|
|
184
|
+
|
|
185
|
+
if (!reassigned && !hasMutUse && !hasValueUse) {
|
|
186
|
+
context.report({
|
|
187
|
+
node: propNode,
|
|
188
|
+
message: `\`${name}\` property is @tracked but isn't modified anywhere.`,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
currentComponent = null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
ImportDeclaration(node) {
|
|
198
|
+
if (node.source.value.includes("/select-kit/")) {
|
|
199
|
+
node.specifiers.forEach((specifier) => {
|
|
200
|
+
selectKitComponents.add(specifier.local.name);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const glimmerComponentName = getImportIdentifier(
|
|
205
|
+
node,
|
|
206
|
+
"@glimmer/component"
|
|
207
|
+
);
|
|
208
|
+
if (glimmerComponentName) {
|
|
209
|
+
componentNames.add(glimmerComponentName);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const emberComponentName = getImportIdentifier(
|
|
213
|
+
node,
|
|
214
|
+
"@ember/component"
|
|
215
|
+
);
|
|
216
|
+
if (emberComponentName) {
|
|
217
|
+
componentNames.add(emberComponentName);
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
|
|
221
|
+
ClassDeclaration: handleClass,
|
|
222
|
+
ClassExpression: handleClass,
|
|
223
|
+
|
|
224
|
+
"ClassDeclaration:exit": handleClassExit,
|
|
225
|
+
"ClassExpression:exit": handleClassExit,
|
|
226
|
+
|
|
227
|
+
ClassProperty: handleTrackedProperty,
|
|
228
|
+
PropertyDefinition: handleTrackedProperty,
|
|
229
|
+
|
|
230
|
+
AssignmentExpression(node) {
|
|
231
|
+
markAssigned(getAssignedPropertyName(node.left));
|
|
232
|
+
},
|
|
233
|
+
|
|
234
|
+
UpdateExpression(node) {
|
|
235
|
+
markAssigned(getAssignedPropertyName(node.argument));
|
|
236
|
+
},
|
|
237
|
+
|
|
238
|
+
GlimmerSubExpression: handleGlimmerSubExpression,
|
|
239
|
+
GlimmerElementNode: handleGlimmerElementNode,
|
|
240
|
+
};
|
|
241
|
+
},
|
|
242
|
+
};
|
package/eslint.mjs
CHANGED
|
@@ -27,6 +27,7 @@ import noDiscourseComputed from "./eslint-rules/no-discourse-computed.mjs";
|
|
|
27
27
|
import noOnclick from "./eslint-rules/no-onclick.mjs";
|
|
28
28
|
import noRouteTemplate from "./eslint-rules/no-route-template.mjs";
|
|
29
29
|
import noSimpleQuerySelector from "./eslint-rules/no-simple-query-selector.mjs";
|
|
30
|
+
import noUnnecessaryTracked from "./eslint-rules/no-unnecessary-tracked.mjs";
|
|
30
31
|
import noUnusedServices from "./eslint-rules/no-unused-services.mjs";
|
|
31
32
|
import pluginApiNoVersion from "./eslint-rules/plugin-api-no-version.mjs";
|
|
32
33
|
import serviceInjectImport from "./eslint-rules/service-inject-import.mjs";
|
|
@@ -146,6 +147,7 @@ export default [
|
|
|
146
147
|
"moved-packages-import-paths": movedPackagesImportPaths,
|
|
147
148
|
"no-discourse-computed": noDiscourseComputed,
|
|
148
149
|
"test-filename-suffix": testFilenameSuffix,
|
|
150
|
+
"no-unnecessary-tracked": noUnnecessaryTracked,
|
|
149
151
|
},
|
|
150
152
|
},
|
|
151
153
|
},
|
|
@@ -317,6 +319,7 @@ export default [
|
|
|
317
319
|
"discourse/test-filename-suffix": ["error"],
|
|
318
320
|
"discourse/keep-array-sorted": ["error"],
|
|
319
321
|
"discourse/no-discourse-computed": ["error"],
|
|
322
|
+
"discourse/no-unnecessary-tracked": ["warn"],
|
|
320
323
|
},
|
|
321
324
|
},
|
|
322
325
|
{
|