eslint-plugin-react-x 1.23.3-next.7 → 1.24.0-beta.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/dist/index.js +419 -481
- package/dist/index.mjs +419 -481
- package/package.json +11 -11
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { F, O, isString, not, or, isNullable } from '@eslint-react/eff';
|
|
2
1
|
import * as JSX7 from '@eslint-react/jsx';
|
|
3
2
|
import { createRuleForPlugin, getSettingsFromContext, unsafeDecodeSettings } from '@eslint-react/shared';
|
|
4
3
|
import * as AST11 from '@eslint-react/ast';
|
|
5
4
|
import { isForwardRefCall, isThisSetState, isClassComponent, isChildrenCount, isChildrenForEach, isChildrenMap, isChildrenOnly, isChildrenToArray, useComponentCollectorLegacy, isCloneElementCall, isCreateRefCall, isComponentName, useComponentCollector, ERFunctionComponentFlag, isChildrenToArrayCall, ERComponentHint, isDirectValueOfRenderPropertyLoose, isDeclaredInRenderPropLoose, isInsideCreateElementProps, isInsideRenderMethod, ERClassComponentFlag, isReactHookCall, isCreateElementCall, isInitializedFromReact } from '@eslint-react/core';
|
|
5
|
+
import { _ } from '@eslint-react/eff';
|
|
6
6
|
import { AST_NODE_TYPES } from '@typescript-eslint/types';
|
|
7
7
|
import { isMatching, match, P } from 'ts-pattern';
|
|
8
8
|
import { compare } from 'compare-versions';
|
|
@@ -15,7 +15,7 @@ import { getTypeImmutability, isUnknown, isImmutable, isReadonlyShallow, isReado
|
|
|
15
15
|
|
|
16
16
|
// package.json
|
|
17
17
|
var name = "eslint-plugin-react-x";
|
|
18
|
-
var version = "1.
|
|
18
|
+
var version = "1.24.0-beta.0";
|
|
19
19
|
var createRule = createRuleForPlugin("x");
|
|
20
20
|
|
|
21
21
|
// src/rules/avoid-shorthand-boolean.ts
|
|
@@ -38,18 +38,19 @@ var avoid_shorthand_boolean_default = createRule({
|
|
|
38
38
|
},
|
|
39
39
|
name: RULE_NAME,
|
|
40
40
|
create(context) {
|
|
41
|
-
function getReportDescriptor(node) {
|
|
42
|
-
return node.value != null ? O.none() : O.some({
|
|
43
|
-
messageId: "avoidShorthandBoolean",
|
|
44
|
-
node,
|
|
45
|
-
data: {
|
|
46
|
-
propName: JSX7.getPropName(node)
|
|
47
|
-
},
|
|
48
|
-
fix: (fixer) => fixer.insertTextAfter(node.name, `={true}`)
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
41
|
return {
|
|
52
|
-
JSXAttribute
|
|
42
|
+
JSXAttribute(node) {
|
|
43
|
+
if (node.value === null) {
|
|
44
|
+
context.report({
|
|
45
|
+
messageId: "avoidShorthandBoolean",
|
|
46
|
+
node,
|
|
47
|
+
data: {
|
|
48
|
+
propName: JSX7.getPropName(node)
|
|
49
|
+
},
|
|
50
|
+
fix: (fixer) => fixer.insertTextAfter(node.name, `={true}`)
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
53
54
|
};
|
|
54
55
|
},
|
|
55
56
|
defaultOptions: []
|
|
@@ -112,7 +113,7 @@ var ensure_forward_ref_using_ref_default = createRule({
|
|
|
112
113
|
if (component == null || !AST11.isFunction(component)) {
|
|
113
114
|
return;
|
|
114
115
|
}
|
|
115
|
-
const
|
|
116
|
+
const ref = component.params[1];
|
|
116
117
|
if (ref != null) {
|
|
117
118
|
return;
|
|
118
119
|
}
|
|
@@ -134,15 +135,15 @@ function getName(node) {
|
|
|
134
135
|
return getName(node.expression);
|
|
135
136
|
}
|
|
136
137
|
if (node.type === AST_NODE_TYPES.Identifier || node.type === AST_NODE_TYPES.PrivateIdentifier) {
|
|
137
|
-
return
|
|
138
|
+
return node.name;
|
|
138
139
|
}
|
|
139
140
|
if (node.type === AST_NODE_TYPES.Literal) {
|
|
140
|
-
return
|
|
141
|
+
return String(node.value);
|
|
141
142
|
}
|
|
142
143
|
if (node.type === AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0) {
|
|
143
|
-
return
|
|
144
|
+
return node.quasis[0]?.value.raw;
|
|
144
145
|
}
|
|
145
|
-
return
|
|
146
|
+
return _;
|
|
146
147
|
}
|
|
147
148
|
var no_access_state_in_setstate_default = createRule({
|
|
148
149
|
meta: {
|
|
@@ -161,89 +162,86 @@ var no_access_state_in_setstate_default = createRule({
|
|
|
161
162
|
if (!context.sourceCode.text.includes("setState")) {
|
|
162
163
|
return {};
|
|
163
164
|
}
|
|
164
|
-
const
|
|
165
|
-
const
|
|
166
|
-
const
|
|
165
|
+
const classEntries = [];
|
|
166
|
+
const methodEntries = [];
|
|
167
|
+
const setStateEntries = [];
|
|
167
168
|
return {
|
|
168
169
|
CallExpression(node) {
|
|
169
170
|
if (!isThisSetState(node)) {
|
|
170
171
|
return;
|
|
171
172
|
}
|
|
172
|
-
|
|
173
|
+
setStateEntries.push([node, false]);
|
|
173
174
|
},
|
|
174
175
|
"CallExpression:exit"(node) {
|
|
175
176
|
if (!isThisSetState(node)) {
|
|
176
177
|
return;
|
|
177
178
|
}
|
|
178
|
-
|
|
179
|
+
setStateEntries.pop();
|
|
179
180
|
},
|
|
180
181
|
ClassDeclaration(node) {
|
|
181
|
-
|
|
182
|
+
classEntries.push([node, isClassComponent(node)]);
|
|
182
183
|
},
|
|
183
184
|
"ClassDeclaration:exit"() {
|
|
184
|
-
|
|
185
|
+
classEntries.pop();
|
|
185
186
|
},
|
|
186
187
|
ClassExpression(node) {
|
|
187
|
-
|
|
188
|
+
classEntries.push([node, isClassComponent(node)]);
|
|
188
189
|
},
|
|
189
190
|
"ClassExpression:exit"() {
|
|
190
|
-
|
|
191
|
+
classEntries.pop();
|
|
191
192
|
},
|
|
192
193
|
MemberExpression(node) {
|
|
193
194
|
if (!AST11.isThisExpression(node.object)) {
|
|
194
195
|
return;
|
|
195
196
|
}
|
|
196
|
-
const [currClass, isComponent] =
|
|
197
|
+
const [currClass, isComponent] = classEntries.at(-1) ?? [];
|
|
197
198
|
if (currClass == null || !isComponent) {
|
|
198
199
|
return;
|
|
199
200
|
}
|
|
200
|
-
const [currMethod, isStatic] =
|
|
201
|
+
const [currMethod, isStatic] = methodEntries.at(-1) ?? [];
|
|
201
202
|
if (currMethod == null || isStatic) {
|
|
202
203
|
return;
|
|
203
204
|
}
|
|
204
|
-
const [setState, hasThisState] =
|
|
205
|
+
const [setState, hasThisState] = setStateEntries.at(-1) ?? [];
|
|
205
206
|
if (setState == null || hasThisState) {
|
|
206
207
|
return;
|
|
207
208
|
}
|
|
208
|
-
if (
|
|
209
|
+
if (getName(node.property) !== "state") {
|
|
209
210
|
return;
|
|
210
211
|
}
|
|
211
212
|
context.report({ messageId: "noAccessStateInSetstate", node });
|
|
212
213
|
},
|
|
213
214
|
MethodDefinition(node) {
|
|
214
|
-
|
|
215
|
+
methodEntries.push([node, node.static]);
|
|
215
216
|
},
|
|
216
217
|
"MethodDefinition:exit"() {
|
|
217
|
-
|
|
218
|
+
methodEntries.pop();
|
|
218
219
|
},
|
|
219
220
|
PropertyDefinition(node) {
|
|
220
|
-
|
|
221
|
+
methodEntries.push([node, node.static]);
|
|
221
222
|
},
|
|
222
223
|
"PropertyDefinition:exit"() {
|
|
223
|
-
|
|
224
|
+
methodEntries.pop();
|
|
224
225
|
},
|
|
225
226
|
VariableDeclarator(node) {
|
|
226
|
-
const [currClass, isComponent] =
|
|
227
|
+
const [currClass, isComponent] = classEntries.at(-1) ?? [];
|
|
227
228
|
if (currClass == null || !isComponent) {
|
|
228
229
|
return;
|
|
229
230
|
}
|
|
230
|
-
const [currMethod, isStatic] =
|
|
231
|
+
const [currMethod, isStatic] = methodEntries.at(-1) ?? [];
|
|
231
232
|
if (currMethod == null || isStatic) {
|
|
232
233
|
return;
|
|
233
234
|
}
|
|
234
|
-
const [setState, hasThisState] =
|
|
235
|
+
const [setState, hasThisState] = setStateEntries.at(-1) ?? [];
|
|
235
236
|
if (setState == null || hasThisState) {
|
|
236
237
|
return;
|
|
237
238
|
}
|
|
238
239
|
if (!(node.init != null && AST11.isThisExpression(node.init) && node.id.type === AST_NODE_TYPES.ObjectPattern)) {
|
|
239
240
|
return;
|
|
240
241
|
}
|
|
241
|
-
const hasState = node.id.properties.some(
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
return false;
|
|
246
|
-
});
|
|
242
|
+
const hasState = node.id.properties.some(
|
|
243
|
+
(prop) => prop.type === AST_NODE_TYPES.Property && AST11.isKeyLiteralLike(prop, prop.key) && getName(prop.key) === "state"
|
|
244
|
+
);
|
|
247
245
|
if (!hasState) {
|
|
248
246
|
return;
|
|
249
247
|
}
|
|
@@ -296,32 +294,32 @@ function isUsingReactChildren(node, context) {
|
|
|
296
294
|
function getMapIndexParamName(node, context) {
|
|
297
295
|
const { callee } = node;
|
|
298
296
|
if (callee.type !== AST_NODE_TYPES.MemberExpression) {
|
|
299
|
-
return
|
|
297
|
+
return _;
|
|
300
298
|
}
|
|
301
299
|
if (callee.property.type !== AST_NODE_TYPES.Identifier) {
|
|
302
|
-
return
|
|
300
|
+
return _;
|
|
303
301
|
}
|
|
304
302
|
const { name: name2 } = callee.property;
|
|
305
303
|
if (!iteratorFunctionIndexParamPosition.has(name2)) {
|
|
306
|
-
return
|
|
304
|
+
return _;
|
|
307
305
|
}
|
|
308
306
|
const callbackArg = node.arguments[isUsingReactChildren(node, context) ? 1 : 0];
|
|
309
307
|
if (callbackArg == null) {
|
|
310
|
-
return
|
|
308
|
+
return _;
|
|
311
309
|
}
|
|
312
310
|
if (!AST11.isOneOf([AST_NODE_TYPES.ArrowFunctionExpression, AST_NODE_TYPES.FunctionExpression])(callbackArg)) {
|
|
313
|
-
return
|
|
311
|
+
return _;
|
|
314
312
|
}
|
|
315
313
|
const { params } = callbackArg;
|
|
316
314
|
const indexParamPosition = iteratorFunctionIndexParamPosition.get(name2);
|
|
317
|
-
if (
|
|
318
|
-
return
|
|
315
|
+
if (indexParamPosition == null) {
|
|
316
|
+
return _;
|
|
319
317
|
}
|
|
320
318
|
if (params.length < indexParamPosition + 1) {
|
|
321
|
-
return
|
|
319
|
+
return _;
|
|
322
320
|
}
|
|
323
321
|
const param = params.at(indexParamPosition);
|
|
324
|
-
return param != null && "name" in param ?
|
|
322
|
+
return param != null && "name" in param ? param.name : _;
|
|
325
323
|
}
|
|
326
324
|
var no_array_index_key_default = createRule({
|
|
327
325
|
meta: {
|
|
@@ -339,7 +337,7 @@ var no_array_index_key_default = createRule({
|
|
|
339
337
|
create(context) {
|
|
340
338
|
const indexParamNames = [];
|
|
341
339
|
function isArrayIndex(node) {
|
|
342
|
-
return node.type === AST_NODE_TYPES.Identifier && indexParamNames.some(
|
|
340
|
+
return node.type === AST_NODE_TYPES.Identifier && indexParamNames.some((name2) => name2 != null && name2 === node.name);
|
|
343
341
|
}
|
|
344
342
|
function isCreateOrCloneElementCall(node) {
|
|
345
343
|
return isCreateElementCall(node, context) || isCloneElementCall(node, context);
|
|
@@ -348,7 +346,7 @@ var no_array_index_key_default = createRule({
|
|
|
348
346
|
switch (node.type) {
|
|
349
347
|
// key={bar}
|
|
350
348
|
case AST_NODE_TYPES.Identifier: {
|
|
351
|
-
if (indexParamNames.some(
|
|
349
|
+
if (indexParamNames.some((name2) => name2 != null && name2 === node.name)) {
|
|
352
350
|
return [{
|
|
353
351
|
messageId: "noArrayIndexKey",
|
|
354
352
|
node
|
|
@@ -402,7 +400,7 @@ var no_array_index_key_default = createRule({
|
|
|
402
400
|
if (!isCreateOrCloneElementCall(node)) {
|
|
403
401
|
return;
|
|
404
402
|
}
|
|
405
|
-
const [
|
|
403
|
+
const [, props] = node.arguments;
|
|
406
404
|
if (props?.type !== AST_NODE_TYPES.ObjectExpression) {
|
|
407
405
|
return;
|
|
408
406
|
}
|
|
@@ -585,10 +583,17 @@ var no_children_prop_default = createRule({
|
|
|
585
583
|
create(context) {
|
|
586
584
|
return {
|
|
587
585
|
JSXElement(node) {
|
|
588
|
-
const
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
586
|
+
const prop = JSX7.getProp(
|
|
587
|
+
"children",
|
|
588
|
+
context.sourceCode.getScope(node),
|
|
589
|
+
node.openingElement.attributes
|
|
590
|
+
);
|
|
591
|
+
if (prop) {
|
|
592
|
+
context.report({
|
|
593
|
+
messageId: "noChildrenProp",
|
|
594
|
+
node: prop
|
|
595
|
+
});
|
|
596
|
+
}
|
|
592
597
|
}
|
|
593
598
|
};
|
|
594
599
|
},
|
|
@@ -657,7 +662,7 @@ var no_class_component_default = createRule({
|
|
|
657
662
|
...listeners,
|
|
658
663
|
"Program:exit"(node) {
|
|
659
664
|
const components = ctx.getAllComponents(node);
|
|
660
|
-
for (const { name: name2, node: component } of components.values()) {
|
|
665
|
+
for (const { name: name2 = "anonymous", node: component } of components.values()) {
|
|
661
666
|
if (component.body.body.some((m) => isComponentDidCatch(m) || isGetDerivedStateFromError(m))) {
|
|
662
667
|
continue;
|
|
663
668
|
}
|
|
@@ -666,7 +671,7 @@ var no_class_component_default = createRule({
|
|
|
666
671
|
node: component,
|
|
667
672
|
data: {
|
|
668
673
|
// eslint-disable-next-line eslint-plugin/no-unused-placeholders
|
|
669
|
-
name:
|
|
674
|
+
name: name2
|
|
670
675
|
}
|
|
671
676
|
});
|
|
672
677
|
}
|
|
@@ -732,22 +737,21 @@ var no_comment_textnodes_default = createRule({
|
|
|
732
737
|
const rawValue = context.sourceCode.getText(node);
|
|
733
738
|
return /^\s*\/(?:\/|\*)/mu.test(rawValue);
|
|
734
739
|
}
|
|
735
|
-
const
|
|
740
|
+
const visitorFunction = (node) => {
|
|
736
741
|
if (!AST11.isOneOf([AST_NODE_TYPES.JSXElement, AST_NODE_TYPES.JSXFragment])(node.parent)) {
|
|
737
|
-
return
|
|
742
|
+
return;
|
|
738
743
|
}
|
|
739
744
|
if (!hasCommentLike(node)) {
|
|
740
|
-
return
|
|
745
|
+
return;
|
|
741
746
|
}
|
|
742
747
|
if (!node.parent.type.includes("JSX")) {
|
|
743
|
-
return
|
|
748
|
+
return;
|
|
744
749
|
}
|
|
745
|
-
|
|
750
|
+
context.report({
|
|
746
751
|
messageId: "noCommentTextnodes",
|
|
747
752
|
node
|
|
748
753
|
});
|
|
749
754
|
};
|
|
750
|
-
const visitorFunction = F.flow(getReportDescriptor, O.map(context.report), F.constVoid);
|
|
751
755
|
return {
|
|
752
756
|
JSXText: visitorFunction,
|
|
753
757
|
Literal: visitorFunction
|
|
@@ -773,20 +777,22 @@ var no_complex_conditional_rendering_default = createRule({
|
|
|
773
777
|
},
|
|
774
778
|
name: RULE_NAME15,
|
|
775
779
|
create(context) {
|
|
776
|
-
|
|
780
|
+
const visitorFunction = (node) => {
|
|
777
781
|
const jsxExpContainer = node.parent?.parent;
|
|
778
782
|
if (!AST11.is(AST_NODE_TYPES.JSXExpressionContainer)(jsxExpContainer)) {
|
|
779
|
-
return
|
|
783
|
+
return;
|
|
780
784
|
}
|
|
781
785
|
if (!AST11.isOneOf([AST_NODE_TYPES.JSXElement, AST_NODE_TYPES.JSXFragment])(jsxExpContainer.parent)) {
|
|
782
|
-
return
|
|
786
|
+
return;
|
|
783
787
|
}
|
|
784
788
|
if (!jsxExpContainer.parent.children.includes(jsxExpContainer)) {
|
|
785
|
-
return
|
|
789
|
+
return;
|
|
786
790
|
}
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
791
|
+
context.report({
|
|
792
|
+
messageId: "noComplexConditionalRendering",
|
|
793
|
+
node: jsxExpContainer
|
|
794
|
+
});
|
|
795
|
+
};
|
|
790
796
|
return {
|
|
791
797
|
"JSXExpressionContainer > ConditionalExpression > ConditionalExpression": visitorFunction,
|
|
792
798
|
"JSXExpressionContainer > ConditionalExpression > LogicalExpression": visitorFunction,
|
|
@@ -1030,13 +1036,9 @@ var no_create_ref_default = createRule({
|
|
|
1030
1036
|
create(context) {
|
|
1031
1037
|
return {
|
|
1032
1038
|
CallExpression(node) {
|
|
1033
|
-
if (
|
|
1034
|
-
|
|
1039
|
+
if (isCreateRefCall(node, context) && !AST11.findParentNode(node, isClassComponent)) {
|
|
1040
|
+
context.report({ messageId: "noCreateRef", node });
|
|
1035
1041
|
}
|
|
1036
|
-
if (O.isSome(AST11.findParentNode(node, isClassComponent))) {
|
|
1037
|
-
return;
|
|
1038
|
-
}
|
|
1039
|
-
context.report({ messageId: "noCreateRef", node });
|
|
1040
1042
|
}
|
|
1041
1043
|
};
|
|
1042
1044
|
},
|
|
@@ -1078,14 +1080,10 @@ var no_default_props_default = createRule({
|
|
|
1078
1080
|
if (!isComponentName(object.name)) {
|
|
1079
1081
|
return;
|
|
1080
1082
|
}
|
|
1081
|
-
const
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
);
|
|
1086
|
-
if (!isComponent) {
|
|
1087
|
-
return;
|
|
1088
|
-
}
|
|
1083
|
+
const variable = VAR.findVariable(object.name, context.sourceCode.getScope(node));
|
|
1084
|
+
const variableNode = VAR.getVariableNode(variable, 0);
|
|
1085
|
+
if (variableNode === _) return;
|
|
1086
|
+
if (!AST11.isFunction(variableNode) && !isClassComponent(variableNode)) return;
|
|
1089
1087
|
context.report({ messageId: "noDefaultProps", node: property });
|
|
1090
1088
|
},
|
|
1091
1089
|
PropertyDefinition(node) {
|
|
@@ -1110,19 +1108,19 @@ function getName2(node) {
|
|
|
1110
1108
|
return getName2(node.expression);
|
|
1111
1109
|
}
|
|
1112
1110
|
if (node.type === AST_NODE_TYPES.Identifier || node.type === AST_NODE_TYPES.PrivateIdentifier) {
|
|
1113
|
-
return
|
|
1111
|
+
return node.name;
|
|
1114
1112
|
}
|
|
1115
1113
|
if (node.type === AST_NODE_TYPES.Literal) {
|
|
1116
|
-
return
|
|
1114
|
+
return node.value?.toString();
|
|
1117
1115
|
}
|
|
1118
1116
|
if (node.type === AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0) {
|
|
1119
|
-
return
|
|
1117
|
+
return node.quasis[0]?.value.raw;
|
|
1120
1118
|
}
|
|
1121
|
-
return
|
|
1119
|
+
return _;
|
|
1122
1120
|
}
|
|
1123
1121
|
function isAssignmentToThisState(node) {
|
|
1124
1122
|
const { left } = node;
|
|
1125
|
-
return left.type === AST_NODE_TYPES.MemberExpression && AST11.isThisExpression(left.object) &&
|
|
1123
|
+
return left.type === AST_NODE_TYPES.MemberExpression && AST11.isThisExpression(left.object) && getName2(left.property) === "state";
|
|
1126
1124
|
}
|
|
1127
1125
|
function isConstructorFunction(node) {
|
|
1128
1126
|
return AST11.isOneOf([AST_NODE_TYPES.FunctionDeclaration, AST_NODE_TYPES.FunctionExpression])(node) && AST11.isOneOf([AST_NODE_TYPES.MethodDefinition, AST_NODE_TYPES.PropertyDefinition])(node.parent) && node.parent.key.type === AST_NODE_TYPES.Identifier && node.parent.key.name === "constructor";
|
|
@@ -1141,31 +1139,26 @@ var no_direct_mutation_state_default = createRule({
|
|
|
1141
1139
|
},
|
|
1142
1140
|
name: RULE_NAME22,
|
|
1143
1141
|
create(context) {
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
O.bind("parentClass", () => AST11.findParentNodeGuard(
|
|
1142
|
+
return {
|
|
1143
|
+
AssignmentExpression(node) {
|
|
1144
|
+
if (!isAssignmentToThisState(node)) {
|
|
1145
|
+
return;
|
|
1146
|
+
}
|
|
1147
|
+
const parentClass = AST11.findParentNodeGuard(
|
|
1151
1148
|
node,
|
|
1152
1149
|
AST11.isOneOf([
|
|
1153
1150
|
AST_NODE_TYPES.ClassDeclaration,
|
|
1154
1151
|
AST_NODE_TYPES.ClassExpression
|
|
1155
1152
|
])
|
|
1156
|
-
)
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
);
|
|
1166
|
-
}
|
|
1167
|
-
return {
|
|
1168
|
-
AssignmentExpression: F.flow(getReportDescriptor, O.map(context.report))
|
|
1153
|
+
);
|
|
1154
|
+
if (!parentClass) return;
|
|
1155
|
+
if (isClassComponent(parentClass) && context.sourceCode.getScope(node).block !== AST11.findParentNodeGuard(node, isConstructorFunction)) {
|
|
1156
|
+
context.report({
|
|
1157
|
+
messageId: "noDirectMutationState",
|
|
1158
|
+
node
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1169
1162
|
};
|
|
1170
1163
|
},
|
|
1171
1164
|
defaultOptions: []
|
|
@@ -1196,7 +1189,7 @@ var no_duplicate_jsx_props_default = createRule({
|
|
|
1196
1189
|
continue;
|
|
1197
1190
|
}
|
|
1198
1191
|
const name2 = attr.name.name;
|
|
1199
|
-
if (
|
|
1192
|
+
if (typeof name2 !== "string") {
|
|
1200
1193
|
continue;
|
|
1201
1194
|
}
|
|
1202
1195
|
if (!props.includes(name2)) {
|
|
@@ -1260,16 +1253,10 @@ var no_duplicate_key_default = createRule({
|
|
|
1260
1253
|
break;
|
|
1261
1254
|
}
|
|
1262
1255
|
default: {
|
|
1263
|
-
const
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
O.bind("arg0", ({ call: call2 }) => O.fromNullable(call2.arguments[0]))
|
|
1268
|
-
);
|
|
1269
|
-
if (O.isNone(mbEntry)) {
|
|
1270
|
-
return;
|
|
1271
|
-
}
|
|
1272
|
-
const { arg0, call, iter } = mbEntry.value;
|
|
1256
|
+
const call = AST11.findParentNodeGuard(jsxElement, AST11.isMapCallLoose);
|
|
1257
|
+
const iter = call && AST11.findParentNodeStop(jsxElement, call, AST11.isFunction);
|
|
1258
|
+
const arg0 = call?.arguments[0];
|
|
1259
|
+
if (!call || !iter || !arg0) return;
|
|
1273
1260
|
if (AST11.unwrapTypeExpression(arg0) !== iter) {
|
|
1274
1261
|
return;
|
|
1275
1262
|
}
|
|
@@ -1370,21 +1357,19 @@ function getComponentPropsFixes(node, typeArguments, fixer, context) {
|
|
|
1370
1357
|
if (arg0 == null) {
|
|
1371
1358
|
return [];
|
|
1372
1359
|
}
|
|
1373
|
-
const fixedArg0Text = match(arg0).with({ type: AST_NODE_TYPES.Identifier }, (n) =>
|
|
1374
|
-
const fixedArg1Text = match(arg1).with(P.nullish, () =>
|
|
1375
|
-
if (
|
|
1360
|
+
const fixedArg0Text = match(arg0).with({ type: AST_NODE_TYPES.Identifier }, (n) => `...${n.name}`).with({ type: AST_NODE_TYPES.ObjectPattern }, (n) => n.properties.map(getText).join(", ")).otherwise(() => _);
|
|
1361
|
+
const fixedArg1Text = match(arg1).with(P.nullish, () => "ref").with({ type: AST_NODE_TYPES.Identifier, name: "ref" }, () => "ref").with({ type: AST_NODE_TYPES.Identifier, name: P.not("ref") }, (n) => `ref: ${n.name}`).otherwise(() => _);
|
|
1362
|
+
if (fixedArg0Text == null || fixedArg1Text == null) {
|
|
1376
1363
|
return [];
|
|
1377
1364
|
}
|
|
1378
|
-
const fixedPropsText = fixedArg0Text.value;
|
|
1379
|
-
const fixedRefText = fixedArg1Text.value;
|
|
1380
1365
|
if (typeArg0 == null || typeArg1 == null) {
|
|
1381
1366
|
return [
|
|
1382
1367
|
fixer.replaceText(
|
|
1383
1368
|
arg0,
|
|
1384
1369
|
[
|
|
1385
1370
|
"{",
|
|
1386
|
-
|
|
1387
|
-
|
|
1371
|
+
fixedArg1Text + ",",
|
|
1372
|
+
fixedArg0Text,
|
|
1388
1373
|
"}"
|
|
1389
1374
|
].join(" ")
|
|
1390
1375
|
),
|
|
@@ -1396,8 +1381,8 @@ function getComponentPropsFixes(node, typeArguments, fixer, context) {
|
|
|
1396
1381
|
arg0,
|
|
1397
1382
|
[
|
|
1398
1383
|
"{",
|
|
1399
|
-
|
|
1400
|
-
|
|
1384
|
+
fixedArg1Text + ",",
|
|
1385
|
+
fixedArg0Text,
|
|
1401
1386
|
"}:",
|
|
1402
1387
|
getText(typeArg1),
|
|
1403
1388
|
"&",
|
|
@@ -1428,17 +1413,17 @@ var no_implicit_key_default = createRule({
|
|
|
1428
1413
|
},
|
|
1429
1414
|
name: RULE_NAME26,
|
|
1430
1415
|
create(context) {
|
|
1431
|
-
function getReportDescriptor(node) {
|
|
1432
|
-
const initialScope = context.sourceCode.getScope(node);
|
|
1433
|
-
const keyPropFound = JSX7.findPropInAttributes(node.attributes, initialScope)("key");
|
|
1434
|
-
const keyPropOnElement = node.attributes.some((n) => AST11.is(AST_NODE_TYPES.JSXAttribute)(n) && n.name.name === "key");
|
|
1435
|
-
if (O.isSome(keyPropFound) && !keyPropOnElement) {
|
|
1436
|
-
return O.some({ messageId: "noImplicitKey", node: keyPropFound.value });
|
|
1437
|
-
}
|
|
1438
|
-
return O.none();
|
|
1439
|
-
}
|
|
1440
1416
|
return {
|
|
1441
|
-
JSXOpeningElement
|
|
1417
|
+
JSXOpeningElement(node) {
|
|
1418
|
+
const initialScope = context.sourceCode.getScope(node);
|
|
1419
|
+
const keyPropFound = JSX7.findPropInAttributes("key", initialScope, node.attributes);
|
|
1420
|
+
const keyPropOnElement = node.attributes.some(
|
|
1421
|
+
(n) => n.type === AST_NODE_TYPES.JSXAttribute && n.name.type === AST_NODE_TYPES.JSXIdentifier && n.name.name === "key"
|
|
1422
|
+
);
|
|
1423
|
+
if (keyPropFound && !keyPropOnElement) {
|
|
1424
|
+
context.report({ messageId: "noImplicitKey", node: keyPropFound });
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1442
1427
|
};
|
|
1443
1428
|
},
|
|
1444
1429
|
defaultOptions: []
|
|
@@ -1484,11 +1469,12 @@ function inspectVariantTypes(types) {
|
|
|
1484
1469
|
const booleans = types.filter(tsHelpers.isBooleanType);
|
|
1485
1470
|
switch (true) {
|
|
1486
1471
|
case (booleans.length === 1 && booleans[0] != null): {
|
|
1487
|
-
const
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
)
|
|
1491
|
-
|
|
1472
|
+
const first = booleans[0];
|
|
1473
|
+
if (isTrueLiteralType(first)) {
|
|
1474
|
+
variantTypes.add("truthy boolean");
|
|
1475
|
+
} else if (isFalseLiteralType(first)) {
|
|
1476
|
+
variantTypes.add("falsy boolean");
|
|
1477
|
+
}
|
|
1492
1478
|
break;
|
|
1493
1479
|
}
|
|
1494
1480
|
case booleans.length === 2: {
|
|
@@ -1498,17 +1484,17 @@ function inspectVariantTypes(types) {
|
|
|
1498
1484
|
}
|
|
1499
1485
|
const strings = types.filter(tsHelpers.isStringType);
|
|
1500
1486
|
if (strings.length > 0) {
|
|
1501
|
-
const evaluated = match(strings).when((types2) => types2.every(tsHelpers.isTruthyStringType),
|
|
1487
|
+
const evaluated = match(strings).when((types2) => types2.every(tsHelpers.isTruthyStringType), () => "truthy string").when((types2) => types2.every(tsHelpers.isFalsyStringType), () => "falsy string").otherwise(() => "string");
|
|
1502
1488
|
variantTypes.add(evaluated);
|
|
1503
1489
|
}
|
|
1504
1490
|
const bigints = types.filter(tsHelpers.isBigIntType);
|
|
1505
1491
|
if (bigints.length > 0) {
|
|
1506
|
-
const evaluated = match(bigints).when((types2) => types2.every(tsHelpers.isTruthyBigIntType),
|
|
1492
|
+
const evaluated = match(bigints).when((types2) => types2.every(tsHelpers.isTruthyBigIntType), () => "truthy bigint").when((types2) => types2.every(tsHelpers.isFalsyBigIntType), () => "falsy bigint").otherwise(() => "bigint");
|
|
1507
1493
|
variantTypes.add(evaluated);
|
|
1508
1494
|
}
|
|
1509
1495
|
const numbers = types.filter(tsHelpers.isNumberType);
|
|
1510
1496
|
if (numbers.length > 0) {
|
|
1511
|
-
const evaluated = match(numbers).when((types2) => types2.every(tsHelpers.isTruthyNumberType),
|
|
1497
|
+
const evaluated = match(numbers).when((types2) => types2.every(tsHelpers.isTruthyNumberType), () => "truthy number").when((types2) => types2.every(tsHelpers.isFalsyNumberType), () => "falsy number").otherwise(() => "number");
|
|
1512
1498
|
variantTypes.add(evaluated);
|
|
1513
1499
|
}
|
|
1514
1500
|
if (types.some(tsHelpers.isEnumType)) {
|
|
@@ -1526,19 +1512,15 @@ function inspectVariantTypes(types) {
|
|
|
1526
1512
|
return variantTypes;
|
|
1527
1513
|
}
|
|
1528
1514
|
function isInitExpression(node) {
|
|
1515
|
+
if (node == null) return false;
|
|
1529
1516
|
return node.type !== AST_NODE_TYPES.VariableDeclaration;
|
|
1530
1517
|
}
|
|
1531
|
-
function getVariableInitExpression(at) {
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
(d) => "init" in d.node ? O.fromNullable(d.node.init) : O.none()
|
|
1538
|
-
),
|
|
1539
|
-
O.filter(isInitExpression)
|
|
1540
|
-
);
|
|
1541
|
-
};
|
|
1518
|
+
function getVariableInitExpression(variable, at) {
|
|
1519
|
+
const def = variable?.defs[at];
|
|
1520
|
+
if (def?.node === _ || !("init" in def.node)) {
|
|
1521
|
+
return _;
|
|
1522
|
+
}
|
|
1523
|
+
return isInitExpression(def.node.init) ? def.node.init : _;
|
|
1542
1524
|
}
|
|
1543
1525
|
var no_leaked_conditional_rendering_default = createRule({
|
|
1544
1526
|
meta: {
|
|
@@ -1572,19 +1554,20 @@ var no_leaked_conditional_rendering_default = createRule({
|
|
|
1572
1554
|
];
|
|
1573
1555
|
const services = ESLintUtils.getParserServices(context, false);
|
|
1574
1556
|
function getReportDescriptor(node) {
|
|
1575
|
-
|
|
1557
|
+
if (node === _) return _;
|
|
1558
|
+
return match(node).when(AST11.isJSX, () => _).with({ type: AST_NODE_TYPES.LogicalExpression, operator: "&&" }, ({ left, right }) => {
|
|
1576
1559
|
const isLeftUnaryNot = left.type === AST_NODE_TYPES.UnaryExpression && left.operator === "!";
|
|
1577
1560
|
if (isLeftUnaryNot) {
|
|
1578
1561
|
return getReportDescriptor(right);
|
|
1579
1562
|
}
|
|
1580
1563
|
const initialScope = context.sourceCode.getScope(left);
|
|
1581
|
-
const isLeftNan = left.type === AST_NODE_TYPES.Identifier && left.name === "NaN" ||
|
|
1564
|
+
const isLeftNan = left.type === AST_NODE_TYPES.Identifier && left.name === "NaN" || VAR.toResolved({ kind: "lazy", node: left, initialScope }).value === "NaN";
|
|
1582
1565
|
if (isLeftNan) {
|
|
1583
|
-
return
|
|
1566
|
+
return {
|
|
1584
1567
|
messageId: "noLeakedConditionalRendering",
|
|
1585
1568
|
node: left,
|
|
1586
1569
|
data: { value: context.sourceCode.getText(left) }
|
|
1587
|
-
}
|
|
1570
|
+
};
|
|
1588
1571
|
}
|
|
1589
1572
|
const leftType = getConstrainedTypeAtLocation(services, left);
|
|
1590
1573
|
const leftTypeVariants = inspectVariantTypes(unionTypeParts(leftType));
|
|
@@ -1592,22 +1575,24 @@ var no_leaked_conditional_rendering_default = createRule({
|
|
|
1592
1575
|
if (isLeftValid) {
|
|
1593
1576
|
return getReportDescriptor(right);
|
|
1594
1577
|
}
|
|
1595
|
-
return
|
|
1578
|
+
return {
|
|
1596
1579
|
messageId: "noLeakedConditionalRendering",
|
|
1597
1580
|
node: left,
|
|
1598
1581
|
data: { value: context.sourceCode.getText(left) }
|
|
1599
|
-
}
|
|
1582
|
+
};
|
|
1600
1583
|
}).with({ type: AST_NODE_TYPES.ConditionalExpression }, ({ alternate, consequent }) => {
|
|
1601
|
-
return
|
|
1584
|
+
return getReportDescriptor(consequent) ?? getReportDescriptor(alternate);
|
|
1602
1585
|
}).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
);
|
|
1608
|
-
}).otherwise(O.none);
|
|
1586
|
+
const variable = VAR.findVariable(n.name, context.sourceCode.getScope(n));
|
|
1587
|
+
const initExpression = getVariableInitExpression(variable, 0);
|
|
1588
|
+
return getReportDescriptor(initExpression);
|
|
1589
|
+
}).otherwise(() => _);
|
|
1609
1590
|
}
|
|
1610
|
-
const visitorFunction =
|
|
1591
|
+
const visitorFunction = (node) => {
|
|
1592
|
+
const descriptor = getReportDescriptor(node);
|
|
1593
|
+
if (descriptor === _) return;
|
|
1594
|
+
context.report(descriptor);
|
|
1595
|
+
};
|
|
1611
1596
|
return {
|
|
1612
1597
|
"JSXExpressionContainer > ConditionalExpression": visitorFunction,
|
|
1613
1598
|
"JSXExpressionContainer > LogicalExpression": visitorFunction
|
|
@@ -1643,13 +1628,13 @@ var no_missing_component_display_name_default = createRule({
|
|
|
1643
1628
|
const components = ctx.getAllComponents(node);
|
|
1644
1629
|
for (const { node: node2, displayName, flag } of components.values()) {
|
|
1645
1630
|
const isMemoOrForwardRef = (flag & (ERFunctionComponentFlag.ForwardRef | ERFunctionComponentFlag.Memo)) > 0n;
|
|
1646
|
-
if (
|
|
1631
|
+
if (AST11.getFunctionIdentifier(node2)) {
|
|
1647
1632
|
continue;
|
|
1648
1633
|
}
|
|
1649
1634
|
if (!isMemoOrForwardRef) {
|
|
1650
1635
|
continue;
|
|
1651
1636
|
}
|
|
1652
|
-
if (
|
|
1637
|
+
if (!displayName) {
|
|
1653
1638
|
context.report({
|
|
1654
1639
|
messageId: "noMissingComponentDisplayName",
|
|
1655
1640
|
node: node2
|
|
@@ -1685,51 +1670,55 @@ var no_missing_key_default = createRule({
|
|
|
1685
1670
|
switch (node.type) {
|
|
1686
1671
|
case AST_NODE_TYPES.JSXElement: {
|
|
1687
1672
|
const initialScope = context.sourceCode.getScope(node);
|
|
1688
|
-
if (!JSX7.hasProp(
|
|
1689
|
-
return
|
|
1673
|
+
if (!JSX7.hasProp("key", initialScope, node.openingElement.attributes)) {
|
|
1674
|
+
return {
|
|
1690
1675
|
messageId: "noMissingKey",
|
|
1691
1676
|
node
|
|
1692
|
-
}
|
|
1677
|
+
};
|
|
1693
1678
|
}
|
|
1694
|
-
return
|
|
1679
|
+
return null;
|
|
1695
1680
|
}
|
|
1696
1681
|
case AST_NODE_TYPES.JSXFragment: {
|
|
1697
|
-
return
|
|
1682
|
+
return {
|
|
1698
1683
|
messageId: "noMissingKeyWithFragment",
|
|
1699
1684
|
node
|
|
1700
|
-
}
|
|
1685
|
+
};
|
|
1701
1686
|
}
|
|
1702
1687
|
default:
|
|
1703
|
-
return
|
|
1688
|
+
return null;
|
|
1704
1689
|
}
|
|
1705
1690
|
}
|
|
1706
1691
|
function checkExpression(node) {
|
|
1707
1692
|
switch (node.type) {
|
|
1708
1693
|
case AST_NODE_TYPES.ConditionalExpression:
|
|
1709
|
-
if (
|
|
1710
|
-
return
|
|
1694
|
+
if ("consequent" in node) {
|
|
1695
|
+
return checkIteratorElement(node.consequent) ?? checkIteratorElement(node.alternate);
|
|
1711
1696
|
}
|
|
1712
|
-
return
|
|
1697
|
+
return null;
|
|
1713
1698
|
case AST_NODE_TYPES.JSXElement:
|
|
1714
1699
|
case AST_NODE_TYPES.JSXFragment:
|
|
1715
1700
|
return checkIteratorElement(node);
|
|
1716
1701
|
case AST_NODE_TYPES.LogicalExpression:
|
|
1717
|
-
if (
|
|
1718
|
-
return
|
|
1702
|
+
if ("left" in node) {
|
|
1703
|
+
return checkIteratorElement(node.left) ?? checkIteratorElement(node.right);
|
|
1719
1704
|
}
|
|
1720
|
-
return
|
|
1705
|
+
return null;
|
|
1721
1706
|
default:
|
|
1722
|
-
return
|
|
1707
|
+
return null;
|
|
1723
1708
|
}
|
|
1724
1709
|
}
|
|
1725
1710
|
function checkBlockStatement(node) {
|
|
1726
|
-
|
|
1711
|
+
const descriptors = [];
|
|
1712
|
+
for (const statement of AST11.getNestedReturnStatements(node)) {
|
|
1727
1713
|
if (statement.argument == null) {
|
|
1728
|
-
|
|
1714
|
+
continue;
|
|
1715
|
+
}
|
|
1716
|
+
const descriptor = checkIteratorElement(statement.argument);
|
|
1717
|
+
if (descriptor != null) {
|
|
1718
|
+
descriptors.push(descriptor);
|
|
1729
1719
|
}
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
}, []);
|
|
1720
|
+
}
|
|
1721
|
+
return descriptors;
|
|
1733
1722
|
}
|
|
1734
1723
|
return {
|
|
1735
1724
|
ArrayExpression(node) {
|
|
@@ -1742,7 +1731,7 @@ var no_missing_key_default = createRule({
|
|
|
1742
1731
|
}
|
|
1743
1732
|
const initialScope = context.sourceCode.getScope(node);
|
|
1744
1733
|
for (const element of elements) {
|
|
1745
|
-
if (!JSX7.hasProp(
|
|
1734
|
+
if (!JSX7.hasProp("key", initialScope, element.openingElement.attributes)) {
|
|
1746
1735
|
context.report({
|
|
1747
1736
|
messageId: "noMissingKey",
|
|
1748
1737
|
node: element
|
|
@@ -1755,30 +1744,24 @@ var no_missing_key_default = createRule({
|
|
|
1755
1744
|
if (state.isWithinChildrenToArray) {
|
|
1756
1745
|
return;
|
|
1757
1746
|
}
|
|
1758
|
-
const
|
|
1759
|
-
|
|
1760
|
-
type: AST_NODE_TYPES.CallExpression,
|
|
1761
|
-
callee: {
|
|
1762
|
-
type: AST_NODE_TYPES.MemberExpression,
|
|
1763
|
-
property: {
|
|
1764
|
-
name: "from"
|
|
1765
|
-
}
|
|
1766
|
-
}
|
|
1767
|
-
}, node);
|
|
1768
|
-
if (!isMapCall && !isArrayFromCall) {
|
|
1747
|
+
const isMapCallLike = AST11.isMapCallLoose(node);
|
|
1748
|
+
if (!isMapCallLike && !isArrayFromCall(node)) {
|
|
1769
1749
|
return;
|
|
1770
1750
|
}
|
|
1771
|
-
const fn = node.arguments[
|
|
1772
|
-
if (
|
|
1751
|
+
const fn = node.arguments[isMapCallLike ? 0 : 1];
|
|
1752
|
+
if (fn?.type !== AST_NODE_TYPES.ArrowFunctionExpression && fn?.type !== AST_NODE_TYPES.FunctionExpression) {
|
|
1773
1753
|
return;
|
|
1774
1754
|
}
|
|
1775
1755
|
if (fn.body.type === AST_NODE_TYPES.BlockStatement) {
|
|
1776
|
-
for (const
|
|
1777
|
-
context.report(
|
|
1756
|
+
for (const descriptor2 of checkBlockStatement(fn.body)) {
|
|
1757
|
+
context.report(descriptor2);
|
|
1778
1758
|
}
|
|
1779
1759
|
return;
|
|
1780
1760
|
}
|
|
1781
|
-
|
|
1761
|
+
const descriptor = checkExpression(fn.body);
|
|
1762
|
+
if (descriptor != null) {
|
|
1763
|
+
context.report(descriptor);
|
|
1764
|
+
}
|
|
1782
1765
|
},
|
|
1783
1766
|
"CallExpression:exit"(node) {
|
|
1784
1767
|
if (!isChildrenToArrayCall(node, context)) {
|
|
@@ -1801,6 +1784,9 @@ var no_missing_key_default = createRule({
|
|
|
1801
1784
|
},
|
|
1802
1785
|
defaultOptions: []
|
|
1803
1786
|
});
|
|
1787
|
+
function isArrayFromCall(node) {
|
|
1788
|
+
return node.type === AST_NODE_TYPES.CallExpression && node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === "from";
|
|
1789
|
+
}
|
|
1804
1790
|
var RULE_NAME30 = "no-nested-components";
|
|
1805
1791
|
var RULE_FEATURES30 = [
|
|
1806
1792
|
"CHK"
|
|
@@ -1827,24 +1813,27 @@ var no_nested_components_default = createRule({
|
|
|
1827
1813
|
...collector.listeners,
|
|
1828
1814
|
...collectorLegacy.listeners,
|
|
1829
1815
|
"Program:exit"(node) {
|
|
1830
|
-
const functionComponents =
|
|
1831
|
-
|
|
1816
|
+
const functionComponents = [
|
|
1817
|
+
...collector.ctx.getAllComponents(node).values()
|
|
1818
|
+
];
|
|
1819
|
+
const classComponents = [
|
|
1820
|
+
...collectorLegacy.ctx.getAllComponents(node).values()
|
|
1821
|
+
];
|
|
1832
1822
|
const isFunctionComponent = (node2) => {
|
|
1833
1823
|
return AST11.isFunction(node2) && functionComponents.some((component) => component.node === node2);
|
|
1834
1824
|
};
|
|
1835
1825
|
const isClassComponent11 = (node2) => {
|
|
1836
1826
|
return AST11.isClass(node2) && classComponents.some((component) => component.node === node2);
|
|
1837
1827
|
};
|
|
1838
|
-
for (const { name:
|
|
1828
|
+
for (const { name: name2, node: component } of functionComponents) {
|
|
1839
1829
|
if (isDirectValueOfRenderPropertyLoose(component)) {
|
|
1840
1830
|
continue;
|
|
1841
1831
|
}
|
|
1842
|
-
if (
|
|
1832
|
+
if (name2 === _) {
|
|
1843
1833
|
continue;
|
|
1844
1834
|
}
|
|
1845
|
-
const name2 = componentName.value;
|
|
1846
1835
|
const isInsideProperty = component.parent.type === AST_NODE_TYPES.Property;
|
|
1847
|
-
const isInsideJSXPropValue = component.parent.type === AST_NODE_TYPES.JSXAttribute ||
|
|
1836
|
+
const isInsideJSXPropValue = component.parent.type === AST_NODE_TYPES.JSXAttribute || !!JSX7.findParentProp(node, (n) => n.value?.type === AST_NODE_TYPES.JSXExpressionContainer);
|
|
1848
1837
|
if (isInsideJSXPropValue) {
|
|
1849
1838
|
if (!isDeclaredInRenderPropLoose(component)) {
|
|
1850
1839
|
context.report({
|
|
@@ -1867,13 +1856,8 @@ var no_nested_components_default = createRule({
|
|
|
1867
1856
|
});
|
|
1868
1857
|
continue;
|
|
1869
1858
|
}
|
|
1870
|
-
const
|
|
1871
|
-
|
|
1872
|
-
component,
|
|
1873
|
-
isFunctionComponent
|
|
1874
|
-
),
|
|
1875
|
-
not(isDirectValueOfRenderPropertyLoose)
|
|
1876
|
-
);
|
|
1859
|
+
const parentComponent = AST11.findParentNodeGuard(component, isFunctionComponent);
|
|
1860
|
+
const isParnetComponentNotDirectValueOfRenderProperty = !!parentComponent && !isDirectValueOfRenderPropertyLoose(parentComponent);
|
|
1877
1861
|
if (isParnetComponentNotDirectValueOfRenderProperty) {
|
|
1878
1862
|
context.report({
|
|
1879
1863
|
messageId: isInsideProperty ? "nestedComponentInProps" : "nestedComponent",
|
|
@@ -1894,15 +1878,15 @@ var no_nested_components_default = createRule({
|
|
|
1894
1878
|
});
|
|
1895
1879
|
}
|
|
1896
1880
|
}
|
|
1897
|
-
for (const { name: name2, node: component } of classComponents) {
|
|
1898
|
-
if (
|
|
1881
|
+
for (const { name: name2 = "unknown", node: component } of classComponents) {
|
|
1882
|
+
if (!AST11.findParentNode(component, (n) => isClassComponent11(n) || isFunctionComponent(n))) {
|
|
1899
1883
|
continue;
|
|
1900
1884
|
}
|
|
1901
1885
|
context.report({
|
|
1902
1886
|
messageId: "nestedComponent",
|
|
1903
1887
|
node: component,
|
|
1904
1888
|
data: {
|
|
1905
|
-
name:
|
|
1889
|
+
name: name2
|
|
1906
1890
|
}
|
|
1907
1891
|
});
|
|
1908
1892
|
}
|
|
@@ -1947,15 +1931,11 @@ var no_prop_types_default = createRule({
|
|
|
1947
1931
|
if (!isComponentName(object.name)) {
|
|
1948
1932
|
return;
|
|
1949
1933
|
}
|
|
1950
|
-
const
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
);
|
|
1955
|
-
if (!isComponent) {
|
|
1956
|
-
return;
|
|
1934
|
+
const variable = VAR.findVariable(object.name, context.sourceCode.getScope(node));
|
|
1935
|
+
const variableNode = variable && VAR.getVariableNode(variable, 0);
|
|
1936
|
+
if (variableNode && (AST11.isFunction(variableNode) || isClassComponent(variableNode))) {
|
|
1937
|
+
context.report({ messageId: "noPropTypes", node: property });
|
|
1957
1938
|
}
|
|
1958
|
-
context.report({ messageId: "noPropTypes", node: property });
|
|
1959
1939
|
},
|
|
1960
1940
|
PropertyDefinition(node) {
|
|
1961
1941
|
if (!isClassComponent(node.parent.parent)) {
|
|
@@ -1999,7 +1979,7 @@ var no_redundant_should_component_update_default = createRule({
|
|
|
1999
1979
|
...listeners,
|
|
2000
1980
|
"Program:exit"(node) {
|
|
2001
1981
|
const components = ctx.getAllComponents(node);
|
|
2002
|
-
for (const { name: name2, node: component, flag } of components.values()) {
|
|
1982
|
+
for (const { name: name2 = "PureComponent", node: component, flag } of components.values()) {
|
|
2003
1983
|
if ((flag & ERClassComponentFlag.PureComponent) === 0n) {
|
|
2004
1984
|
continue;
|
|
2005
1985
|
}
|
|
@@ -2010,7 +1990,7 @@ var no_redundant_should_component_update_default = createRule({
|
|
|
2010
1990
|
messageId: "noRedundantShouldComponentUpdate",
|
|
2011
1991
|
node: member,
|
|
2012
1992
|
data: {
|
|
2013
|
-
componentName:
|
|
1993
|
+
componentName: name2
|
|
2014
1994
|
}
|
|
2015
1995
|
});
|
|
2016
1996
|
}
|
|
@@ -2045,32 +2025,22 @@ var no_set_state_in_component_did_mount_default = createRule({
|
|
|
2045
2025
|
if (!context.sourceCode.text.includes("componentDidMount")) {
|
|
2046
2026
|
return {};
|
|
2047
2027
|
}
|
|
2048
|
-
function getReportDescriptor(node) {
|
|
2049
|
-
if (!isThisSetState(node)) {
|
|
2050
|
-
return O.none();
|
|
2051
|
-
}
|
|
2052
|
-
return F.pipe(
|
|
2053
|
-
O.Do,
|
|
2054
|
-
O.bind("clazz", () => AST11.findParentNodeGuard(node, isClassComponent)),
|
|
2055
|
-
O.bind("method", ({ clazz }) => AST11.findParentNodeStop(node, clazz, isComponentDidMount)),
|
|
2056
|
-
O.bind("methodScope", ({ method }) => O.some(context.sourceCode.getScope(method))),
|
|
2057
|
-
O.bind("upperScope", () => O.fromNullable(context.sourceCode.getScope(node).upper)),
|
|
2058
|
-
O.filter(
|
|
2059
|
-
({
|
|
2060
|
-
clazz,
|
|
2061
|
-
method,
|
|
2062
|
-
methodScope,
|
|
2063
|
-
upperScope
|
|
2064
|
-
}) => method.parent === clazz.body && upperScope === methodScope
|
|
2065
|
-
),
|
|
2066
|
-
O.map(() => ({
|
|
2067
|
-
messageId: "noSetStateInComponentDidMount",
|
|
2068
|
-
node
|
|
2069
|
-
}))
|
|
2070
|
-
);
|
|
2071
|
-
}
|
|
2072
2028
|
return {
|
|
2073
|
-
CallExpression
|
|
2029
|
+
CallExpression(node) {
|
|
2030
|
+
if (!isThisSetState(node)) {
|
|
2031
|
+
return;
|
|
2032
|
+
}
|
|
2033
|
+
const clazz = AST11.findParentNodeGuard(node, isClassComponent);
|
|
2034
|
+
const method = clazz && AST11.findParentNodeStop(node, clazz, isComponentDidMount);
|
|
2035
|
+
const methodScope = method && context.sourceCode.getScope(method);
|
|
2036
|
+
const upperScope = context.sourceCode.getScope(node).upper;
|
|
2037
|
+
if (clazz && method && method.parent === clazz.body && upperScope === methodScope) {
|
|
2038
|
+
context.report({
|
|
2039
|
+
messageId: "noSetStateInComponentDidMount",
|
|
2040
|
+
node
|
|
2041
|
+
});
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2074
2044
|
};
|
|
2075
2045
|
},
|
|
2076
2046
|
defaultOptions: []
|
|
@@ -2099,32 +2069,22 @@ var no_set_state_in_component_did_update_default = createRule({
|
|
|
2099
2069
|
if (!context.sourceCode.text.includes("componentDidUpdate")) {
|
|
2100
2070
|
return {};
|
|
2101
2071
|
}
|
|
2102
|
-
function getReportDescriptor(node) {
|
|
2103
|
-
if (!isThisSetState(node)) {
|
|
2104
|
-
return O.none();
|
|
2105
|
-
}
|
|
2106
|
-
return F.pipe(
|
|
2107
|
-
O.Do,
|
|
2108
|
-
O.bind("clazz", () => AST11.findParentNodeGuard(node, isClassComponent)),
|
|
2109
|
-
O.bind("method", ({ clazz }) => AST11.findParentNodeStop(node, clazz, isComponentDidUpdate)),
|
|
2110
|
-
O.bind("methodScope", ({ method }) => O.some(context.sourceCode.getScope(method))),
|
|
2111
|
-
O.bind("upperScope", () => O.fromNullable(context.sourceCode.getScope(node).upper)),
|
|
2112
|
-
O.filter(
|
|
2113
|
-
({
|
|
2114
|
-
clazz,
|
|
2115
|
-
method,
|
|
2116
|
-
methodScope,
|
|
2117
|
-
upperScope
|
|
2118
|
-
}) => method.parent === clazz.body && upperScope === methodScope
|
|
2119
|
-
),
|
|
2120
|
-
O.map(() => ({
|
|
2121
|
-
messageId: "noSetStateInComponentDidUpdate",
|
|
2122
|
-
node
|
|
2123
|
-
}))
|
|
2124
|
-
);
|
|
2125
|
-
}
|
|
2126
2072
|
return {
|
|
2127
|
-
CallExpression
|
|
2073
|
+
CallExpression(node) {
|
|
2074
|
+
if (!isThisSetState(node)) {
|
|
2075
|
+
return;
|
|
2076
|
+
}
|
|
2077
|
+
const clazz = AST11.findParentNodeGuard(node, isClassComponent);
|
|
2078
|
+
const method = clazz && AST11.findParentNodeStop(node, clazz, isComponentDidUpdate);
|
|
2079
|
+
const methodScope = method && context.sourceCode.getScope(method);
|
|
2080
|
+
const upperScope = context.sourceCode.getScope(node).upper;
|
|
2081
|
+
if (clazz && method && method.parent === clazz.body && upperScope === methodScope) {
|
|
2082
|
+
context.report({
|
|
2083
|
+
messageId: "noSetStateInComponentDidUpdate",
|
|
2084
|
+
node
|
|
2085
|
+
});
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2128
2088
|
};
|
|
2129
2089
|
},
|
|
2130
2090
|
defaultOptions: []
|
|
@@ -2153,32 +2113,22 @@ var no_set_state_in_component_will_update_default = createRule({
|
|
|
2153
2113
|
if (!context.sourceCode.text.includes("componentWillUpdate")) {
|
|
2154
2114
|
return {};
|
|
2155
2115
|
}
|
|
2156
|
-
function getReportDescriptor(node) {
|
|
2157
|
-
if (!isThisSetState(node)) {
|
|
2158
|
-
return O.none();
|
|
2159
|
-
}
|
|
2160
|
-
return F.pipe(
|
|
2161
|
-
O.Do,
|
|
2162
|
-
O.bind("clazz", () => AST11.findParentNodeGuard(node, isClassComponent)),
|
|
2163
|
-
O.bind("method", ({ clazz }) => AST11.findParentNodeStop(node, clazz, isComponentWillUpdate3)),
|
|
2164
|
-
O.bind("methodScope", ({ method }) => O.some(context.sourceCode.getScope(method))),
|
|
2165
|
-
O.bind("upperScope", () => O.fromNullable(context.sourceCode.getScope(node).upper)),
|
|
2166
|
-
O.filter(
|
|
2167
|
-
({
|
|
2168
|
-
clazz,
|
|
2169
|
-
method,
|
|
2170
|
-
methodScope,
|
|
2171
|
-
upperScope
|
|
2172
|
-
}) => method.parent === clazz.body && upperScope === methodScope
|
|
2173
|
-
),
|
|
2174
|
-
O.map(() => ({
|
|
2175
|
-
messageId: "noSetStateInComponentWillUpdate",
|
|
2176
|
-
node
|
|
2177
|
-
}))
|
|
2178
|
-
);
|
|
2179
|
-
}
|
|
2180
2116
|
return {
|
|
2181
|
-
CallExpression
|
|
2117
|
+
CallExpression(node) {
|
|
2118
|
+
if (!isThisSetState(node)) {
|
|
2119
|
+
return;
|
|
2120
|
+
}
|
|
2121
|
+
const clazz = AST11.findParentNodeGuard(node, isClassComponent);
|
|
2122
|
+
const method = clazz && AST11.findParentNodeStop(node, clazz, isComponentWillUpdate3);
|
|
2123
|
+
const methodScope = method && context.sourceCode.getScope(method);
|
|
2124
|
+
const upperScope = context.sourceCode.getScope(node).upper;
|
|
2125
|
+
if (clazz && method && method.parent === clazz.body && upperScope === methodScope) {
|
|
2126
|
+
context.report({
|
|
2127
|
+
messageId: "noSetStateInComponentWillUpdate",
|
|
2128
|
+
node
|
|
2129
|
+
});
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2182
2132
|
};
|
|
2183
2133
|
},
|
|
2184
2134
|
defaultOptions: []
|
|
@@ -2188,14 +2138,14 @@ var RULE_FEATURES36 = [
|
|
|
2188
2138
|
"CHK"
|
|
2189
2139
|
];
|
|
2190
2140
|
function containsStringLiteral({ value }) {
|
|
2191
|
-
return value?.type === AST_NODE_TYPES.Literal &&
|
|
2141
|
+
return value?.type === AST_NODE_TYPES.Literal && typeof value.value === "string";
|
|
2192
2142
|
}
|
|
2193
2143
|
function containsStringExpressionContainer({ value }) {
|
|
2194
2144
|
if (value?.type !== AST_NODE_TYPES.JSXExpressionContainer) {
|
|
2195
2145
|
return false;
|
|
2196
2146
|
}
|
|
2197
2147
|
if (value.expression.type === AST_NODE_TYPES.Literal) {
|
|
2198
|
-
return
|
|
2148
|
+
return typeof value.expression.value === "string";
|
|
2199
2149
|
}
|
|
2200
2150
|
return value.expression.type === AST_NODE_TYPES.TemplateLiteral;
|
|
2201
2151
|
}
|
|
@@ -2396,39 +2346,28 @@ var no_unstable_context_value_default = createRule({
|
|
|
2396
2346
|
if (openingElementName.property.name !== "Provider") {
|
|
2397
2347
|
return;
|
|
2398
2348
|
}
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
node.attributes.find((attribute) => {
|
|
2404
|
-
return attribute.type === AST_NODE_TYPES.JSXAttribute && attribute.name.name === "value";
|
|
2405
|
-
})
|
|
2406
|
-
)),
|
|
2407
|
-
O.bind("value", ({ attribute }) => "value" in attribute ? O.some(attribute.value) : O.none()),
|
|
2408
|
-
O.bind("valueExpression", ({ value }) => value?.type === AST_NODE_TYPES.JSXExpressionContainer ? O.some(value.expression) : O.none()),
|
|
2409
|
-
O.bind("construction", ({ valueExpression }) => {
|
|
2410
|
-
const initialScope = context.sourceCode.getScope(valueExpression);
|
|
2411
|
-
return O.some(VAR.getValueConstruction(valueExpression, initialScope));
|
|
2412
|
-
}),
|
|
2413
|
-
O.map((vc) => {
|
|
2414
|
-
if (vc.construction.kind === "None") {
|
|
2415
|
-
return;
|
|
2416
|
-
}
|
|
2417
|
-
if (isReactHookCall(vc.construction.node)) {
|
|
2418
|
-
return;
|
|
2419
|
-
}
|
|
2420
|
-
const prevs = constructions.get(vc.function.node) ?? [];
|
|
2421
|
-
constructions.set(vc.function.node, [...prevs, vc.construction]);
|
|
2422
|
-
})
|
|
2349
|
+
const functionEntry = ctx.getCurrentEntry();
|
|
2350
|
+
if (functionEntry === _) return;
|
|
2351
|
+
const attribute = node.attributes.find(
|
|
2352
|
+
(attribute2) => attribute2.type === AST_NODE_TYPES.JSXAttribute && attribute2.name.name === "value"
|
|
2423
2353
|
);
|
|
2354
|
+
if (attribute === _ || !("value" in attribute)) return;
|
|
2355
|
+
const value = attribute.value;
|
|
2356
|
+
if (value?.type !== AST_NODE_TYPES.JSXExpressionContainer) return;
|
|
2357
|
+
const valueExpression = value.expression;
|
|
2358
|
+
const initialScope = context.sourceCode.getScope(valueExpression);
|
|
2359
|
+
const construction = VAR.getValueConstruction(valueExpression, initialScope);
|
|
2360
|
+
if (construction === _) return;
|
|
2361
|
+
if (isReactHookCall(construction.node)) {
|
|
2362
|
+
return;
|
|
2363
|
+
}
|
|
2364
|
+
const prevs = constructions.get(functionEntry.node) ?? [];
|
|
2365
|
+
constructions.set(functionEntry.node, [...prevs, construction]);
|
|
2424
2366
|
},
|
|
2425
2367
|
"Program:exit"(node) {
|
|
2426
2368
|
const components = ctx.getAllComponents(node).values();
|
|
2427
2369
|
for (const { node: component } of components) {
|
|
2428
2370
|
for (const construction of constructions.get(component) ?? []) {
|
|
2429
|
-
if (construction.kind === "None") {
|
|
2430
|
-
continue;
|
|
2431
|
-
}
|
|
2432
2371
|
const { kind, node: constructionNode } = construction;
|
|
2433
2372
|
const messageId = kind.startsWith("Function") ? "noUnstableContextValueWithFunction" : "noUnstableContextValueWithIdentifier";
|
|
2434
2373
|
context.report({
|
|
@@ -2490,7 +2429,7 @@ var no_unstable_default_props_default = createRule({
|
|
|
2490
2429
|
initialScope,
|
|
2491
2430
|
VAR.ValueConstructionHint.StrictCallExpression
|
|
2492
2431
|
);
|
|
2493
|
-
if (construction
|
|
2432
|
+
if (construction === _) {
|
|
2494
2433
|
continue;
|
|
2495
2434
|
}
|
|
2496
2435
|
if (isReactHookCall(construction.node)) {
|
|
@@ -2508,13 +2447,10 @@ var no_unstable_default_props_default = createRule({
|
|
|
2508
2447
|
}
|
|
2509
2448
|
},
|
|
2510
2449
|
"VariableDeclarator[id.type='ObjectPattern'][init.type='Identifier']"(node) {
|
|
2511
|
-
const
|
|
2512
|
-
if (
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
const entry = mbEntry.value;
|
|
2516
|
-
const prevs = declarators.get(entry.node) ?? [];
|
|
2517
|
-
declarators.set(entry.node, [...prevs, node]);
|
|
2450
|
+
const functionEntry = ctx.getCurrentEntry();
|
|
2451
|
+
if (!functionEntry) return;
|
|
2452
|
+
const prevs = declarators.get(functionEntry.node) ?? [];
|
|
2453
|
+
declarators.set(functionEntry.node, [...prevs, node]);
|
|
2518
2454
|
}
|
|
2519
2455
|
};
|
|
2520
2456
|
},
|
|
@@ -2546,15 +2482,15 @@ function getName3(node) {
|
|
|
2546
2482
|
return getName3(node.expression);
|
|
2547
2483
|
}
|
|
2548
2484
|
if (node.type === AST_NODE_TYPES.Identifier || node.type === AST_NODE_TYPES.PrivateIdentifier) {
|
|
2549
|
-
return
|
|
2485
|
+
return node.name;
|
|
2550
2486
|
}
|
|
2551
2487
|
if (node.type === AST_NODE_TYPES.Literal) {
|
|
2552
|
-
return
|
|
2488
|
+
return node.value?.toString();
|
|
2553
2489
|
}
|
|
2554
2490
|
if (node.type === AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0) {
|
|
2555
|
-
return
|
|
2491
|
+
return node.quasis[0]?.value.raw;
|
|
2556
2492
|
}
|
|
2557
|
-
return
|
|
2493
|
+
return _;
|
|
2558
2494
|
}
|
|
2559
2495
|
var no_unused_class_component_members_default = createRule({
|
|
2560
2496
|
meta: {
|
|
@@ -2570,12 +2506,12 @@ var no_unused_class_component_members_default = createRule({
|
|
|
2570
2506
|
},
|
|
2571
2507
|
name: RULE_NAME42,
|
|
2572
2508
|
create(context) {
|
|
2573
|
-
const
|
|
2574
|
-
const
|
|
2509
|
+
const classEntries = [];
|
|
2510
|
+
const methodEntries = [];
|
|
2575
2511
|
const propertyDefs = /* @__PURE__ */ new WeakMap();
|
|
2576
2512
|
const propertyUsages = /* @__PURE__ */ new WeakMap();
|
|
2577
2513
|
function classEnter(node) {
|
|
2578
|
-
|
|
2514
|
+
classEntries.push(node);
|
|
2579
2515
|
if (!isClassComponent(node)) {
|
|
2580
2516
|
return;
|
|
2581
2517
|
}
|
|
@@ -2583,37 +2519,37 @@ var no_unused_class_component_members_default = createRule({
|
|
|
2583
2519
|
propertyUsages.set(node, /* @__PURE__ */ new Set());
|
|
2584
2520
|
}
|
|
2585
2521
|
function classExit() {
|
|
2586
|
-
const currentClass =
|
|
2522
|
+
const currentClass = classEntries.pop();
|
|
2587
2523
|
if (currentClass == null || !isClassComponent(currentClass)) {
|
|
2588
2524
|
return;
|
|
2589
2525
|
}
|
|
2590
|
-
const className =
|
|
2526
|
+
const className = AST11.getClassIdentifier(currentClass)?.name;
|
|
2591
2527
|
const defs = propertyDefs.get(currentClass);
|
|
2592
2528
|
const usages = propertyUsages.get(currentClass);
|
|
2593
2529
|
if (defs == null) {
|
|
2594
2530
|
return;
|
|
2595
2531
|
}
|
|
2596
2532
|
for (const def of defs) {
|
|
2597
|
-
const
|
|
2598
|
-
if (
|
|
2533
|
+
const methodName = getName3(def);
|
|
2534
|
+
if (methodName == null) {
|
|
2599
2535
|
continue;
|
|
2600
2536
|
}
|
|
2601
|
-
if (usages?.has(
|
|
2537
|
+
if (usages?.has(methodName) || LIFECYCLE_METHODS.has(methodName)) {
|
|
2602
2538
|
continue;
|
|
2603
2539
|
}
|
|
2604
2540
|
context.report({
|
|
2605
2541
|
messageId: "noUnusedClassComponentMembers",
|
|
2606
2542
|
node: def,
|
|
2607
2543
|
data: {
|
|
2608
|
-
className:
|
|
2609
|
-
methodName
|
|
2544
|
+
className: className ?? "Component",
|
|
2545
|
+
methodName
|
|
2610
2546
|
}
|
|
2611
2547
|
});
|
|
2612
2548
|
}
|
|
2613
2549
|
}
|
|
2614
2550
|
function methodEnter(node) {
|
|
2615
|
-
|
|
2616
|
-
const currentClass =
|
|
2551
|
+
methodEntries.push(node);
|
|
2552
|
+
const currentClass = classEntries.at(-1);
|
|
2617
2553
|
if (currentClass == null || !isClassComponent(currentClass)) {
|
|
2618
2554
|
return;
|
|
2619
2555
|
}
|
|
@@ -2625,7 +2561,7 @@ var no_unused_class_component_members_default = createRule({
|
|
|
2625
2561
|
}
|
|
2626
2562
|
}
|
|
2627
2563
|
function methodExit() {
|
|
2628
|
-
|
|
2564
|
+
methodEntries.pop();
|
|
2629
2565
|
}
|
|
2630
2566
|
return {
|
|
2631
2567
|
ClassDeclaration: classEnter,
|
|
@@ -2633,8 +2569,8 @@ var no_unused_class_component_members_default = createRule({
|
|
|
2633
2569
|
ClassExpression: classEnter,
|
|
2634
2570
|
"ClassExpression:exit": classExit,
|
|
2635
2571
|
MemberExpression(node) {
|
|
2636
|
-
const currentClass =
|
|
2637
|
-
const currentMethod =
|
|
2572
|
+
const currentClass = classEntries.at(-1);
|
|
2573
|
+
const currentMethod = methodEntries.at(-1);
|
|
2638
2574
|
if (currentClass == null || currentMethod == null) {
|
|
2639
2575
|
return;
|
|
2640
2576
|
}
|
|
@@ -2648,15 +2584,18 @@ var no_unused_class_component_members_default = createRule({
|
|
|
2648
2584
|
propertyDefs.get(currentClass)?.add(node.property);
|
|
2649
2585
|
return;
|
|
2650
2586
|
}
|
|
2651
|
-
|
|
2587
|
+
const propertyName = getName3(node.property);
|
|
2588
|
+
if (propertyName != null) {
|
|
2589
|
+
propertyUsages.get(currentClass)?.add(propertyName);
|
|
2590
|
+
}
|
|
2652
2591
|
},
|
|
2653
2592
|
MethodDefinition: methodEnter,
|
|
2654
2593
|
"MethodDefinition:exit": methodExit,
|
|
2655
2594
|
PropertyDefinition: methodEnter,
|
|
2656
2595
|
"PropertyDefinition:exit": methodExit,
|
|
2657
2596
|
VariableDeclarator(node) {
|
|
2658
|
-
const currentClass =
|
|
2659
|
-
const currentMethod =
|
|
2597
|
+
const currentClass = classEntries.at(-1);
|
|
2598
|
+
const currentMethod = methodEntries.at(-1);
|
|
2660
2599
|
if (currentClass == null || currentMethod == null) {
|
|
2661
2600
|
return;
|
|
2662
2601
|
}
|
|
@@ -2666,10 +2605,9 @@ var no_unused_class_component_members_default = createRule({
|
|
|
2666
2605
|
if (node.init != null && AST11.isThisExpression(node.init) && node.id.type === AST_NODE_TYPES.ObjectPattern) {
|
|
2667
2606
|
for (const prop of node.id.properties) {
|
|
2668
2607
|
if (prop.type === AST_NODE_TYPES.Property && AST11.isKeyLiteralLike(prop, prop.key)) {
|
|
2669
|
-
const
|
|
2670
|
-
if (
|
|
2671
|
-
|
|
2672
|
-
propertyUsages.get(currentClass)?.add(name2);
|
|
2608
|
+
const keyName = getName3(prop.key);
|
|
2609
|
+
if (keyName != null) {
|
|
2610
|
+
propertyUsages.get(currentClass)?.add(keyName);
|
|
2673
2611
|
}
|
|
2674
2612
|
}
|
|
2675
2613
|
}
|
|
@@ -2688,19 +2626,19 @@ function getName4(node) {
|
|
|
2688
2626
|
return getName4(node.expression);
|
|
2689
2627
|
}
|
|
2690
2628
|
if (node.type === AST_NODE_TYPES.Identifier || node.type === AST_NODE_TYPES.PrivateIdentifier) {
|
|
2691
|
-
return
|
|
2629
|
+
return node.name;
|
|
2692
2630
|
}
|
|
2693
2631
|
if (node.type === AST_NODE_TYPES.Literal) {
|
|
2694
|
-
return
|
|
2632
|
+
return String(node.value);
|
|
2695
2633
|
}
|
|
2696
2634
|
if (node.type === AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0) {
|
|
2697
|
-
return
|
|
2635
|
+
return node.quasis[0]?.value.raw;
|
|
2698
2636
|
}
|
|
2699
|
-
return
|
|
2637
|
+
return _;
|
|
2700
2638
|
}
|
|
2701
2639
|
function isAssignmentToThisState2(node) {
|
|
2702
2640
|
const { left } = node;
|
|
2703
|
-
return left.type === AST_NODE_TYPES.MemberExpression && AST11.isThisExpression(left.object) &&
|
|
2641
|
+
return left.type === AST_NODE_TYPES.MemberExpression && AST11.isThisExpression(left.object) && getName4(left.property) === "state";
|
|
2704
2642
|
}
|
|
2705
2643
|
var isGetDerivedStateFromProps = isMatching({
|
|
2706
2644
|
key: {
|
|
@@ -2725,72 +2663,72 @@ var no_unused_state_default = createRule({
|
|
|
2725
2663
|
},
|
|
2726
2664
|
name: RULE_NAME43,
|
|
2727
2665
|
create(context) {
|
|
2728
|
-
const
|
|
2729
|
-
const
|
|
2730
|
-
const
|
|
2666
|
+
const classEntries = [];
|
|
2667
|
+
const methodEntries = [];
|
|
2668
|
+
const constructorEntries = [];
|
|
2731
2669
|
const stateDefs = /* @__PURE__ */ new WeakMap();
|
|
2732
2670
|
function classEnter(node) {
|
|
2733
|
-
|
|
2671
|
+
classEntries.push(node);
|
|
2734
2672
|
}
|
|
2735
2673
|
function classExit() {
|
|
2736
|
-
const currentClass =
|
|
2674
|
+
const currentClass = classEntries.pop();
|
|
2737
2675
|
if (currentClass == null || !isClassComponent(currentClass)) {
|
|
2738
2676
|
return;
|
|
2739
2677
|
}
|
|
2740
|
-
const className =
|
|
2741
|
-
const
|
|
2742
|
-
if (
|
|
2678
|
+
const className = AST11.getClassIdentifier(currentClass)?.name;
|
|
2679
|
+
const { node: defNode, isUsed } = stateDefs.get(currentClass) ?? {};
|
|
2680
|
+
if (!defNode || isUsed) {
|
|
2743
2681
|
return;
|
|
2744
2682
|
}
|
|
2745
2683
|
context.report({
|
|
2746
2684
|
messageId: "noUnusedState",
|
|
2747
|
-
node:
|
|
2685
|
+
node: defNode,
|
|
2748
2686
|
data: {
|
|
2749
|
-
className:
|
|
2687
|
+
className: className ?? "Component"
|
|
2750
2688
|
}
|
|
2751
2689
|
});
|
|
2752
2690
|
}
|
|
2753
2691
|
function methodEnter(node) {
|
|
2754
|
-
|
|
2755
|
-
const currentClass =
|
|
2692
|
+
methodEntries.push(node);
|
|
2693
|
+
const currentClass = classEntries.at(-1);
|
|
2756
2694
|
if (currentClass == null || !isClassComponent(currentClass)) {
|
|
2757
2695
|
return;
|
|
2758
2696
|
}
|
|
2759
2697
|
if (node.static) {
|
|
2760
2698
|
if (isGetDerivedStateFromProps(node) && node.value.params.length > 1) {
|
|
2761
|
-
const
|
|
2762
|
-
stateDefs.set(currentClass,
|
|
2699
|
+
const defNode = stateDefs.get(currentClass)?.node;
|
|
2700
|
+
stateDefs.set(currentClass, { node: defNode, isUsed: true });
|
|
2763
2701
|
}
|
|
2764
2702
|
return;
|
|
2765
2703
|
}
|
|
2766
|
-
if (
|
|
2767
|
-
stateDefs.set(currentClass,
|
|
2704
|
+
if (getName4(node.key) === "state") {
|
|
2705
|
+
stateDefs.set(currentClass, { node: node.key, isUsed: false });
|
|
2768
2706
|
}
|
|
2769
2707
|
}
|
|
2770
2708
|
function methodExit() {
|
|
2771
|
-
|
|
2709
|
+
methodEntries.pop();
|
|
2772
2710
|
}
|
|
2773
2711
|
function constructorEnter(node) {
|
|
2774
|
-
|
|
2712
|
+
constructorEntries.push(node);
|
|
2775
2713
|
}
|
|
2776
2714
|
function constructorExit() {
|
|
2777
|
-
|
|
2715
|
+
constructorEntries.pop();
|
|
2778
2716
|
}
|
|
2779
2717
|
return {
|
|
2780
2718
|
AssignmentExpression(node) {
|
|
2781
2719
|
if (!isAssignmentToThisState2(node)) {
|
|
2782
2720
|
return;
|
|
2783
2721
|
}
|
|
2784
|
-
const currentClass =
|
|
2722
|
+
const currentClass = classEntries.at(-1);
|
|
2785
2723
|
if (currentClass == null || !isClassComponent(currentClass)) {
|
|
2786
2724
|
return;
|
|
2787
2725
|
}
|
|
2788
|
-
const currentConstructor =
|
|
2726
|
+
const currentConstructor = constructorEntries.at(-1);
|
|
2789
2727
|
if (currentConstructor == null || !currentClass.body.body.includes(currentConstructor)) {
|
|
2790
2728
|
return;
|
|
2791
2729
|
}
|
|
2792
|
-
const
|
|
2793
|
-
stateDefs.set(currentClass,
|
|
2730
|
+
const isUsed = stateDefs.get(currentClass)?.isUsed ?? false;
|
|
2731
|
+
stateDefs.set(currentClass, { node: node.left, isUsed });
|
|
2794
2732
|
},
|
|
2795
2733
|
ClassDeclaration: classEnter,
|
|
2796
2734
|
"ClassDeclaration:exit": classExit,
|
|
@@ -2800,25 +2738,25 @@ var no_unused_state_default = createRule({
|
|
|
2800
2738
|
if (!AST11.isThisExpression(node.object)) {
|
|
2801
2739
|
return;
|
|
2802
2740
|
}
|
|
2803
|
-
if (
|
|
2741
|
+
if (getName4(node.property) !== "state") {
|
|
2804
2742
|
return;
|
|
2805
2743
|
}
|
|
2806
|
-
const currentClass =
|
|
2744
|
+
const currentClass = classEntries.at(-1);
|
|
2807
2745
|
if (currentClass == null || !isClassComponent(currentClass)) {
|
|
2808
2746
|
return;
|
|
2809
2747
|
}
|
|
2810
|
-
const currentMethod =
|
|
2748
|
+
const currentMethod = methodEntries.at(-1);
|
|
2811
2749
|
if (currentMethod == null || currentMethod.static) {
|
|
2812
2750
|
return;
|
|
2813
2751
|
}
|
|
2814
|
-
if (currentMethod ===
|
|
2752
|
+
if (currentMethod === constructorEntries.at(-1)) {
|
|
2815
2753
|
return;
|
|
2816
2754
|
}
|
|
2817
2755
|
if (!currentClass.body.body.includes(currentMethod)) {
|
|
2818
2756
|
return;
|
|
2819
2757
|
}
|
|
2820
|
-
const
|
|
2821
|
-
stateDefs.set(currentClass,
|
|
2758
|
+
const defNode = stateDefs.get(currentClass)?.node;
|
|
2759
|
+
stateDefs.set(currentClass, { node: defNode, isUsed: true });
|
|
2822
2760
|
},
|
|
2823
2761
|
MethodDefinition: methodEnter,
|
|
2824
2762
|
"MethodDefinition:exit": methodExit,
|
|
@@ -2827,15 +2765,15 @@ var no_unused_state_default = createRule({
|
|
|
2827
2765
|
PropertyDefinition: methodEnter,
|
|
2828
2766
|
"PropertyDefinition:exit": methodExit,
|
|
2829
2767
|
VariableDeclarator(node) {
|
|
2830
|
-
const currentClass =
|
|
2768
|
+
const currentClass = classEntries.at(-1);
|
|
2831
2769
|
if (currentClass == null || !isClassComponent(currentClass)) {
|
|
2832
2770
|
return;
|
|
2833
2771
|
}
|
|
2834
|
-
const currentMethod =
|
|
2772
|
+
const currentMethod = methodEntries.at(-1);
|
|
2835
2773
|
if (currentMethod == null || currentMethod.static) {
|
|
2836
2774
|
return;
|
|
2837
2775
|
}
|
|
2838
|
-
if (currentMethod ===
|
|
2776
|
+
if (currentMethod === constructorEntries.at(-1)) {
|
|
2839
2777
|
return;
|
|
2840
2778
|
}
|
|
2841
2779
|
if (!currentClass.body.body.includes(currentMethod)) {
|
|
@@ -2846,15 +2784,15 @@ var no_unused_state_default = createRule({
|
|
|
2846
2784
|
}
|
|
2847
2785
|
const hasState = node.id.properties.some((prop) => {
|
|
2848
2786
|
if (prop.type === AST_NODE_TYPES.Property && AST11.isKeyLiteralLike(prop, prop.key)) {
|
|
2849
|
-
return
|
|
2787
|
+
return getName4(prop.key) === "state";
|
|
2850
2788
|
}
|
|
2851
2789
|
return false;
|
|
2852
2790
|
});
|
|
2853
2791
|
if (!hasState) {
|
|
2854
2792
|
return;
|
|
2855
2793
|
}
|
|
2856
|
-
const
|
|
2857
|
-
stateDefs.set(currentClass,
|
|
2794
|
+
const defNode = stateDefs.get(currentClass)?.node;
|
|
2795
|
+
stateDefs.set(currentClass, { node: defNode, isUsed: true });
|
|
2858
2796
|
}
|
|
2859
2797
|
};
|
|
2860
2798
|
},
|
|
@@ -2974,15 +2912,15 @@ var prefer_destructuring_assignment_default = createRule({
|
|
|
2974
2912
|
}
|
|
2975
2913
|
},
|
|
2976
2914
|
"Program:exit"(node) {
|
|
2977
|
-
const components =
|
|
2915
|
+
const components = [
|
|
2916
|
+
...ctx.getAllComponents(node).values()
|
|
2917
|
+
];
|
|
2978
2918
|
function isFunctionComponent(block) {
|
|
2979
2919
|
if (!AST11.isFunction(block)) {
|
|
2980
2920
|
return false;
|
|
2981
2921
|
}
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
(id) => isComponentName(id.name) && components.some((component) => component.node === block)
|
|
2985
|
-
);
|
|
2922
|
+
const id = AST11.getFunctionIdentifier(block);
|
|
2923
|
+
return !!id && isComponentName(id.name) && components.some((component) => component.node === block);
|
|
2986
2924
|
}
|
|
2987
2925
|
for (const [initialScope, memberExpression] of memberExpressionWithNames) {
|
|
2988
2926
|
let scope = initialScope;
|
|
@@ -3111,7 +3049,7 @@ var prefer_read_only_props_default = createRule({
|
|
|
3111
3049
|
...listeners,
|
|
3112
3050
|
"Program:exit"(node) {
|
|
3113
3051
|
const components = ctx.getAllComponents(node);
|
|
3114
|
-
for (const [
|
|
3052
|
+
for (const [, component] of components) {
|
|
3115
3053
|
const [props] = component.node.params;
|
|
3116
3054
|
if (props == null) {
|
|
3117
3055
|
continue;
|
|
@@ -3147,24 +3085,23 @@ var prefer_shorthand_boolean_default = createRule({
|
|
|
3147
3085
|
},
|
|
3148
3086
|
name: RULE_NAME48,
|
|
3149
3087
|
create(context) {
|
|
3150
|
-
function getReportDescriptor(node) {
|
|
3151
|
-
const { value } = node;
|
|
3152
|
-
const propName = JSX7.getPropName(node);
|
|
3153
|
-
const hasValueTrue = value?.type === AST_NODE_TYPES.JSXExpressionContainer && value.expression.type === AST_NODE_TYPES.Literal && value.expression.value === true;
|
|
3154
|
-
if (!hasValueTrue) {
|
|
3155
|
-
return O.none();
|
|
3156
|
-
}
|
|
3157
|
-
return O.some({
|
|
3158
|
-
messageId: "preferShorthandBoolean",
|
|
3159
|
-
node,
|
|
3160
|
-
data: {
|
|
3161
|
-
propName
|
|
3162
|
-
},
|
|
3163
|
-
fix: (fixer) => fixer.removeRange([node.name.range[1], value.range[1]])
|
|
3164
|
-
});
|
|
3165
|
-
}
|
|
3166
3088
|
return {
|
|
3167
|
-
JSXAttribute
|
|
3089
|
+
JSXAttribute(node) {
|
|
3090
|
+
const { value } = node;
|
|
3091
|
+
const propName = JSX7.getPropName(node);
|
|
3092
|
+
const hasValueTrue = value?.type === AST_NODE_TYPES.JSXExpressionContainer && value.expression.type === AST_NODE_TYPES.Literal && value.expression.value === true;
|
|
3093
|
+
if (!hasValueTrue) {
|
|
3094
|
+
return;
|
|
3095
|
+
}
|
|
3096
|
+
context.report({
|
|
3097
|
+
messageId: "preferShorthandBoolean",
|
|
3098
|
+
node,
|
|
3099
|
+
data: {
|
|
3100
|
+
propName
|
|
3101
|
+
},
|
|
3102
|
+
fix: (fixer) => fixer.removeRange([node.name.range[1], value.range[1]])
|
|
3103
|
+
});
|
|
3104
|
+
}
|
|
3168
3105
|
};
|
|
3169
3106
|
},
|
|
3170
3107
|
defaultOptions: []
|
|
@@ -3189,31 +3126,32 @@ var prefer_shorthand_fragment_default = createRule({
|
|
|
3189
3126
|
},
|
|
3190
3127
|
name: RULE_NAME49,
|
|
3191
3128
|
create(context) {
|
|
3192
|
-
function getReportDescriptor(node) {
|
|
3193
|
-
if (JSX7.getElementName(node.openingElement).split(".").at(-1) !== "Fragment") {
|
|
3194
|
-
return O.none();
|
|
3195
|
-
}
|
|
3196
|
-
const hasAttributes = node.openingElement.attributes.length > 0;
|
|
3197
|
-
if (hasAttributes) {
|
|
3198
|
-
return O.none();
|
|
3199
|
-
}
|
|
3200
|
-
return O.some({
|
|
3201
|
-
messageId: "preferShorthandFragment",
|
|
3202
|
-
node,
|
|
3203
|
-
fix: (fixer) => {
|
|
3204
|
-
const { closingElement, openingElement } = node;
|
|
3205
|
-
if (closingElement == null) {
|
|
3206
|
-
return [];
|
|
3207
|
-
}
|
|
3208
|
-
return [
|
|
3209
|
-
fixer.replaceTextRange([openingElement.range[0], openingElement.range[1]], "<>"),
|
|
3210
|
-
fixer.replaceTextRange([closingElement.range[0], closingElement.range[1]], "</>")
|
|
3211
|
-
];
|
|
3212
|
-
}
|
|
3213
|
-
});
|
|
3214
|
-
}
|
|
3215
3129
|
return {
|
|
3216
|
-
JSXElement
|
|
3130
|
+
JSXElement(node) {
|
|
3131
|
+
if (JSX7.getElementName(node.openingElement).split(".").at(-1) !== "Fragment") {
|
|
3132
|
+
return;
|
|
3133
|
+
}
|
|
3134
|
+
const hasAttributes = node.openingElement.attributes.length > 0;
|
|
3135
|
+
if (hasAttributes) {
|
|
3136
|
+
return;
|
|
3137
|
+
}
|
|
3138
|
+
context.report({
|
|
3139
|
+
messageId: "preferShorthandFragment",
|
|
3140
|
+
node,
|
|
3141
|
+
fix: (fixer) => {
|
|
3142
|
+
const { closingElement, openingElement } = node;
|
|
3143
|
+
if (closingElement == null) {
|
|
3144
|
+
return [];
|
|
3145
|
+
}
|
|
3146
|
+
return [
|
|
3147
|
+
// eslint-disable-next-line eslint-plugin/prefer-replace-text
|
|
3148
|
+
fixer.replaceTextRange([openingElement.range[0], openingElement.range[1]], "<>"),
|
|
3149
|
+
// eslint-disable-next-line eslint-plugin/prefer-replace-text
|
|
3150
|
+
fixer.replaceTextRange([closingElement.range[0], closingElement.range[1]], "</>")
|
|
3151
|
+
];
|
|
3152
|
+
}
|
|
3153
|
+
});
|
|
3154
|
+
}
|
|
3217
3155
|
};
|
|
3218
3156
|
},
|
|
3219
3157
|
defaultOptions: []
|
|
@@ -3235,23 +3173,23 @@ var use_jsx_vars_default = createRule({
|
|
|
3235
3173
|
},
|
|
3236
3174
|
name: RULE_NAME50,
|
|
3237
3175
|
create(context) {
|
|
3238
|
-
function getName5(node) {
|
|
3239
|
-
switch (node.type) {
|
|
3240
|
-
case AST_NODE_TYPES.JSXIdentifier:
|
|
3241
|
-
return O.some(node.name);
|
|
3242
|
-
case AST_NODE_TYPES.JSXMemberExpression:
|
|
3243
|
-
return getName5(node.object);
|
|
3244
|
-
default:
|
|
3245
|
-
return O.none();
|
|
3246
|
-
}
|
|
3247
|
-
}
|
|
3248
3176
|
return {
|
|
3249
3177
|
JSXOpeningElement(node) {
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3178
|
+
switch (node.name.type) {
|
|
3179
|
+
case AST_NODE_TYPES.JSXIdentifier: {
|
|
3180
|
+
if (/^[a-z]/u.test(node.name.name)) {
|
|
3181
|
+
return;
|
|
3182
|
+
}
|
|
3183
|
+
context.sourceCode.markVariableAsUsed(node.name.name, node);
|
|
3184
|
+
break;
|
|
3185
|
+
}
|
|
3186
|
+
case AST_NODE_TYPES.JSXMemberExpression: {
|
|
3187
|
+
const { object } = node.name;
|
|
3188
|
+
if (object.type === AST_NODE_TYPES.JSXIdentifier) {
|
|
3189
|
+
context.sourceCode.markVariableAsUsed(object.name, node);
|
|
3190
|
+
}
|
|
3191
|
+
break;
|
|
3192
|
+
}
|
|
3255
3193
|
}
|
|
3256
3194
|
}
|
|
3257
3195
|
};
|