eslint-plugin-react-x 5.30.0-beta.9 → 5.30.0-rc.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 +234 -1145
- package/package.json +11 -11
package/dist/index.js
CHANGED
|
@@ -2,10 +2,11 @@ import { DEFAULT_ESLINT_REACT_SETTINGS, getSettingsFromContext, toRegExp } from
|
|
|
2
2
|
import * as ESLintUtils from "@typescript-eslint/utils/eslint-utils";
|
|
3
3
|
import { Analyze, Check, Collect, Compare, Extract, Traverse } from "@eslint-react/ast";
|
|
4
4
|
import * as core from "@eslint-react/core";
|
|
5
|
+
import { isChildrenMap, isChildrenMapCall, isChildrenToArray, isChildrenToArrayCall } from "@eslint-react/core";
|
|
5
6
|
import { getConstrainedTypeAtLocation, getText, merge, report } from "@eslint-react/eslint";
|
|
6
7
|
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
7
8
|
import { P, isMatching, match } from "ts-pattern";
|
|
8
|
-
import { isAssignmentTargetEqual, isInitializedFromReact, resolveEnclosingAssignmentTarget, resolveObjectType, resolveOrigin, resolveOriginChain } from "@eslint-react/var";
|
|
9
|
+
import { isAssignmentTargetEqual, isInitializedFromReact, resolveAssignmentTargets, resolveEnclosingAssignmentTarget, resolveObjectType, resolveOrigin, resolveOriginChain } from "@eslint-react/var";
|
|
9
10
|
import { DefinitionType, ScopeType } from "@typescript-eslint/scope-manager";
|
|
10
11
|
import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
11
12
|
import { findParentAttribute, getElementFullType, getElementSelfType, hasAttribute, isInsideCreateElementProps, isPlainAttribute, parseCreateElementCall } from "@eslint-react/jsx";
|
|
@@ -38,7 +39,6 @@ const conflictingRules$1 = [
|
|
|
38
39
|
"react/hook-use-state",
|
|
39
40
|
"react/iframe-missing-sandbox",
|
|
40
41
|
"react/jsx-boolean-value",
|
|
41
|
-
"react/jsx-filename-extension",
|
|
42
42
|
"react/jsx-fragments",
|
|
43
43
|
"react/jsx-key",
|
|
44
44
|
"react/jsx-no-comment-textnodes",
|
|
@@ -60,12 +60,13 @@ const conflictingRules$1 = [
|
|
|
60
60
|
"react/no-find-dom-node",
|
|
61
61
|
"react/no-namespace",
|
|
62
62
|
"react/no-object-type-as-default-prop",
|
|
63
|
+
"react/no-redundant-should-component-update",
|
|
63
64
|
"react/no-render-return-value",
|
|
64
65
|
"react/no-string-refs",
|
|
65
66
|
"react/no-unknown-property",
|
|
66
67
|
"react/no-unsafe",
|
|
67
68
|
"react/no-unstable-nested-components",
|
|
68
|
-
"react/no-unused-class-component-
|
|
69
|
+
"react/no-unused-class-component-methods",
|
|
69
70
|
"react/no-unused-state",
|
|
70
71
|
"react/no-will-update-set-state",
|
|
71
72
|
"react/prop-types",
|
|
@@ -112,8 +113,7 @@ const rules$7 = {
|
|
|
112
113
|
"react-x/no-unused-state": "off",
|
|
113
114
|
"react-x/refs": "off",
|
|
114
115
|
"react-x/set-state-in-render": "off",
|
|
115
|
-
"react-x/static-components": "off"
|
|
116
|
-
"react-x/unstable-exhaustive-deps": "off"
|
|
116
|
+
"react-x/static-components": "off"
|
|
117
117
|
};
|
|
118
118
|
// src/configs/disable-type-checked.ts
|
|
119
119
|
var disable_type_checked_exports = /* @__PURE__ */ __exportAll({
|
|
@@ -130,7 +130,7 @@ const rules$6 = {
|
|
|
130
130
|
};
|
|
131
131
|
// package.json
|
|
132
132
|
var name$6 = "eslint-plugin-react-x";
|
|
133
|
-
var version = "5.30.0-
|
|
133
|
+
var version = "5.30.0-rc.0";
|
|
134
134
|
// src/utils/create-rule.ts
|
|
135
135
|
function getDocsUrl(ruleName) {
|
|
136
136
|
return `https://eslint-react.xyz/docs/rules/${ruleName}`;
|
|
@@ -192,7 +192,7 @@ function create$50(context) {
|
|
|
192
192
|
});
|
|
193
193
|
}
|
|
194
194
|
// src/rules/exhaustive-deps/exhaustive-deps.ts
|
|
195
|
-
const rule$
|
|
195
|
+
const rule$1 = {
|
|
196
196
|
meta: {
|
|
197
197
|
type: "suggestion",
|
|
198
198
|
docs: {
|
|
@@ -288,7 +288,7 @@ const rule$2 = {
|
|
|
288
288
|
while (init.type === "TSAsExpression" || init.type === "AsExpression") init = init.expression;
|
|
289
289
|
let declaration = defNode.parent;
|
|
290
290
|
if (declaration == null && componentScope != null) {
|
|
291
|
-
fastFindReferenceWithParent
|
|
291
|
+
fastFindReferenceWithParent(componentScope.block, def.node.id);
|
|
292
292
|
declaration = def.node.parent;
|
|
293
293
|
if (declaration == null) return false;
|
|
294
294
|
}
|
|
@@ -300,7 +300,7 @@ const rule$2 = {
|
|
|
300
300
|
const id = def.node.id;
|
|
301
301
|
const { name } = callee;
|
|
302
302
|
if (name === "useRef" && id.type === "Identifier") return true;
|
|
303
|
-
else if (isUseEffectEventIdentifier$
|
|
303
|
+
else if (isUseEffectEventIdentifier$1(callee) && id.type === "Identifier") {
|
|
304
304
|
for (const ref of resolved.references) if (ref !== id) useEffectEventVariables.add(ref.identifier);
|
|
305
305
|
return true;
|
|
306
306
|
} else if (name === "useState" || name === "useReducer" || name === "useActionState") {
|
|
@@ -372,10 +372,10 @@ const rule$2 = {
|
|
|
372
372
|
for (const reference of currentScope.references) {
|
|
373
373
|
if (!reference.resolved) continue;
|
|
374
374
|
if (!pureScopes.has(reference.resolved.scope)) continue;
|
|
375
|
-
const referenceNode = fastFindReferenceWithParent
|
|
375
|
+
const referenceNode = fastFindReferenceWithParent(node, reference.identifier);
|
|
376
376
|
if (referenceNode == null) continue;
|
|
377
|
-
const dependencyNode = getDependency
|
|
378
|
-
const dependency = analyzePropertyChain
|
|
377
|
+
const dependencyNode = getDependency(referenceNode);
|
|
378
|
+
const dependency = analyzePropertyChain(dependencyNode, optionalChains);
|
|
379
379
|
if (isEffect && dependencyNode.type === "Identifier" && (dependencyNode.parent?.type === "MemberExpression" || dependencyNode.parent?.type === "OptionalMemberExpression") && !dependencyNode.parent.computed && dependencyNode.parent.property.type === "Identifier" && dependencyNode.parent.property.name === "current" && isInsideEffectCleanup(reference)) currentRefsInEffectCleanup.set(dependency, {
|
|
380
380
|
reference,
|
|
381
381
|
dependencyNode
|
|
@@ -445,7 +445,7 @@ const rule$2 = {
|
|
|
445
445
|
});
|
|
446
446
|
});
|
|
447
447
|
if (setStateInsideEffectWithoutDeps) {
|
|
448
|
-
const { suggestedDependencies } = collectRecommendations
|
|
448
|
+
const { suggestedDependencies } = collectRecommendations({
|
|
449
449
|
dependencies,
|
|
450
450
|
declaredDependencies: [],
|
|
451
451
|
stableDependencies,
|
|
@@ -495,7 +495,7 @@ const rule$2 = {
|
|
|
495
495
|
});
|
|
496
496
|
let declaredDependency;
|
|
497
497
|
try {
|
|
498
|
-
declaredDependency = analyzePropertyChain
|
|
498
|
+
declaredDependency = analyzePropertyChain(declaredDependencyNode, null);
|
|
499
499
|
} catch (error) {
|
|
500
500
|
if (error instanceof Error && /Unsupported node type/.test(error.message)) {
|
|
501
501
|
if (declaredDependencyNode.type === "Literal") {
|
|
@@ -523,7 +523,7 @@ const rule$2 = {
|
|
|
523
523
|
});
|
|
524
524
|
if (!isDeclaredInComponent) externalDependencies.add(declaredDependency);
|
|
525
525
|
});
|
|
526
|
-
const { suggestedDependencies, unnecessaryDependencies, missingDependencies, duplicateDependencies } = collectRecommendations
|
|
526
|
+
const { suggestedDependencies, unnecessaryDependencies, missingDependencies, duplicateDependencies } = collectRecommendations({
|
|
527
527
|
dependencies,
|
|
528
528
|
declaredDependencies,
|
|
529
529
|
stableDependencies,
|
|
@@ -532,7 +532,7 @@ const rule$2 = {
|
|
|
532
532
|
});
|
|
533
533
|
let suggestedDeps = suggestedDependencies;
|
|
534
534
|
if (duplicateDependencies.size + missingDependencies.size + unnecessaryDependencies.size === 0) {
|
|
535
|
-
scanForConstructions
|
|
535
|
+
scanForConstructions({
|
|
536
536
|
declaredDependencies,
|
|
537
537
|
declaredDependenciesNode,
|
|
538
538
|
componentScope,
|
|
@@ -560,7 +560,7 @@ const rule$2 = {
|
|
|
560
560
|
});
|
|
561
561
|
return;
|
|
562
562
|
}
|
|
563
|
-
if (!isEffect && missingDependencies.size > 0) suggestedDeps = collectRecommendations
|
|
563
|
+
if (!isEffect && missingDependencies.size > 0) suggestedDeps = collectRecommendations({
|
|
564
564
|
dependencies,
|
|
565
565
|
declaredDependencies: [],
|
|
566
566
|
stableDependencies,
|
|
@@ -589,7 +589,7 @@ const rule$2 = {
|
|
|
589
589
|
}
|
|
590
590
|
function getWarningMessage(deps, singlePrefix, label, fixVerb) {
|
|
591
591
|
if (deps.size === 0) return null;
|
|
592
|
-
return (deps.size > 1 ? "" : singlePrefix + " ") + label + " " + (deps.size > 1 ? "dependencies" : "dependency") + ": " + joinEnglish
|
|
592
|
+
return (deps.size > 1 ? "" : singlePrefix + " ") + label + " " + (deps.size > 1 ? "dependencies" : "dependency") + ": " + joinEnglish(Array.from(deps).sort().map((name) => "'" + formatDependency(name) + "'")) + `. Either ${fixVerb} ${deps.size > 1 ? "them" : "it"} or remove the dependency array.`;
|
|
593
593
|
}
|
|
594
594
|
let extraWarning = "";
|
|
595
595
|
if (unnecessaryDependencies.size > 0) {
|
|
@@ -611,7 +611,7 @@ const rule$2 = {
|
|
|
611
611
|
if (!Array.isArray(refs)) return;
|
|
612
612
|
let isPropsOnlyUsedInMembers = true;
|
|
613
613
|
for (const ref of refs) {
|
|
614
|
-
const id = fastFindReferenceWithParent
|
|
614
|
+
const id = fastFindReferenceWithParent(componentScope.block, ref.identifier);
|
|
615
615
|
if (!id) {
|
|
616
616
|
isPropsOnlyUsedInMembers = false;
|
|
617
617
|
break;
|
|
@@ -719,11 +719,11 @@ const rule$2 = {
|
|
|
719
719
|
});
|
|
720
720
|
}
|
|
721
721
|
function visitCallExpression(node) {
|
|
722
|
-
const callbackIndex = getReactiveHookCallbackIndex
|
|
722
|
+
const callbackIndex = getReactiveHookCallbackIndex(node.callee, options);
|
|
723
723
|
if (callbackIndex === -1) return;
|
|
724
724
|
let callback = node.arguments[callbackIndex];
|
|
725
725
|
const reactiveHook = node.callee;
|
|
726
|
-
const nodeWithoutNamespace = getNodeWithoutReactNamespace$
|
|
726
|
+
const nodeWithoutNamespace = getNodeWithoutReactNamespace$1(reactiveHook);
|
|
727
727
|
const reactiveHookName = "name" in nodeWithoutNamespace ? nodeWithoutNamespace.name : "";
|
|
728
728
|
const maybeNode = node.arguments[callbackIndex + 1];
|
|
729
729
|
const declaredDependenciesNode = maybeNode && !(maybeNode.type === "Identifier" && maybeNode.name === "undefined") ? maybeNode : void 0;
|
|
@@ -763,7 +763,7 @@ const rule$2 = {
|
|
|
763
763
|
if (def.type === "Parameter") {
|
|
764
764
|
reportProblem({
|
|
765
765
|
node: reactiveHook,
|
|
766
|
-
message: getUnknownDependenciesMessage
|
|
766
|
+
message: getUnknownDependenciesMessage(reactiveHookName)
|
|
767
767
|
});
|
|
768
768
|
return;
|
|
769
769
|
}
|
|
@@ -786,7 +786,7 @@ const rule$2 = {
|
|
|
786
786
|
default:
|
|
787
787
|
reportProblem({
|
|
788
788
|
node: reactiveHook,
|
|
789
|
-
message: getUnknownDependenciesMessage
|
|
789
|
+
message: getUnknownDependenciesMessage(reactiveHookName)
|
|
790
790
|
});
|
|
791
791
|
return;
|
|
792
792
|
}
|
|
@@ -804,7 +804,7 @@ const rule$2 = {
|
|
|
804
804
|
return { CallExpression: visitCallExpression };
|
|
805
805
|
}
|
|
806
806
|
};
|
|
807
|
-
function collectRecommendations
|
|
807
|
+
function collectRecommendations({ dependencies, declaredDependencies, stableDependencies, externalDependencies, isEffect }) {
|
|
808
808
|
const depTree = createDepTree();
|
|
809
809
|
function createDepTree() {
|
|
810
810
|
return {
|
|
@@ -890,7 +890,7 @@ function collectRecommendations$1({ dependencies, declaredDependencies, stableDe
|
|
|
890
890
|
missingDependencies
|
|
891
891
|
};
|
|
892
892
|
}
|
|
893
|
-
function getConstructionExpressionType
|
|
893
|
+
function getConstructionExpressionType(node) {
|
|
894
894
|
switch (node.type) {
|
|
895
895
|
case "ObjectExpression": return "object";
|
|
896
896
|
case "ArrayExpression": return "array";
|
|
@@ -898,15 +898,15 @@ function getConstructionExpressionType$1(node) {
|
|
|
898
898
|
case "FunctionExpression": return "function";
|
|
899
899
|
case "ClassExpression": return "class";
|
|
900
900
|
case "ConditionalExpression":
|
|
901
|
-
if (getConstructionExpressionType
|
|
901
|
+
if (getConstructionExpressionType(node.consequent) != null || getConstructionExpressionType(node.alternate) != null) return "conditional";
|
|
902
902
|
return null;
|
|
903
903
|
case "LogicalExpression":
|
|
904
|
-
if (getConstructionExpressionType
|
|
904
|
+
if (getConstructionExpressionType(node.left) != null || getConstructionExpressionType(node.right) != null) return "logical expression";
|
|
905
905
|
return null;
|
|
906
906
|
case "JSXFragment": return "JSX fragment";
|
|
907
907
|
case "JSXElement": return "JSX element";
|
|
908
908
|
case "AssignmentExpression":
|
|
909
|
-
if (getConstructionExpressionType
|
|
909
|
+
if (getConstructionExpressionType(node.right) != null) return "assignment expression";
|
|
910
910
|
return null;
|
|
911
911
|
case "NewExpression": return "object construction";
|
|
912
912
|
case "Literal":
|
|
@@ -914,18 +914,18 @@ function getConstructionExpressionType$1(node) {
|
|
|
914
914
|
return null;
|
|
915
915
|
case "TypeCastExpression":
|
|
916
916
|
case "AsExpression":
|
|
917
|
-
case "TSAsExpression": return getConstructionExpressionType
|
|
917
|
+
case "TSAsExpression": return getConstructionExpressionType(node.expression);
|
|
918
918
|
}
|
|
919
919
|
return null;
|
|
920
920
|
}
|
|
921
|
-
function scanForConstructions
|
|
921
|
+
function scanForConstructions({ declaredDependencies, declaredDependenciesNode, componentScope, scope }) {
|
|
922
922
|
const constructions = declaredDependencies.map(({ key }) => {
|
|
923
923
|
const ref = componentScope.variables.find((v) => v.name === key);
|
|
924
924
|
if (ref == null) return null;
|
|
925
925
|
const node = ref.defs[0];
|
|
926
926
|
if (node == null) return null;
|
|
927
927
|
if (node.type === "Variable" && node.node.type === "VariableDeclarator" && node.node.id.type === "Identifier" && node.node.init != null) {
|
|
928
|
-
const constantExpressionType = getConstructionExpressionType
|
|
928
|
+
const constantExpressionType = getConstructionExpressionType(node.node.init);
|
|
929
929
|
if (constantExpressionType) return [ref, constantExpressionType];
|
|
930
930
|
}
|
|
931
931
|
if (node.type === "FunctionName" && node.node.type === "FunctionDeclaration") return [ref, "function"];
|
|
@@ -945,7 +945,7 @@ function scanForConstructions$1({ declaredDependencies, declaredDependenciesNode
|
|
|
945
945
|
let currentScope = reference.from;
|
|
946
946
|
while (currentScope !== scope && currentScope != null) currentScope = currentScope.upper;
|
|
947
947
|
if (currentScope !== scope) {
|
|
948
|
-
if (!isAncestorNodeOf
|
|
948
|
+
if (!isAncestorNodeOf(declaredDependenciesNode, reference.identifier)) return true;
|
|
949
949
|
}
|
|
950
950
|
}
|
|
951
951
|
return false;
|
|
@@ -956,45 +956,45 @@ function scanForConstructions$1({ declaredDependencies, declaredDependenciesNode
|
|
|
956
956
|
isUsedOutsideOfHook: isUsedOutsideOfHook(ref)
|
|
957
957
|
}));
|
|
958
958
|
}
|
|
959
|
-
function getDependency
|
|
960
|
-
if (node.parent && (node.parent.type === "MemberExpression" || node.parent.type === "OptionalMemberExpression") && node.parent.object === node && "name" in node.parent.property && node.parent.property.name !== "current" && !node.parent.computed && !(node.parent.parent != null && (node.parent.parent.type === "CallExpression" || node.parent.parent.type === "OptionalCallExpression") && node.parent.parent.callee === node.parent)) return getDependency
|
|
959
|
+
function getDependency(node) {
|
|
960
|
+
if (node.parent && (node.parent.type === "MemberExpression" || node.parent.type === "OptionalMemberExpression") && node.parent.object === node && "name" in node.parent.property && node.parent.property.name !== "current" && !node.parent.computed && !(node.parent.parent != null && (node.parent.parent.type === "CallExpression" || node.parent.parent.type === "OptionalCallExpression") && node.parent.parent.callee === node.parent)) return getDependency(node.parent);
|
|
961
961
|
else if (node.type === "MemberExpression" && node.parent && node.parent.type === "AssignmentExpression" && node.parent.left === node) return node.object;
|
|
962
962
|
else return node;
|
|
963
963
|
}
|
|
964
|
-
function markNode
|
|
964
|
+
function markNode(node, optionalChains, result) {
|
|
965
965
|
if (optionalChains) {
|
|
966
966
|
if ("optional" in node && node.optional) {
|
|
967
967
|
if (!optionalChains.has(result)) optionalChains.set(result, true);
|
|
968
968
|
} else optionalChains.set(result, false);
|
|
969
969
|
}
|
|
970
970
|
}
|
|
971
|
-
function analyzePropertyChain
|
|
971
|
+
function analyzePropertyChain(node, optionalChains) {
|
|
972
972
|
if (node.type === "Identifier" || node.type === "JSXIdentifier") {
|
|
973
973
|
const result = node.name;
|
|
974
974
|
if (optionalChains) optionalChains.set(result, false);
|
|
975
975
|
return result;
|
|
976
976
|
} else if (node.type === "MemberExpression" && !node.computed) {
|
|
977
|
-
const result = `${analyzePropertyChain
|
|
978
|
-
markNode
|
|
977
|
+
const result = `${analyzePropertyChain(node.object, optionalChains)}.${analyzePropertyChain(node.property, null)}`;
|
|
978
|
+
markNode(node, optionalChains, result);
|
|
979
979
|
return result;
|
|
980
980
|
} else if (node.type === "OptionalMemberExpression" && !node.computed) {
|
|
981
|
-
const result = `${analyzePropertyChain
|
|
982
|
-
markNode
|
|
981
|
+
const result = `${analyzePropertyChain(node.object, optionalChains)}.${analyzePropertyChain(node.property, null)}`;
|
|
982
|
+
markNode(node, optionalChains, result);
|
|
983
983
|
return result;
|
|
984
984
|
} else if (node.type === "ChainExpression" && (!("computed" in node) || !node.computed)) {
|
|
985
985
|
const expression = node.expression;
|
|
986
986
|
if (expression.type === "CallExpression") throw new Error(`Unsupported node type: ${expression.type}`);
|
|
987
|
-
const result = `${analyzePropertyChain
|
|
988
|
-
markNode
|
|
987
|
+
const result = `${analyzePropertyChain(expression.object, optionalChains)}.${analyzePropertyChain(expression.property, null)}`;
|
|
988
|
+
markNode(expression, optionalChains, result);
|
|
989
989
|
return result;
|
|
990
990
|
} else throw new Error(`Unsupported node type: ${node.type}`);
|
|
991
991
|
}
|
|
992
|
-
function getNodeWithoutReactNamespace$
|
|
992
|
+
function getNodeWithoutReactNamespace$1(node) {
|
|
993
993
|
if (node.type === "MemberExpression" && node.object.type === "Identifier" && node.object.name === "React" && node.property.type === "Identifier" && !node.computed) return node.property;
|
|
994
994
|
return node;
|
|
995
995
|
}
|
|
996
|
-
function getReactiveHookCallbackIndex
|
|
997
|
-
const node = getNodeWithoutReactNamespace$
|
|
996
|
+
function getReactiveHookCallbackIndex(calleeNode, options) {
|
|
997
|
+
const node = getNodeWithoutReactNamespace$1(calleeNode);
|
|
998
998
|
if (node.type !== "Identifier") return -1;
|
|
999
999
|
switch (node.name) {
|
|
1000
1000
|
case "useEffect":
|
|
@@ -1005,7 +1005,7 @@ function getReactiveHookCallbackIndex$1(calleeNode, options) {
|
|
|
1005
1005
|
default: if (node === calleeNode && options && options.additionalHooks) {
|
|
1006
1006
|
let name;
|
|
1007
1007
|
try {
|
|
1008
|
-
name = analyzePropertyChain
|
|
1008
|
+
name = analyzePropertyChain(node, null);
|
|
1009
1009
|
} catch (error) {
|
|
1010
1010
|
if (error instanceof Error && /Unsupported node type/.test(error.message)) return 0;
|
|
1011
1011
|
else throw error;
|
|
@@ -1014,20 +1014,20 @@ function getReactiveHookCallbackIndex$1(calleeNode, options) {
|
|
|
1014
1014
|
} else return -1;
|
|
1015
1015
|
}
|
|
1016
1016
|
}
|
|
1017
|
-
function fastFindReferenceWithParent
|
|
1017
|
+
function fastFindReferenceWithParent(start, target) {
|
|
1018
1018
|
const queue = [start];
|
|
1019
1019
|
let item;
|
|
1020
1020
|
while (queue.length) {
|
|
1021
1021
|
item = queue.shift();
|
|
1022
|
-
if (isSameIdentifier
|
|
1023
|
-
if (!isAncestorNodeOf
|
|
1022
|
+
if (isSameIdentifier(item, target)) return item;
|
|
1023
|
+
if (!isAncestorNodeOf(item, target)) continue;
|
|
1024
1024
|
for (const [key, value] of Object.entries(item)) {
|
|
1025
1025
|
if (key === "parent") continue;
|
|
1026
|
-
if (isNodeLike
|
|
1026
|
+
if (isNodeLike(value)) {
|
|
1027
1027
|
value.parent = item;
|
|
1028
1028
|
queue.push(value);
|
|
1029
1029
|
} else if (Array.isArray(value)) value.forEach((val) => {
|
|
1030
|
-
if (isNodeLike
|
|
1030
|
+
if (isNodeLike(val)) {
|
|
1031
1031
|
val.parent = item;
|
|
1032
1032
|
queue.push(val);
|
|
1033
1033
|
}
|
|
@@ -1036,7 +1036,7 @@ function fastFindReferenceWithParent$1(start, target) {
|
|
|
1036
1036
|
}
|
|
1037
1037
|
return null;
|
|
1038
1038
|
}
|
|
1039
|
-
function joinEnglish
|
|
1039
|
+
function joinEnglish(arr) {
|
|
1040
1040
|
let s = "";
|
|
1041
1041
|
for (let i = 0; i < arr.length; i++) {
|
|
1042
1042
|
s += arr[i];
|
|
@@ -1046,19 +1046,19 @@ function joinEnglish$1(arr) {
|
|
|
1046
1046
|
}
|
|
1047
1047
|
return s;
|
|
1048
1048
|
}
|
|
1049
|
-
function isNodeLike
|
|
1049
|
+
function isNodeLike(val) {
|
|
1050
1050
|
return typeof val === "object" && val !== null && !Array.isArray(val) && "type" in val && typeof val.type === "string";
|
|
1051
1051
|
}
|
|
1052
|
-
function isSameIdentifier
|
|
1052
|
+
function isSameIdentifier(a, b) {
|
|
1053
1053
|
return (a.type === "Identifier" || a.type === "JSXIdentifier") && a.type === b.type && a.name === b.name && !!a.range && !!b.range && a.range[0] === b.range[0] && a.range[1] === b.range[1];
|
|
1054
1054
|
}
|
|
1055
|
-
function isAncestorNodeOf
|
|
1055
|
+
function isAncestorNodeOf(a, b) {
|
|
1056
1056
|
return !!a.range && !!b.range && a.range[0] <= b.range[0] && a.range[1] >= b.range[1];
|
|
1057
1057
|
}
|
|
1058
|
-
function isUseEffectEventIdentifier$
|
|
1058
|
+
function isUseEffectEventIdentifier$1(node) {
|
|
1059
1059
|
return node.type === "Identifier" && node.name === "useEffectEvent";
|
|
1060
1060
|
}
|
|
1061
|
-
function getUnknownDependenciesMessage
|
|
1061
|
+
function getUnknownDependenciesMessage(reactiveHookName) {
|
|
1062
1062
|
return `React Hook ${reactiveHookName} received a function whose dependencies are unknown. Pass an inline function instead.`;
|
|
1063
1063
|
}
|
|
1064
1064
|
// src/rules/globals/helpers.ts
|
|
@@ -1096,7 +1096,7 @@ function createFactCollector$6() {
|
|
|
1096
1096
|
facts,
|
|
1097
1097
|
visitor: {
|
|
1098
1098
|
AssignmentExpression(node) {
|
|
1099
|
-
for (const target of
|
|
1099
|
+
for (const target of resolveAssignmentTargets(node.left)) pushWrite(node, target);
|
|
1100
1100
|
},
|
|
1101
1101
|
CallExpression(node) {
|
|
1102
1102
|
const caller = getEnclosingFunction(node);
|
|
@@ -1118,11 +1118,11 @@ function createFactCollector$6() {
|
|
|
1118
1118
|
},
|
|
1119
1119
|
ForInStatement(node) {
|
|
1120
1120
|
if (node.left.type === AST_NODE_TYPES.VariableDeclaration) return;
|
|
1121
|
-
for (const target of
|
|
1121
|
+
for (const target of resolveAssignmentTargets(node.left)) pushWrite(node, target);
|
|
1122
1122
|
},
|
|
1123
1123
|
ForOfStatement(node) {
|
|
1124
1124
|
if (node.left.type === AST_NODE_TYPES.VariableDeclaration) return;
|
|
1125
|
-
for (const target of
|
|
1125
|
+
for (const target of resolveAssignmentTargets(node.left)) pushWrite(node, target);
|
|
1126
1126
|
},
|
|
1127
1127
|
UnaryExpression(node) {
|
|
1128
1128
|
if (node.operator !== "delete") return;
|
|
@@ -1254,10 +1254,10 @@ function create$49(context) {
|
|
|
1254
1254
|
const comps = core.getFunctionComponentCollector(context);
|
|
1255
1255
|
const facts = createFactCollector$6();
|
|
1256
1256
|
return merge(hooks.visitor, comps.visitor, facts.visitor, { "Program:exit"(program) {
|
|
1257
|
-
const
|
|
1257
|
+
const funcs = [...comps.api.getAllComponents(program), ...hooks.api.getAllHooks(program)].map(({ node }) => node);
|
|
1258
1258
|
const directEffects = inferGlobalMutations(context, facts.facts);
|
|
1259
1259
|
const callGraph = inferCallGraph$1(context, facts.facts.callEdges);
|
|
1260
|
-
for (const effect of collectReachableEffects(
|
|
1260
|
+
for (const effect of collectReachableEffects(funcs, directEffects, callGraph)) {
|
|
1261
1261
|
const data = effect.method == null ? { name: effect.name } : {
|
|
1262
1262
|
name: effect.name,
|
|
1263
1263
|
method: effect.method
|
|
@@ -1304,9 +1304,13 @@ function isRefLikeName$2(name) {
|
|
|
1304
1304
|
return name === "ref" || name.endsWith("Ref");
|
|
1305
1305
|
}
|
|
1306
1306
|
function hasRefLikeNameInChain(node) {
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1307
|
+
let current = Extract.unwrap(node);
|
|
1308
|
+
while (current.type === AST_NODE_TYPES.MemberExpression) {
|
|
1309
|
+
const property = Extract.unwrap(current.property);
|
|
1310
|
+
if (Check.isIdentifier(property) && isRefLikeName$2(property.name)) return true;
|
|
1311
|
+
current = Extract.unwrap(current.object);
|
|
1312
|
+
}
|
|
1313
|
+
return Check.isIdentifier(current) && isRefLikeName$2(current.name);
|
|
1310
1314
|
}
|
|
1311
1315
|
// src/rules/immutability/collect.ts
|
|
1312
1316
|
function createFactCollector$5() {
|
|
@@ -1326,8 +1330,9 @@ function createFactCollector$5() {
|
|
|
1326
1330
|
});
|
|
1327
1331
|
}
|
|
1328
1332
|
function pushMemberMutation(node, target) {
|
|
1329
|
-
|
|
1330
|
-
|
|
1333
|
+
let root = Extract.unwrap(target);
|
|
1334
|
+
while (root.type === AST_NODE_TYPES.MemberExpression) root = Extract.unwrap(root.object);
|
|
1335
|
+
if (!Check.isIdentifier(root)) return;
|
|
1331
1336
|
pushMutation("value", node, target, root);
|
|
1332
1337
|
}
|
|
1333
1338
|
function pushTargetMutation(node, target) {
|
|
@@ -1341,7 +1346,7 @@ function createFactCollector$5() {
|
|
|
1341
1346
|
facts,
|
|
1342
1347
|
visitor: {
|
|
1343
1348
|
AssignmentExpression(node) {
|
|
1344
|
-
for (const target of
|
|
1349
|
+
for (const target of resolveAssignmentTargets(node.left)) pushTargetMutation(node, target);
|
|
1345
1350
|
},
|
|
1346
1351
|
CallExpression(node) {
|
|
1347
1352
|
const callee = Extract.unwrap(node.callee);
|
|
@@ -1360,11 +1365,11 @@ function createFactCollector$5() {
|
|
|
1360
1365
|
},
|
|
1361
1366
|
ForInStatement(node) {
|
|
1362
1367
|
if (node.left.type === AST_NODE_TYPES.VariableDeclaration) return;
|
|
1363
|
-
for (const target of
|
|
1368
|
+
for (const target of resolveAssignmentTargets(node.left)) pushTargetMutation(node, target);
|
|
1364
1369
|
},
|
|
1365
1370
|
ForOfStatement(node) {
|
|
1366
1371
|
if (node.left.type === AST_NODE_TYPES.VariableDeclaration) return;
|
|
1367
|
-
for (const target of
|
|
1372
|
+
for (const target of resolveAssignmentTargets(node.left)) pushTargetMutation(node, target);
|
|
1368
1373
|
},
|
|
1369
1374
|
JSXAttribute(node) {
|
|
1370
1375
|
if (node.value?.type !== AST_NODE_TYPES.JSXExpressionContainer) return;
|
|
@@ -1430,8 +1435,9 @@ function getStateHookName(context, init) {
|
|
|
1430
1435
|
return null;
|
|
1431
1436
|
}
|
|
1432
1437
|
function isInitializedFromCall(context, node, isCall) {
|
|
1433
|
-
|
|
1434
|
-
|
|
1438
|
+
let root = Extract.unwrap(node);
|
|
1439
|
+
while (root.type === AST_NODE_TYPES.MemberExpression) root = Extract.unwrap(root.object);
|
|
1440
|
+
if (!Check.isIdentifier(root)) return false;
|
|
1435
1441
|
const variable = findVariable(context.sourceCode.getScope(root), root);
|
|
1436
1442
|
if (variable == null) return false;
|
|
1437
1443
|
const origin = resolveVariableOrigin(context, variable);
|
|
@@ -1461,8 +1467,9 @@ function classifyFrozenOrigin(context, variable, components, mode = "alias", see
|
|
|
1461
1467
|
if (def.node.init == null) {
|
|
1462
1468
|
const loop = def.node.parent.parent;
|
|
1463
1469
|
if (loop.type !== AST_NODE_TYPES.ForOfStatement || loop.left !== def.node.parent) return null;
|
|
1464
|
-
|
|
1465
|
-
|
|
1470
|
+
let root = Extract.unwrap(loop.right);
|
|
1471
|
+
while (root.type === AST_NODE_TYPES.MemberExpression) root = Extract.unwrap(root.object);
|
|
1472
|
+
if (!Check.isIdentifier(root)) return null;
|
|
1466
1473
|
const source = findVariable(context.sourceCode.getScope(root), root);
|
|
1467
1474
|
if (source == null) return null;
|
|
1468
1475
|
const inner = classifyFrozenOrigin(context, source, components, mode, seen);
|
|
@@ -1793,7 +1800,8 @@ function create$46(context) {
|
|
|
1793
1800
|
for (const property of propsObject.properties) {
|
|
1794
1801
|
if (property.type !== AST_NODE_TYPES.Property) continue;
|
|
1795
1802
|
if (property.computed) continue;
|
|
1796
|
-
|
|
1803
|
+
const key = Extract.unwrap(property.key);
|
|
1804
|
+
if (Check.isIdentifier(key) ? key.name !== "key" : key.value !== "key") continue;
|
|
1797
1805
|
const value = property.value;
|
|
1798
1806
|
for (const desc of visitKeyExpression(value)) context.report(desc);
|
|
1799
1807
|
}
|
|
@@ -2281,6 +2289,79 @@ function isAssignmentToThisState(node) {
|
|
|
2281
2289
|
}
|
|
2282
2290
|
return false;
|
|
2283
2291
|
}
|
|
2292
|
+
// src/rules/no-duplicate-key/lib.ts
|
|
2293
|
+
function isReturnedBy(element, fn) {
|
|
2294
|
+
let node = element;
|
|
2295
|
+
let viaReturn = false;
|
|
2296
|
+
while (true) {
|
|
2297
|
+
const parent = node.parent;
|
|
2298
|
+
if (parent == null) return false;
|
|
2299
|
+
if (parent === fn) {
|
|
2300
|
+
if (viaReturn) return true;
|
|
2301
|
+
return parent.type === AST_NODE_TYPES.ArrowFunctionExpression && parent.body === node;
|
|
2302
|
+
}
|
|
2303
|
+
if (Check.isTypeOrChainExpression(parent) || parent.type === AST_NODE_TYPES.LogicalExpression) {
|
|
2304
|
+
node = parent;
|
|
2305
|
+
continue;
|
|
2306
|
+
}
|
|
2307
|
+
switch (parent.type) {
|
|
2308
|
+
case AST_NODE_TYPES.ConditionalExpression:
|
|
2309
|
+
if (parent.test === node) return false;
|
|
2310
|
+
node = parent;
|
|
2311
|
+
continue;
|
|
2312
|
+
case AST_NODE_TYPES.SequenceExpression:
|
|
2313
|
+
if (parent.expressions[parent.expressions.length - 1] !== node) return false;
|
|
2314
|
+
node = parent;
|
|
2315
|
+
continue;
|
|
2316
|
+
case AST_NODE_TYPES.ReturnStatement:
|
|
2317
|
+
viaReturn = true;
|
|
2318
|
+
node = parent;
|
|
2319
|
+
continue;
|
|
2320
|
+
case AST_NODE_TYPES.BlockStatement:
|
|
2321
|
+
case AST_NODE_TYPES.IfStatement:
|
|
2322
|
+
if (!viaReturn) return false;
|
|
2323
|
+
node = parent;
|
|
2324
|
+
continue;
|
|
2325
|
+
default: return false;
|
|
2326
|
+
}
|
|
2327
|
+
}
|
|
2328
|
+
}
|
|
2329
|
+
function isRekeyedByChildrenCall(context, root) {
|
|
2330
|
+
if (root.type === AST_NODE_TYPES.JSXElement) return false;
|
|
2331
|
+
let node = root.parent;
|
|
2332
|
+
while (node != null) {
|
|
2333
|
+
if (isChildrenToArrayCall(context, node) || isChildrenMapCall(context, node)) return true;
|
|
2334
|
+
switch (node.type) {
|
|
2335
|
+
case AST_NODE_TYPES.ArrayExpression:
|
|
2336
|
+
case AST_NODE_TYPES.ConditionalExpression:
|
|
2337
|
+
case AST_NODE_TYPES.JSXExpressionContainer:
|
|
2338
|
+
case AST_NODE_TYPES.JSXFragment:
|
|
2339
|
+
case AST_NODE_TYPES.LogicalExpression:
|
|
2340
|
+
case AST_NODE_TYPES.SpreadElement:
|
|
2341
|
+
node = node.parent;
|
|
2342
|
+
continue;
|
|
2343
|
+
default:
|
|
2344
|
+
if (Check.isTypeOrChainExpression(node)) {
|
|
2345
|
+
node = node.parent;
|
|
2346
|
+
continue;
|
|
2347
|
+
}
|
|
2348
|
+
return false;
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
return false;
|
|
2352
|
+
}
|
|
2353
|
+
function mapsProvablyAtMostOneElement(call) {
|
|
2354
|
+
const callee = Extract.unwrap(call.callee);
|
|
2355
|
+
if (callee.type !== AST_NODE_TYPES.MemberExpression) return false;
|
|
2356
|
+
const object = Extract.unwrap(callee.object);
|
|
2357
|
+
return object.type === AST_NODE_TYPES.ArrayExpression && object.elements.length <= 1 && !object.elements.some((element) => element?.type === AST_NODE_TYPES.SpreadElement);
|
|
2358
|
+
}
|
|
2359
|
+
function isKeyValueEqual(a, b) {
|
|
2360
|
+
const aValue = a.value;
|
|
2361
|
+
const bValue = b.value;
|
|
2362
|
+
if (aValue == null || bValue == null) return false;
|
|
2363
|
+
return Compare.isEqual(aValue, bValue);
|
|
2364
|
+
}
|
|
2284
2365
|
var no_duplicate_key_default = createRule({
|
|
2285
2366
|
meta: {
|
|
2286
2367
|
type: "problem",
|
|
@@ -2294,17 +2375,31 @@ var no_duplicate_key_default = createRule({
|
|
|
2294
2375
|
function create$32(context) {
|
|
2295
2376
|
if (!/\bkey\b/u.test(context.sourceCode.text)) return {};
|
|
2296
2377
|
const keyGroups = /* @__PURE__ */ new Map();
|
|
2378
|
+
function getGroup(root) {
|
|
2379
|
+
let group = keyGroups.get(root);
|
|
2380
|
+
if (group == null) {
|
|
2381
|
+
group = { keys: [] };
|
|
2382
|
+
keyGroups.set(root, group);
|
|
2383
|
+
}
|
|
2384
|
+
return group;
|
|
2385
|
+
}
|
|
2297
2386
|
function recordSiblingKey(root, key) {
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2387
|
+
if (isRekeyedByChildrenCall(context, root)) return;
|
|
2388
|
+
const group = getGroup(root);
|
|
2389
|
+
let duplicate = false;
|
|
2390
|
+
for (const entry of group.keys) if (isKeyValueEqual(entry.node, key)) {
|
|
2391
|
+
entry.duplicate = true;
|
|
2392
|
+
duplicate = true;
|
|
2393
|
+
}
|
|
2394
|
+
group.keys.push({
|
|
2395
|
+
duplicate,
|
|
2396
|
+
node: key
|
|
2302
2397
|
});
|
|
2303
2398
|
}
|
|
2304
2399
|
function recordMapCallbackKey(call, key) {
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2400
|
+
getGroup(call).keys.push({
|
|
2401
|
+
duplicate: key.value?.type === AST_NODE_TYPES.Literal,
|
|
2402
|
+
node: key
|
|
2308
2403
|
});
|
|
2309
2404
|
}
|
|
2310
2405
|
return {
|
|
@@ -2324,30 +2419,28 @@ function create$32(context) {
|
|
|
2324
2419
|
return Check.isIdentifier(callee.property, "map");
|
|
2325
2420
|
});
|
|
2326
2421
|
if (call == null) return;
|
|
2422
|
+
if (isChildrenMap(context, call.callee) || isChildrenToArray(context, call.callee)) return;
|
|
2327
2423
|
const callback = call.arguments[0];
|
|
2328
2424
|
if (callback == null) return;
|
|
2329
|
-
|
|
2425
|
+
const callbackFn = Extract.unwrap(callback);
|
|
2426
|
+
if (!Check.isFunction(callbackFn)) return;
|
|
2427
|
+
if (!isReturnedBy(jsxElement, callbackFn)) return;
|
|
2428
|
+
if (mapsProvablyAtMostOneElement(call)) return;
|
|
2330
2429
|
recordMapCallbackKey(call, node);
|
|
2331
2430
|
}
|
|
2332
2431
|
}
|
|
2333
2432
|
},
|
|
2334
2433
|
"Program:exit"() {
|
|
2335
|
-
for (const {
|
|
2336
|
-
if (!
|
|
2337
|
-
|
|
2434
|
+
for (const { keys } of keyGroups.values()) for (const { duplicate, node } of keys) {
|
|
2435
|
+
if (!duplicate) continue;
|
|
2436
|
+
context.report({
|
|
2338
2437
|
messageId: "default",
|
|
2339
|
-
node
|
|
2438
|
+
node
|
|
2340
2439
|
});
|
|
2341
2440
|
}
|
|
2342
2441
|
}
|
|
2343
2442
|
};
|
|
2344
2443
|
}
|
|
2345
|
-
function isKeyValueEqual(a, b) {
|
|
2346
|
-
const aValue = a.value;
|
|
2347
|
-
const bValue = b.value;
|
|
2348
|
-
if (aValue == null || bValue == null) return false;
|
|
2349
|
-
return Compare.isEqual(aValue, bValue);
|
|
2350
|
-
}
|
|
2351
2444
|
var no_forward_ref_default = createRule({
|
|
2352
2445
|
meta: {
|
|
2353
2446
|
type: "suggestion",
|
|
@@ -2703,24 +2796,13 @@ function create$25(context) {
|
|
|
2703
2796
|
}
|
|
2704
2797
|
};
|
|
2705
2798
|
}
|
|
2706
|
-
// src/
|
|
2707
|
-
const
|
|
2799
|
+
// src/rules/no-missing-key/no-missing-key.ts
|
|
2800
|
+
const RULE_NAME$24 = "no-missing-key";
|
|
2801
|
+
const ITERATOR_CALLBACK_POSITIONS = /* @__PURE__ */ new Map([
|
|
2708
2802
|
["flatMap", 0],
|
|
2709
2803
|
["from", 1],
|
|
2710
2804
|
["map", 0]
|
|
2711
2805
|
]);
|
|
2712
|
-
function getIteratorCallback(node) {
|
|
2713
|
-
const callee = Extract.unwrap(node.callee);
|
|
2714
|
-
if (callee.type !== AST_NODE_TYPES.MemberExpression) return null;
|
|
2715
|
-
if (!Check.isIdentifier(callee.property)) return null;
|
|
2716
|
-
const position = KNOWN_CALLBACK_POSITIONS.get(callee.property.name);
|
|
2717
|
-
if (position == null) return null;
|
|
2718
|
-
const argument = node.arguments[position];
|
|
2719
|
-
if (argument == null) return null;
|
|
2720
|
-
const callback = Extract.unwrap(argument);
|
|
2721
|
-
if (!Check.isFunction(callback)) return null;
|
|
2722
|
-
return callback;
|
|
2723
|
-
}
|
|
2724
2806
|
var no_missing_key_default = createRule({
|
|
2725
2807
|
meta: {
|
|
2726
2808
|
type: "problem",
|
|
@@ -2731,7 +2813,7 @@ var no_missing_key_default = createRule({
|
|
|
2731
2813
|
},
|
|
2732
2814
|
schema: []
|
|
2733
2815
|
},
|
|
2734
|
-
name:
|
|
2816
|
+
name: RULE_NAME$24,
|
|
2735
2817
|
create: create$24
|
|
2736
2818
|
});
|
|
2737
2819
|
function create$24(context) {
|
|
@@ -2772,13 +2854,20 @@ function create$24(context) {
|
|
|
2772
2854
|
}
|
|
2773
2855
|
},
|
|
2774
2856
|
CallExpression(node) {
|
|
2857
|
+
const call = node;
|
|
2775
2858
|
if (core.isChildrenToArrayCall(context, node)) {
|
|
2776
2859
|
childrenToArrayDepth += 1;
|
|
2777
2860
|
return;
|
|
2778
2861
|
}
|
|
2779
2862
|
if (childrenToArrayDepth > 0) return;
|
|
2780
|
-
const
|
|
2781
|
-
if (
|
|
2863
|
+
const callee = Extract.unwrap(call.callee);
|
|
2864
|
+
if (callee.type !== AST_NODE_TYPES.MemberExpression || !Check.isIdentifier(callee.property)) return;
|
|
2865
|
+
const position = ITERATOR_CALLBACK_POSITIONS.get(callee.property.name);
|
|
2866
|
+
if (position == null) return;
|
|
2867
|
+
const argument = call.arguments[position];
|
|
2868
|
+
if (argument == null) return;
|
|
2869
|
+
const callback = Extract.unwrap(argument);
|
|
2870
|
+
if (!Check.isFunction(callback)) return;
|
|
2782
2871
|
for (const desc of visitIteratorCallback(callback)) context.report(desc);
|
|
2783
2872
|
},
|
|
2784
2873
|
"CallExpression:exit"(node) {
|
|
@@ -3305,8 +3394,7 @@ var no_unstable_default_props_default = createRule({
|
|
|
3305
3394
|
create: create$12
|
|
3306
3395
|
});
|
|
3307
3396
|
function create$12(context) {
|
|
3308
|
-
const
|
|
3309
|
-
const safeDefaultProps = options.safeDefaultProps ??= [];
|
|
3397
|
+
const safeDefaultProps = (context.options[0] ?? {}).safeDefaultProps ?? [];
|
|
3310
3398
|
const { compilationMode } = getSettingsFromContext(context);
|
|
3311
3399
|
if (compilationMode === "infer" || compilationMode === "all") return {};
|
|
3312
3400
|
if (compilationMode === "annotation" && context.sourceCode.ast.body.some((stmt) => Check.isDirective(stmt, "use memo"))) return {};
|
|
@@ -4071,16 +4159,23 @@ function getDestructuredPropertyName(pattern, name) {
|
|
|
4071
4159
|
for (const property of pattern.properties) {
|
|
4072
4160
|
if (property.type !== AST_NODE_TYPES.Property) continue;
|
|
4073
4161
|
if ((property.value.type === AST_NODE_TYPES.AssignmentPattern ? property.value.left : property.value) !== name) continue;
|
|
4074
|
-
|
|
4162
|
+
const key = Extract.unwrap(property.key);
|
|
4163
|
+
if (!property.computed && Check.isIdentifier(key)) return key.name;
|
|
4164
|
+
if (key.type === AST_NODE_TYPES.Literal && typeof key.value === "string") return key.value;
|
|
4165
|
+
if (key.type === AST_NODE_TYPES.TemplateLiteral && key.expressions.length === 0) return key.quasis[0]?.value.cooked ?? key.quasis[0]?.value.raw ?? null;
|
|
4166
|
+
return null;
|
|
4075
4167
|
}
|
|
4076
4168
|
return null;
|
|
4077
4169
|
}
|
|
4078
4170
|
function resolveBuiltinMember(context, node, seen = /* @__PURE__ */ new Set()) {
|
|
4079
|
-
|
|
4080
|
-
const
|
|
4081
|
-
|
|
4082
|
-
|
|
4083
|
-
|
|
4171
|
+
let rootId = Extract.unwrap(node);
|
|
4172
|
+
const segments = [];
|
|
4173
|
+
for (let current = rootId; current.type === AST_NODE_TYPES.MemberExpression; current = Extract.unwrap(current.object)) {
|
|
4174
|
+
if (current.computed) return null;
|
|
4175
|
+
segments.unshift(Extract.unwrap(current.property));
|
|
4176
|
+
rootId = Extract.unwrap(current.object);
|
|
4177
|
+
}
|
|
4178
|
+
if (!Check.isIdentifier(rootId)) return null;
|
|
4084
4179
|
if (segments.length === 0 || !segments.every((segment) => Check.isIdentifier(segment))) return null;
|
|
4085
4180
|
const resolved = resolveBuiltinObjectName(context, rootId, seen);
|
|
4086
4181
|
if (resolved == null) return null;
|
|
@@ -6093,7 +6188,7 @@ function isInsideTryCatch(node) {
|
|
|
6093
6188
|
}
|
|
6094
6189
|
return false;
|
|
6095
6190
|
}
|
|
6096
|
-
function getNodeWithoutReactNamespace
|
|
6191
|
+
function getNodeWithoutReactNamespace(node) {
|
|
6097
6192
|
if (node.type === "MemberExpression" && node.object.type === "Identifier" && node.object.name === "React" && node.property.type === "Identifier" && !node.computed) return node.property;
|
|
6098
6193
|
return node;
|
|
6099
6194
|
}
|
|
@@ -6102,7 +6197,7 @@ function isEffectIdentifier(node, additionalHooks) {
|
|
|
6102
6197
|
if (additionalHooks && node.type === "Identifier") return additionalHooks.test(node.name);
|
|
6103
6198
|
return false;
|
|
6104
6199
|
}
|
|
6105
|
-
function isUseEffectEventIdentifier
|
|
6200
|
+
function isUseEffectEventIdentifier(node) {
|
|
6106
6201
|
return node.type === "Identifier" && node.name === "useEffectEvent";
|
|
6107
6202
|
}
|
|
6108
6203
|
function useEffectEventError(fn, called) {
|
|
@@ -6112,7 +6207,7 @@ function useEffectEventError(fn, called) {
|
|
|
6112
6207
|
function isUseIdentifier(node) {
|
|
6113
6208
|
return isReactFunction(node, "use");
|
|
6114
6209
|
}
|
|
6115
|
-
const rule
|
|
6210
|
+
const rule = {
|
|
6116
6211
|
meta: {
|
|
6117
6212
|
type: "problem",
|
|
6118
6213
|
docs: {
|
|
@@ -6137,7 +6232,7 @@ const rule$1 = {
|
|
|
6137
6232
|
function recordAllUseEffectEventFunctions(scope) {
|
|
6138
6233
|
for (const reference of scope.references) {
|
|
6139
6234
|
const parent = reference.identifier.parent;
|
|
6140
|
-
if (parent?.type === "VariableDeclarator" && parent.init && parent.init.type === "CallExpression" && parent.init.callee && isUseEffectEventIdentifier
|
|
6235
|
+
if (parent?.type === "VariableDeclarator" && parent.init && parent.init.type === "CallExpression" && parent.init.callee && isUseEffectEventIdentifier(parent.init.callee)) {
|
|
6141
6236
|
if (reference.resolved === null) throw new Error("Unexpected null reference.resolved");
|
|
6142
6237
|
for (const ref of reference.resolved.references) if (ref !== reference) useEffectEventFunctions.add(ref.identifier);
|
|
6143
6238
|
}
|
|
@@ -6316,9 +6411,9 @@ const rule$1 = {
|
|
|
6316
6411
|
}
|
|
6317
6412
|
reactHooks.push(callee);
|
|
6318
6413
|
}
|
|
6319
|
-
const nodeWithoutNamespace = getNodeWithoutReactNamespace
|
|
6320
|
-
if ((isEffectIdentifier(nodeWithoutNamespace, additionalEffectHooks) || isUseEffectEventIdentifier
|
|
6321
|
-
if (isUseEffectEventIdentifier
|
|
6414
|
+
const nodeWithoutNamespace = getNodeWithoutReactNamespace(node.callee);
|
|
6415
|
+
if ((isEffectIdentifier(nodeWithoutNamespace, additionalEffectHooks) || isUseEffectEventIdentifier(nodeWithoutNamespace)) && node.arguments.length > 0) lastEffect = node;
|
|
6416
|
+
if (isUseEffectEventIdentifier(nodeWithoutNamespace) && node.parent?.type !== "VariableDeclarator" && node.parent?.type !== "ExpressionStatement") {
|
|
6322
6417
|
const message = useEffectEventError(null, false);
|
|
6323
6418
|
context.report({
|
|
6324
6419
|
node,
|
|
@@ -6664,6 +6759,9 @@ function createSetStateResolver$1(context, facts = EMPTY_FACTS) {
|
|
|
6664
6759
|
if (survivingCandidates.length > 0) return survivingCandidates.some((c) => c.previous != null && isInitializedFromRef(c.previous, refParams, seen) || isRefValue(c.value, refParams, seen, "initializer"));
|
|
6665
6760
|
return variable.defs.some((d) => d.type === DefinitionType.Parameter && (refParams?.has(d.name) === true || d.name.type === AST_NODE_TYPES.Identifier && isRefLikeName(d.name.name)));
|
|
6666
6761
|
}
|
|
6762
|
+
function isInitializedFromParameter(id) {
|
|
6763
|
+
return findVariable(context.sourceCode.getScope(id), id)?.defs.some((d) => d.type === DefinitionType.Parameter) ?? false;
|
|
6764
|
+
}
|
|
6667
6765
|
function isRefValue(node, refParams, seen, mode) {
|
|
6668
6766
|
const expression = Extract.unwrap(node);
|
|
6669
6767
|
if (isUseRefCall(expression)) return true;
|
|
@@ -6672,7 +6770,7 @@ function createSetStateResolver$1(context, facts = EMPTY_FACTS) {
|
|
|
6672
6770
|
case AST_NODE_TYPES.MemberExpression: {
|
|
6673
6771
|
const property = Extract.unwrap(expression.property);
|
|
6674
6772
|
const object = Extract.unwrap(expression.object);
|
|
6675
|
-
if (!expression.computed && Check.isIdentifier(property, "current") && object.type === AST_NODE_TYPES.Identifier &&
|
|
6773
|
+
if (!expression.computed && Check.isIdentifier(property, "current") && object.type === AST_NODE_TYPES.Identifier && isInitializedFromParameter(object)) return true;
|
|
6676
6774
|
if (mode === "initializer") {
|
|
6677
6775
|
let current = expression;
|
|
6678
6776
|
while (current.type === AST_NODE_TYPES.MemberExpression) {
|
|
@@ -6681,7 +6779,7 @@ function createSetStateResolver$1(context, facts = EMPTY_FACTS) {
|
|
|
6681
6779
|
if (!current.computed && Check.isIdentifier(currentProperty, "current")) {
|
|
6682
6780
|
const sourceName = getRefSourceName(currentObject);
|
|
6683
6781
|
if (sourceName != null && isRefLikeName(sourceName)) return true;
|
|
6684
|
-
if (currentObject.type === AST_NODE_TYPES.Identifier &&
|
|
6782
|
+
if (currentObject.type === AST_NODE_TYPES.Identifier && isInitializedFromParameter(currentObject)) return true;
|
|
6685
6783
|
}
|
|
6686
6784
|
current = currentObject;
|
|
6687
6785
|
}
|
|
@@ -6782,9 +6880,6 @@ function createSetStateResolver$1(context, facts = EMPTY_FACTS) {
|
|
|
6782
6880
|
}
|
|
6783
6881
|
return false;
|
|
6784
6882
|
}
|
|
6785
|
-
function isParameterId(id) {
|
|
6786
|
-
return findVariable(context.sourceCode.getScope(id), id)?.defs.some((d) => d.type === DefinitionType.Parameter) ?? false;
|
|
6787
|
-
}
|
|
6788
6883
|
return {
|
|
6789
6884
|
getRefDerivedParams,
|
|
6790
6885
|
getSetterName,
|
|
@@ -7132,9 +7227,7 @@ function inferCreatedComponents(context, componentUsages, isInsideRender) {
|
|
|
7132
7227
|
for (const usage of componentUsages) {
|
|
7133
7228
|
const variable = findVariable(context.sourceCode.getScope(usage.node), usage.name);
|
|
7134
7229
|
if (variable == null) continue;
|
|
7135
|
-
|
|
7136
|
-
if (def == null) continue;
|
|
7137
|
-
if (!isInsideRender(def.node)) continue;
|
|
7230
|
+
if (!variable.defs.some((def) => isInsideRender(def.node))) continue;
|
|
7138
7231
|
const { creationNode, isDynamic } = resolveDynamicComponentOrigin(context, variable, isInsideRender);
|
|
7139
7232
|
if (!isDynamic) continue;
|
|
7140
7233
|
effects.push({
|
|
@@ -7171,7 +7264,6 @@ function create$3(context) {
|
|
|
7171
7264
|
messageId: "default",
|
|
7172
7265
|
node: effect.node
|
|
7173
7266
|
});
|
|
7174
|
-
if (effect.creationNode == null) continue;
|
|
7175
7267
|
if (reportedCreations.has(effect.creationNode)) continue;
|
|
7176
7268
|
reportedCreations.add(effect.creationNode);
|
|
7177
7269
|
context.report({
|
|
@@ -7181,1008 +7273,6 @@ function create$3(context) {
|
|
|
7181
7273
|
}
|
|
7182
7274
|
} });
|
|
7183
7275
|
}
|
|
7184
|
-
// src/rules/unstable-exhaustive-deps/lib.ts
|
|
7185
|
-
function getHookConfigs(hooks = []) {
|
|
7186
|
-
const reactive = /* @__PURE__ */ new Map();
|
|
7187
|
-
const stable = /* @__PURE__ */ new Map();
|
|
7188
|
-
for (const { name, closureIndex, dependenciesIndex, stableResult } of hooks) {
|
|
7189
|
-
if (closureIndex != null && dependenciesIndex != null && closureIndex !== dependenciesIndex) reactive.set(name, {
|
|
7190
|
-
closureIndex,
|
|
7191
|
-
dependenciesIndex
|
|
7192
|
-
});
|
|
7193
|
-
if (stableResult === true) stable.set(name, true);
|
|
7194
|
-
else if (typeof stableResult === "number") stable.set(name, /* @__PURE__ */ new Set([stableResult]));
|
|
7195
|
-
else if (Array.isArray(stableResult)) stable.set(name, new Set(stableResult));
|
|
7196
|
-
}
|
|
7197
|
-
return {
|
|
7198
|
-
reactive,
|
|
7199
|
-
stable
|
|
7200
|
-
};
|
|
7201
|
-
}
|
|
7202
|
-
function getReactiveHookConfig(node, configs) {
|
|
7203
|
-
if (configs.reactive.size === 0) return null;
|
|
7204
|
-
const name = Extract.getCalleeName(node);
|
|
7205
|
-
if (name == null) return null;
|
|
7206
|
-
const config = configs.reactive.get(name);
|
|
7207
|
-
if (config == null) return null;
|
|
7208
|
-
return {
|
|
7209
|
-
name,
|
|
7210
|
-
...config
|
|
7211
|
-
};
|
|
7212
|
-
}
|
|
7213
|
-
function isConfiguredStableHookValue(variable, configs) {
|
|
7214
|
-
if (configs.stable.size === 0) return false;
|
|
7215
|
-
let matchedNode = null;
|
|
7216
|
-
let stableResult = null;
|
|
7217
|
-
let initializerCount = 0;
|
|
7218
|
-
for (const def of variable.defs) {
|
|
7219
|
-
if (def.type !== DefinitionType.Variable || def.node.init == null) continue;
|
|
7220
|
-
initializerCount++;
|
|
7221
|
-
const init = Extract.unwrap(def.node.init);
|
|
7222
|
-
if (init.type !== AST_NODE_TYPES.CallExpression) continue;
|
|
7223
|
-
const name = Extract.getCalleeName(init);
|
|
7224
|
-
if (name == null) continue;
|
|
7225
|
-
const config = configs.stable.get(name);
|
|
7226
|
-
if (config == null) continue;
|
|
7227
|
-
if (matchedNode != null) return false;
|
|
7228
|
-
matchedNode = def.node;
|
|
7229
|
-
stableResult = config;
|
|
7230
|
-
}
|
|
7231
|
-
if (initializerCount !== 1) return false;
|
|
7232
|
-
if (matchedNode == null || stableResult == null) return false;
|
|
7233
|
-
if (variable.references.some((reference) => reference.isWrite() && reference.init !== true)) return false;
|
|
7234
|
-
const { id } = matchedNode;
|
|
7235
|
-
if (stableResult === true) return id.type === AST_NODE_TYPES.Identifier;
|
|
7236
|
-
if (id.type !== AST_NODE_TYPES.ArrayPattern) return false;
|
|
7237
|
-
const index = id.elements.findIndex((element) => {
|
|
7238
|
-
if (element == null) return false;
|
|
7239
|
-
const binding = element.type === AST_NODE_TYPES.AssignmentPattern ? element.left : element;
|
|
7240
|
-
return variable.identifiers.some((identifier) => identifier === binding);
|
|
7241
|
-
});
|
|
7242
|
-
return index !== -1 && stableResult.has(index);
|
|
7243
|
-
}
|
|
7244
|
-
// src/rules/unstable-exhaustive-deps/unstable-exhaustive-deps.ts
|
|
7245
|
-
const rule = {
|
|
7246
|
-
meta: {
|
|
7247
|
-
type: "suggestion",
|
|
7248
|
-
docs: {
|
|
7249
|
-
description: "Verifies the list of dependencies for Hooks like 'useEffect' and similar, with support for custom Hooks and stable Hook results.",
|
|
7250
|
-
url: "https://eslint-react.xyz/docs/rules/unstable-exhaustive-deps"
|
|
7251
|
-
},
|
|
7252
|
-
fixable: "code",
|
|
7253
|
-
hasSuggestions: true,
|
|
7254
|
-
schema: [{
|
|
7255
|
-
type: "object",
|
|
7256
|
-
additionalProperties: false,
|
|
7257
|
-
enableDangerousAutofixThisMayCauseInfiniteLoops: false,
|
|
7258
|
-
properties: {
|
|
7259
|
-
additionalHooks: { type: "string" },
|
|
7260
|
-
enableDangerousAutofixThisMayCauseInfiniteLoops: { type: "boolean" },
|
|
7261
|
-
experimental_autoDependenciesHooks: {
|
|
7262
|
-
type: "array",
|
|
7263
|
-
items: { type: "string" }
|
|
7264
|
-
},
|
|
7265
|
-
hooks: {
|
|
7266
|
-
type: "array",
|
|
7267
|
-
items: {
|
|
7268
|
-
type: "object",
|
|
7269
|
-
additionalProperties: false,
|
|
7270
|
-
dependencies: {
|
|
7271
|
-
closureIndex: ["dependenciesIndex"],
|
|
7272
|
-
dependenciesIndex: ["closureIndex"]
|
|
7273
|
-
},
|
|
7274
|
-
properties: {
|
|
7275
|
-
name: {
|
|
7276
|
-
type: "string",
|
|
7277
|
-
minLength: 1
|
|
7278
|
-
},
|
|
7279
|
-
closureIndex: {
|
|
7280
|
-
type: "integer",
|
|
7281
|
-
minimum: 0
|
|
7282
|
-
},
|
|
7283
|
-
dependenciesIndex: {
|
|
7284
|
-
type: "integer",
|
|
7285
|
-
minimum: 0
|
|
7286
|
-
},
|
|
7287
|
-
stableResult: { oneOf: [
|
|
7288
|
-
{ type: "boolean" },
|
|
7289
|
-
{
|
|
7290
|
-
type: "integer",
|
|
7291
|
-
minimum: 0
|
|
7292
|
-
},
|
|
7293
|
-
{
|
|
7294
|
-
type: "array",
|
|
7295
|
-
items: {
|
|
7296
|
-
type: "integer",
|
|
7297
|
-
minimum: 0
|
|
7298
|
-
},
|
|
7299
|
-
minItems: 1
|
|
7300
|
-
}
|
|
7301
|
-
] }
|
|
7302
|
-
},
|
|
7303
|
-
required: ["name"]
|
|
7304
|
-
}
|
|
7305
|
-
},
|
|
7306
|
-
requireExplicitEffectDeps: { type: "boolean" }
|
|
7307
|
-
}
|
|
7308
|
-
}]
|
|
7309
|
-
},
|
|
7310
|
-
create(context) {
|
|
7311
|
-
const rawOptions = context.options && context.options[0];
|
|
7312
|
-
const additionalHooks = rawOptions && rawOptions.additionalHooks ? new RegExp(rawOptions.additionalHooks) : getSettingsFromContext(context).additionalEffectHooks;
|
|
7313
|
-
const enableDangerousAutofixThisMayCauseInfiniteLoops = rawOptions && rawOptions.enableDangerousAutofixThisMayCauseInfiniteLoops || false;
|
|
7314
|
-
const experimental_autoDependenciesHooks = rawOptions && Array.isArray(rawOptions.experimental_autoDependenciesHooks) ? rawOptions.experimental_autoDependenciesHooks : [];
|
|
7315
|
-
const requireExplicitEffectDeps = rawOptions && rawOptions.requireExplicitEffectDeps || false;
|
|
7316
|
-
const options = {
|
|
7317
|
-
additionalHooks,
|
|
7318
|
-
experimental_autoDependenciesHooks,
|
|
7319
|
-
enableDangerousAutofixThisMayCauseInfiniteLoops,
|
|
7320
|
-
hooks: getHookConfigs(rawOptions && rawOptions.hooks),
|
|
7321
|
-
requireExplicitEffectDeps
|
|
7322
|
-
};
|
|
7323
|
-
function reportProblem(problem) {
|
|
7324
|
-
if (enableDangerousAutofixThisMayCauseInfiniteLoops) {
|
|
7325
|
-
if (Array.isArray(problem.suggest) && problem.suggest.length > 0 && problem.suggest[0]) problem.fix = problem.suggest[0].fix;
|
|
7326
|
-
}
|
|
7327
|
-
context.report(problem);
|
|
7328
|
-
}
|
|
7329
|
-
const getSourceCode = typeof context.getSourceCode === "function" ? () => {
|
|
7330
|
-
return context.getSourceCode();
|
|
7331
|
-
} : () => {
|
|
7332
|
-
return context.sourceCode;
|
|
7333
|
-
};
|
|
7334
|
-
const getScope = typeof context.getScope === "function" ? () => {
|
|
7335
|
-
return context.getScope();
|
|
7336
|
-
} : (node) => {
|
|
7337
|
-
return context.sourceCode.getScope(node);
|
|
7338
|
-
};
|
|
7339
|
-
const scopeManager = getSourceCode().scopeManager;
|
|
7340
|
-
const setStateCallSites = /* @__PURE__ */ new WeakMap();
|
|
7341
|
-
const stateVariables = /* @__PURE__ */ new WeakSet();
|
|
7342
|
-
const stableKnownValueCache = /* @__PURE__ */ new WeakMap();
|
|
7343
|
-
const functionWithoutCapturedValueCache = /* @__PURE__ */ new WeakMap();
|
|
7344
|
-
const useEffectEventVariables = /* @__PURE__ */ new WeakSet();
|
|
7345
|
-
function memoizeWithWeakMap(fn, map) {
|
|
7346
|
-
return function(arg) {
|
|
7347
|
-
if (map.has(arg)) return map.get(arg);
|
|
7348
|
-
const result = fn(arg);
|
|
7349
|
-
map.set(arg, result);
|
|
7350
|
-
return result;
|
|
7351
|
-
};
|
|
7352
|
-
}
|
|
7353
|
-
function visitFunctionWithDependencies(node, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect, isAutoDepsHook, callExpression, dependenciesIndex) {
|
|
7354
|
-
if (isEffect && node.async) reportProblem({
|
|
7355
|
-
node,
|
|
7356
|
-
message: "Effect callbacks are synchronous to prevent race conditions. Put the async function inside:\n\nuseEffect(() => {\n async function fetchData() {\n // You can await here\n const response = await MyAPI.getData(someId);\n // ...\n }\n fetchData();\n}, [someId]); // Or [] if effect doesn't need props or state\n\nLearn more about data fetching with Hooks: https://react.dev/link/hooks-data-fetching"
|
|
7357
|
-
});
|
|
7358
|
-
const scope = scopeManager.acquire(node);
|
|
7359
|
-
if (!scope) throw new Error("Unable to acquire scope for the current node. This is a bug in eslint-plugin-react-hooks, please file an issue.");
|
|
7360
|
-
const pureScopes = /* @__PURE__ */ new Set();
|
|
7361
|
-
let componentScope = null;
|
|
7362
|
-
{
|
|
7363
|
-
let currentScope = scope.upper;
|
|
7364
|
-
while (currentScope) {
|
|
7365
|
-
pureScopes.add(currentScope);
|
|
7366
|
-
if (currentScope.type === "function" || currentScope.type === "hook" || currentScope.type === "component") break;
|
|
7367
|
-
currentScope = currentScope.upper;
|
|
7368
|
-
}
|
|
7369
|
-
if (!currentScope) return;
|
|
7370
|
-
componentScope = currentScope;
|
|
7371
|
-
}
|
|
7372
|
-
const isArray = Array.isArray;
|
|
7373
|
-
function isStableKnownHookValue(resolved) {
|
|
7374
|
-
if (!isArray(resolved.defs)) return false;
|
|
7375
|
-
const initializers = resolved.defs.filter((definition) => definition.type === "Variable" && definition.node.type === "VariableDeclarator" && definition.node.init != null);
|
|
7376
|
-
if (initializers.length !== 1) return false;
|
|
7377
|
-
const def = initializers[0];
|
|
7378
|
-
const defNode = def.node;
|
|
7379
|
-
if (defNode.type !== "VariableDeclarator") return false;
|
|
7380
|
-
let init = defNode.init;
|
|
7381
|
-
if (init == null) return false;
|
|
7382
|
-
while (init.type === "TSAsExpression" || init.type === "AsExpression") init = init.expression;
|
|
7383
|
-
let declaration = defNode.parent;
|
|
7384
|
-
if (declaration == null && componentScope != null) {
|
|
7385
|
-
fastFindReferenceWithParent(componentScope.block, def.node.id);
|
|
7386
|
-
declaration = def.node.parent;
|
|
7387
|
-
if (declaration == null) return false;
|
|
7388
|
-
}
|
|
7389
|
-
if (declaration != null && "kind" in declaration && declaration.kind === "const" && init.type === "Literal" && (typeof init.value === "string" || typeof init.value === "number" || init.value === null)) return true;
|
|
7390
|
-
if (init.type !== "CallExpression") return false;
|
|
7391
|
-
let callee = init.callee;
|
|
7392
|
-
if (callee.type === "MemberExpression" && "name" in callee.object && callee.object.name === "React" && callee.property != null && !callee.computed) callee = callee.property;
|
|
7393
|
-
if (callee.type !== "Identifier") return false;
|
|
7394
|
-
const id = def.node.id;
|
|
7395
|
-
const { name } = callee;
|
|
7396
|
-
if (name === "useRef" && id.type === "Identifier") return true;
|
|
7397
|
-
else if (isUseEffectEventIdentifier(callee) && id.type === "Identifier") {
|
|
7398
|
-
for (const ref of resolved.references) if (ref !== id) useEffectEventVariables.add(ref.identifier);
|
|
7399
|
-
return true;
|
|
7400
|
-
} else if (name === "useState" || name === "useReducer" || name === "useActionState") {
|
|
7401
|
-
const isActionState = name === "useActionState";
|
|
7402
|
-
if (id.type === "ArrayPattern" && (isActionState ? id.elements.length >= 2 : id.elements.length === 2) && isArray(resolved.identifiers)) {
|
|
7403
|
-
const firstNode = id.elements[0] ?? null;
|
|
7404
|
-
const secondNode = id.elements[1] ?? null;
|
|
7405
|
-
const firstElement = firstNode?.type === "AssignmentPattern" ? firstNode.left : firstNode;
|
|
7406
|
-
const secondElement = secondNode?.type === "AssignmentPattern" ? secondNode.left : secondNode;
|
|
7407
|
-
if (resolved.identifiers.some((identifier) => identifier === secondElement)) {
|
|
7408
|
-
if (name === "useState") {
|
|
7409
|
-
const references = resolved.references;
|
|
7410
|
-
for (const reference of references) {
|
|
7411
|
-
if (reference.isWrite() && reference.init !== true) return false;
|
|
7412
|
-
setStateCallSites.set(reference.identifier, firstElement);
|
|
7413
|
-
}
|
|
7414
|
-
}
|
|
7415
|
-
return true;
|
|
7416
|
-
} else if (resolved.identifiers.some((identifier) => identifier === firstElement)) {
|
|
7417
|
-
if (name === "useState") {
|
|
7418
|
-
const references = resolved.references;
|
|
7419
|
-
for (const reference of references) stateVariables.add(reference.identifier);
|
|
7420
|
-
}
|
|
7421
|
-
return false;
|
|
7422
|
-
}
|
|
7423
|
-
}
|
|
7424
|
-
} else if (name === "useTransition") {
|
|
7425
|
-
if (id.type === "ArrayPattern" && id.elements.length === 2 && Array.isArray(resolved.identifiers)) {
|
|
7426
|
-
const secondNode = id.elements[1] ?? null;
|
|
7427
|
-
const secondElement = secondNode?.type === "AssignmentPattern" ? secondNode.left : secondNode;
|
|
7428
|
-
if (resolved.identifiers.some((identifier) => identifier === secondElement)) return true;
|
|
7429
|
-
}
|
|
7430
|
-
}
|
|
7431
|
-
return false;
|
|
7432
|
-
}
|
|
7433
|
-
function isFunctionWithoutCapturedValues(resolved) {
|
|
7434
|
-
if (!isArray(resolved.defs)) return false;
|
|
7435
|
-
const def = resolved.defs[0];
|
|
7436
|
-
if (def == null) return false;
|
|
7437
|
-
if (def.node == null || def.node.id == null) return false;
|
|
7438
|
-
const fnNode = def.node;
|
|
7439
|
-
const childScopes = componentScope?.childScopes || [];
|
|
7440
|
-
let fnScope = null;
|
|
7441
|
-
for (const childScope of childScopes) {
|
|
7442
|
-
const childScopeBlock = childScope.block;
|
|
7443
|
-
if (fnNode.type === "FunctionDeclaration" && childScopeBlock === fnNode || fnNode.type === "VariableDeclarator" && childScopeBlock.parent === fnNode) {
|
|
7444
|
-
fnScope = childScope;
|
|
7445
|
-
break;
|
|
7446
|
-
}
|
|
7447
|
-
}
|
|
7448
|
-
if (fnScope == null) return false;
|
|
7449
|
-
for (const ref of fnScope.through) {
|
|
7450
|
-
if (ref.resolved == null) continue;
|
|
7451
|
-
if (pureScopes.has(ref.resolved.scope) && !memoizedIsStableKnownHookValue(ref.resolved)) return false;
|
|
7452
|
-
}
|
|
7453
|
-
return true;
|
|
7454
|
-
}
|
|
7455
|
-
const memoizedIsStableKnownHookValue = memoizeWithWeakMap((resolved) => isStableKnownHookValue(resolved) || isConfiguredStableHookValue(resolved, options.hooks), stableKnownValueCache);
|
|
7456
|
-
const memoizedIsFunctionWithoutCapturedValues = memoizeWithWeakMap(isFunctionWithoutCapturedValues, functionWithoutCapturedValueCache);
|
|
7457
|
-
const currentRefsInEffectCleanup = /* @__PURE__ */ new Map();
|
|
7458
|
-
function isInsideEffectCleanup(reference) {
|
|
7459
|
-
let curScope = reference.from;
|
|
7460
|
-
let isInReturnedFunction = false;
|
|
7461
|
-
while (curScope != null && curScope.block !== node) {
|
|
7462
|
-
if (curScope.type === "function") isInReturnedFunction = curScope.block.parent != null && curScope.block.parent.type === "ReturnStatement";
|
|
7463
|
-
curScope = curScope.upper;
|
|
7464
|
-
}
|
|
7465
|
-
return isInReturnedFunction;
|
|
7466
|
-
}
|
|
7467
|
-
const dependencies = /* @__PURE__ */ new Map();
|
|
7468
|
-
const optionalChains = /* @__PURE__ */ new Map();
|
|
7469
|
-
gatherDependenciesRecursively(scope);
|
|
7470
|
-
function gatherDependenciesRecursively(currentScope) {
|
|
7471
|
-
for (const reference of currentScope.references) {
|
|
7472
|
-
if (!reference.resolved) continue;
|
|
7473
|
-
if (!pureScopes.has(reference.resolved.scope)) continue;
|
|
7474
|
-
const referenceNode = fastFindReferenceWithParent(node, reference.identifier);
|
|
7475
|
-
if (referenceNode == null) continue;
|
|
7476
|
-
const dependencyNode = getDependency(referenceNode);
|
|
7477
|
-
const dependency = analyzePropertyChain(dependencyNode, optionalChains);
|
|
7478
|
-
if (isEffect && dependencyNode.type === "Identifier" && (dependencyNode.parent?.type === "MemberExpression" || dependencyNode.parent?.type === "OptionalMemberExpression") && !dependencyNode.parent.computed && dependencyNode.parent.property.type === "Identifier" && dependencyNode.parent.property.name === "current" && isInsideEffectCleanup(reference)) currentRefsInEffectCleanup.set(dependency, {
|
|
7479
|
-
reference,
|
|
7480
|
-
dependencyNode
|
|
7481
|
-
});
|
|
7482
|
-
if (dependencyNode.parent?.type === "TSTypeQuery" || dependencyNode.parent?.type === "TSTypeReference") continue;
|
|
7483
|
-
const def = reference.resolved.defs[0];
|
|
7484
|
-
if (def == null) continue;
|
|
7485
|
-
if (def.node != null && def.node.init === node.parent) continue;
|
|
7486
|
-
if (def.type === "TypeParameter" || dependencyNode.parent?.type === "GenericTypeAnnotation") continue;
|
|
7487
|
-
if (!dependencies.has(dependency)) {
|
|
7488
|
-
const resolved = reference.resolved;
|
|
7489
|
-
const isStable = memoizedIsStableKnownHookValue(resolved) || memoizedIsFunctionWithoutCapturedValues(resolved);
|
|
7490
|
-
dependencies.set(dependency, {
|
|
7491
|
-
isStable,
|
|
7492
|
-
references: [reference]
|
|
7493
|
-
});
|
|
7494
|
-
} else dependencies.get(dependency)?.references.push(reference);
|
|
7495
|
-
}
|
|
7496
|
-
for (const childScope of currentScope.childScopes) gatherDependenciesRecursively(childScope);
|
|
7497
|
-
}
|
|
7498
|
-
currentRefsInEffectCleanup.forEach(({ reference, dependencyNode }, dependency) => {
|
|
7499
|
-
const references = reference.resolved?.references || [];
|
|
7500
|
-
let foundCurrentAssignment = false;
|
|
7501
|
-
for (const ref of references) {
|
|
7502
|
-
const { identifier } = ref;
|
|
7503
|
-
const { parent } = identifier;
|
|
7504
|
-
if (parent != null && parent.type === "MemberExpression" && !parent.computed && parent.property.type === "Identifier" && parent.property.name === "current" && parent.parent?.type === "AssignmentExpression" && parent.parent.left === parent) {
|
|
7505
|
-
foundCurrentAssignment = true;
|
|
7506
|
-
break;
|
|
7507
|
-
}
|
|
7508
|
-
}
|
|
7509
|
-
if (foundCurrentAssignment) return;
|
|
7510
|
-
reportProblem({
|
|
7511
|
-
node: dependencyNode.parent.property,
|
|
7512
|
-
message: `The ref value '${dependency}.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy '${dependency}.current' to a variable inside the effect, and use that variable in the cleanup function.`
|
|
7513
|
-
});
|
|
7514
|
-
});
|
|
7515
|
-
const staleAssignments = /* @__PURE__ */ new Set();
|
|
7516
|
-
function reportStaleAssignment(writeExpr, key) {
|
|
7517
|
-
if (staleAssignments.has(key)) return;
|
|
7518
|
-
staleAssignments.add(key);
|
|
7519
|
-
reportProblem({
|
|
7520
|
-
node: writeExpr,
|
|
7521
|
-
message: `Assignments to the '${key}' variable from inside React Hook ${getSourceCode().getText(reactiveHook)} will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside ${getSourceCode().getText(reactiveHook)}.`
|
|
7522
|
-
});
|
|
7523
|
-
}
|
|
7524
|
-
const stableDependencies = /* @__PURE__ */ new Set();
|
|
7525
|
-
dependencies.forEach(({ isStable, references }, key) => {
|
|
7526
|
-
if (isStable) stableDependencies.add(key);
|
|
7527
|
-
references.forEach((reference) => {
|
|
7528
|
-
if (reference.writeExpr) reportStaleAssignment(reference.writeExpr, key);
|
|
7529
|
-
});
|
|
7530
|
-
});
|
|
7531
|
-
if (staleAssignments.size > 0) return;
|
|
7532
|
-
if (!declaredDependenciesNode) {
|
|
7533
|
-
if (isAutoDepsHook) return;
|
|
7534
|
-
let setStateInsideEffectWithoutDeps = null;
|
|
7535
|
-
dependencies.forEach(({ references }, key) => {
|
|
7536
|
-
if (setStateInsideEffectWithoutDeps) return;
|
|
7537
|
-
references.forEach((reference) => {
|
|
7538
|
-
if (setStateInsideEffectWithoutDeps) return;
|
|
7539
|
-
const id = reference.identifier;
|
|
7540
|
-
if (!setStateCallSites.has(id)) return;
|
|
7541
|
-
let fnScope = reference.from;
|
|
7542
|
-
while (fnScope != null && fnScope.type !== "function") fnScope = fnScope.upper;
|
|
7543
|
-
if (fnScope?.block === node) setStateInsideEffectWithoutDeps = key;
|
|
7544
|
-
});
|
|
7545
|
-
});
|
|
7546
|
-
if (setStateInsideEffectWithoutDeps) {
|
|
7547
|
-
const { suggestedDependencies } = collectRecommendations({
|
|
7548
|
-
dependencies,
|
|
7549
|
-
declaredDependencies: [],
|
|
7550
|
-
stableDependencies,
|
|
7551
|
-
externalDependencies: /* @__PURE__ */ new Set(),
|
|
7552
|
-
isEffect: true
|
|
7553
|
-
});
|
|
7554
|
-
const suggestedDependenciesText = `[${suggestedDependencies.join(", ")}]`;
|
|
7555
|
-
const dependenciesArgument = callExpression.arguments[dependenciesIndex];
|
|
7556
|
-
const hasSpreadArgumentBeforeOrAtDependencies = callExpression.arguments.slice(0, dependenciesIndex + 1).some((argument) => argument?.type === "SpreadElement");
|
|
7557
|
-
let fix = null;
|
|
7558
|
-
if (!hasSpreadArgumentBeforeOrAtDependencies) {
|
|
7559
|
-
if (dependenciesArgument?.type === "Identifier" && dependenciesArgument.name === "undefined") fix = (fixer) => fixer.replaceText(dependenciesArgument, suggestedDependenciesText);
|
|
7560
|
-
else if (dependenciesArgument == null && dependenciesIndex === callExpression.arguments.length) {
|
|
7561
|
-
const closeParen = getSourceCode().getLastToken(callExpression);
|
|
7562
|
-
if (closeParen != null && closeParen.value === ")") {
|
|
7563
|
-
const tokenBeforeCloseParen = getSourceCode().getTokenBefore(closeParen);
|
|
7564
|
-
fix = tokenBeforeCloseParen?.value === "," ? (fixer) => fixer.insertTextAfter(tokenBeforeCloseParen, ` ${suggestedDependenciesText}`) : (fixer) => fixer.insertTextBefore(closeParen, `, ${suggestedDependenciesText}`);
|
|
7565
|
-
}
|
|
7566
|
-
}
|
|
7567
|
-
}
|
|
7568
|
-
const dependenciesArgumentText = dependenciesIndex === 1 ? "as a second argument" : `as the dependencies argument (index ${dependenciesIndex})`;
|
|
7569
|
-
const problem = {
|
|
7570
|
-
node: reactiveHook,
|
|
7571
|
-
message: `React Hook ${reactiveHookName} contains a call to '${setStateInsideEffectWithoutDeps}'. Without a list of dependencies, this can lead to an infinite chain of updates. To fix this, pass ${suggestedDependenciesText} ${dependenciesArgumentText} to the ${reactiveHookName} Hook.`
|
|
7572
|
-
};
|
|
7573
|
-
if (fix != null) problem.suggest = [{
|
|
7574
|
-
desc: `Add dependencies array: ${suggestedDependenciesText}`,
|
|
7575
|
-
fix
|
|
7576
|
-
}];
|
|
7577
|
-
reportProblem(problem);
|
|
7578
|
-
}
|
|
7579
|
-
return;
|
|
7580
|
-
}
|
|
7581
|
-
if (isAutoDepsHook && declaredDependenciesNode.type === "Literal" && declaredDependenciesNode.value === null) return;
|
|
7582
|
-
const declaredDependencies = [];
|
|
7583
|
-
const externalDependencies = /* @__PURE__ */ new Set();
|
|
7584
|
-
const isArrayExpression = declaredDependenciesNode.type === "ArrayExpression";
|
|
7585
|
-
const isTSAsArrayExpression = declaredDependenciesNode.type === "TSAsExpression" && declaredDependenciesNode.expression.type === "ArrayExpression";
|
|
7586
|
-
if (!isArrayExpression && !isTSAsArrayExpression) reportProblem({
|
|
7587
|
-
node: declaredDependenciesNode,
|
|
7588
|
-
message: `React Hook ${getSourceCode().getText(reactiveHook)} was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies.`
|
|
7589
|
-
});
|
|
7590
|
-
else (isTSAsArrayExpression ? declaredDependenciesNode.expression : declaredDependenciesNode).elements.forEach((declaredDependencyNode) => {
|
|
7591
|
-
if (declaredDependencyNode === null) return;
|
|
7592
|
-
if (declaredDependencyNode.type === "SpreadElement") {
|
|
7593
|
-
reportProblem({
|
|
7594
|
-
node: declaredDependencyNode,
|
|
7595
|
-
message: `React Hook ${getSourceCode().getText(reactiveHook)} has a spread element in its dependency array. This means we can't statically verify whether you've passed the correct dependencies.`
|
|
7596
|
-
});
|
|
7597
|
-
return;
|
|
7598
|
-
}
|
|
7599
|
-
if (useEffectEventVariables.has(declaredDependencyNode)) reportProblem({
|
|
7600
|
-
node: declaredDependencyNode,
|
|
7601
|
-
message: `Functions returned from \`useEffectEvent\` must not be included in the dependency array. Remove \`${getSourceCode().getText(declaredDependencyNode)}\` from the list.`,
|
|
7602
|
-
suggest: [{
|
|
7603
|
-
desc: `Remove the dependency \`${getSourceCode().getText(declaredDependencyNode)}\``,
|
|
7604
|
-
fix(fixer) {
|
|
7605
|
-
return fixer.removeRange(declaredDependencyNode.range);
|
|
7606
|
-
}
|
|
7607
|
-
}]
|
|
7608
|
-
});
|
|
7609
|
-
let declaredDependency;
|
|
7610
|
-
try {
|
|
7611
|
-
declaredDependency = analyzePropertyChain(declaredDependencyNode, null);
|
|
7612
|
-
} catch (error) {
|
|
7613
|
-
if (error instanceof Error && /Unsupported node type/.test(error.message)) {
|
|
7614
|
-
if (declaredDependencyNode.type === "Literal") {
|
|
7615
|
-
if (declaredDependencyNode.value && dependencies.has(declaredDependencyNode.value)) reportProblem({
|
|
7616
|
-
node: declaredDependencyNode,
|
|
7617
|
-
message: `The ${declaredDependencyNode.raw} literal is not a valid dependency because it never changes. Did you mean to include ${declaredDependencyNode.value} in the array instead?`
|
|
7618
|
-
});
|
|
7619
|
-
else reportProblem({
|
|
7620
|
-
node: declaredDependencyNode,
|
|
7621
|
-
message: `The ${declaredDependencyNode.raw} literal is not a valid dependency because it never changes. You can safely remove it.`
|
|
7622
|
-
});
|
|
7623
|
-
} else reportProblem({
|
|
7624
|
-
node: declaredDependencyNode,
|
|
7625
|
-
message: `React Hook ${getSourceCode().getText(reactiveHook)} has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked.`
|
|
7626
|
-
});
|
|
7627
|
-
return;
|
|
7628
|
-
} else throw error;
|
|
7629
|
-
}
|
|
7630
|
-
let maybeID = declaredDependencyNode;
|
|
7631
|
-
while (maybeID.type === "MemberExpression" || maybeID.type === "OptionalMemberExpression" || maybeID.type === "ChainExpression") maybeID = maybeID.object || maybeID.expression.object;
|
|
7632
|
-
const isDeclaredInComponent = !componentScope.through.some((ref) => ref.identifier === maybeID);
|
|
7633
|
-
declaredDependencies.push({
|
|
7634
|
-
key: declaredDependency,
|
|
7635
|
-
node: declaredDependencyNode
|
|
7636
|
-
});
|
|
7637
|
-
if (!isDeclaredInComponent) externalDependencies.add(declaredDependency);
|
|
7638
|
-
});
|
|
7639
|
-
const { suggestedDependencies, unnecessaryDependencies, missingDependencies, duplicateDependencies } = collectRecommendations({
|
|
7640
|
-
dependencies,
|
|
7641
|
-
declaredDependencies,
|
|
7642
|
-
stableDependencies,
|
|
7643
|
-
externalDependencies,
|
|
7644
|
-
isEffect
|
|
7645
|
-
});
|
|
7646
|
-
let suggestedDeps = suggestedDependencies;
|
|
7647
|
-
if (duplicateDependencies.size + missingDependencies.size + unnecessaryDependencies.size === 0) {
|
|
7648
|
-
scanForConstructions({
|
|
7649
|
-
declaredDependencies,
|
|
7650
|
-
declaredDependenciesNode,
|
|
7651
|
-
componentScope,
|
|
7652
|
-
scope
|
|
7653
|
-
}).forEach(({ construction, isUsedOutsideOfHook, depType }) => {
|
|
7654
|
-
const wrapperHook = depType === "function" ? "useCallback" : "useMemo";
|
|
7655
|
-
const constructionType = depType === "function" ? "definition" : "initialization";
|
|
7656
|
-
const defaultAdvice = `wrap the ${constructionType} of '${construction.name.name}' in its own ${wrapperHook}() Hook.`;
|
|
7657
|
-
const advice = isUsedOutsideOfHook ? `To fix this, ${defaultAdvice}` : `Move it inside the ${reactiveHookName} callback. Alternatively, ${defaultAdvice}`;
|
|
7658
|
-
const causation = depType === "conditional" || depType === "logical expression" ? "could make" : "makes";
|
|
7659
|
-
const message = `The '${construction.name.name}' ${depType} ${causation} the dependencies of ${reactiveHookName} Hook (at line ${declaredDependenciesNode.loc?.start.line}) change on every render. ${advice}`;
|
|
7660
|
-
let suggest;
|
|
7661
|
-
if (isUsedOutsideOfHook && construction.type === "Variable" && depType === "function") suggest = [{
|
|
7662
|
-
desc: `Wrap the ${constructionType} of '${construction.name.name}' in its own ${wrapperHook}() Hook.`,
|
|
7663
|
-
fix(fixer) {
|
|
7664
|
-
const [before, after] = wrapperHook === "useMemo" ? [`useMemo(() => { return `, "; })"] : ["useCallback(", ")"];
|
|
7665
|
-
return [fixer.insertTextBefore(construction.node.init, before), fixer.insertTextAfter(construction.node.init, after)];
|
|
7666
|
-
}
|
|
7667
|
-
}];
|
|
7668
|
-
reportProblem({
|
|
7669
|
-
node: construction.node,
|
|
7670
|
-
message,
|
|
7671
|
-
suggest
|
|
7672
|
-
});
|
|
7673
|
-
});
|
|
7674
|
-
return;
|
|
7675
|
-
}
|
|
7676
|
-
if (!isEffect && missingDependencies.size > 0) suggestedDeps = collectRecommendations({
|
|
7677
|
-
dependencies,
|
|
7678
|
-
declaredDependencies: [],
|
|
7679
|
-
stableDependencies,
|
|
7680
|
-
externalDependencies,
|
|
7681
|
-
isEffect
|
|
7682
|
-
}).suggestedDependencies;
|
|
7683
|
-
function areDeclaredDepsAlphabetized() {
|
|
7684
|
-
if (declaredDependencies.length === 0) return true;
|
|
7685
|
-
const declaredDepKeys = declaredDependencies.map((dep) => dep.key);
|
|
7686
|
-
const sortedDeclaredDepKeys = declaredDepKeys.slice().sort();
|
|
7687
|
-
return declaredDepKeys.join(",") === sortedDeclaredDepKeys.join(",");
|
|
7688
|
-
}
|
|
7689
|
-
if (areDeclaredDepsAlphabetized()) suggestedDeps.sort();
|
|
7690
|
-
function formatDependency(path) {
|
|
7691
|
-
const members = path.split(".");
|
|
7692
|
-
let finalPath = "";
|
|
7693
|
-
for (let i = 0; i < members.length; i++) {
|
|
7694
|
-
if (i !== 0) {
|
|
7695
|
-
const pathSoFar = members.slice(0, i + 1).join(".");
|
|
7696
|
-
const isOptional = optionalChains.get(pathSoFar) === true;
|
|
7697
|
-
finalPath += isOptional ? "?." : ".";
|
|
7698
|
-
}
|
|
7699
|
-
finalPath += members[i];
|
|
7700
|
-
}
|
|
7701
|
-
return finalPath;
|
|
7702
|
-
}
|
|
7703
|
-
function getWarningMessage(deps, singlePrefix, label, fixVerb) {
|
|
7704
|
-
if (deps.size === 0) return null;
|
|
7705
|
-
return (deps.size > 1 ? "" : singlePrefix + " ") + label + " " + (deps.size > 1 ? "dependencies" : "dependency") + ": " + joinEnglish(Array.from(deps).sort().map((name) => "'" + formatDependency(name) + "'")) + `. Either ${fixVerb} ${deps.size > 1 ? "them" : "it"} or remove the dependency array.`;
|
|
7706
|
-
}
|
|
7707
|
-
let extraWarning = "";
|
|
7708
|
-
if (unnecessaryDependencies.size > 0) {
|
|
7709
|
-
let badRef = null;
|
|
7710
|
-
Array.from(unnecessaryDependencies.keys()).forEach((key) => {
|
|
7711
|
-
if (badRef !== null) return;
|
|
7712
|
-
if (key.endsWith(".current")) badRef = key;
|
|
7713
|
-
});
|
|
7714
|
-
if (badRef !== null) extraWarning = ` Mutable values like '${badRef}' aren't valid dependencies because mutating them doesn't re-render the component.`;
|
|
7715
|
-
else if (externalDependencies.size > 0) {
|
|
7716
|
-
const dep = Array.from(externalDependencies)[0];
|
|
7717
|
-
if (!scope.set.has(dep)) extraWarning = ` Outer scope values like '${dep}' aren't valid dependencies because mutating them doesn't re-render the component.`;
|
|
7718
|
-
}
|
|
7719
|
-
}
|
|
7720
|
-
if (!extraWarning && missingDependencies.has("props")) {
|
|
7721
|
-
const propDep = dependencies.get("props");
|
|
7722
|
-
if (propDep == null) return;
|
|
7723
|
-
const refs = propDep.references;
|
|
7724
|
-
if (!Array.isArray(refs)) return;
|
|
7725
|
-
let isPropsOnlyUsedInMembers = true;
|
|
7726
|
-
for (const ref of refs) {
|
|
7727
|
-
const id = fastFindReferenceWithParent(componentScope.block, ref.identifier);
|
|
7728
|
-
if (!id) {
|
|
7729
|
-
isPropsOnlyUsedInMembers = false;
|
|
7730
|
-
break;
|
|
7731
|
-
}
|
|
7732
|
-
const parent = id.parent;
|
|
7733
|
-
if (parent == null) {
|
|
7734
|
-
isPropsOnlyUsedInMembers = false;
|
|
7735
|
-
break;
|
|
7736
|
-
}
|
|
7737
|
-
if (parent.type !== "MemberExpression" && parent.type !== "OptionalMemberExpression") {
|
|
7738
|
-
isPropsOnlyUsedInMembers = false;
|
|
7739
|
-
break;
|
|
7740
|
-
}
|
|
7741
|
-
}
|
|
7742
|
-
if (isPropsOnlyUsedInMembers) extraWarning = ` However, 'props' will change when *any* prop changes, so the preferred fix is to destructure the 'props' object outside of the ${reactiveHookName} call and refer to those specific props inside ${getSourceCode().getText(reactiveHook)}.`;
|
|
7743
|
-
}
|
|
7744
|
-
if (!extraWarning && missingDependencies.size > 0) {
|
|
7745
|
-
let missingCallbackDep = null;
|
|
7746
|
-
missingDependencies.forEach((missingDep) => {
|
|
7747
|
-
if (missingCallbackDep) return;
|
|
7748
|
-
const topScopeRef = componentScope.set.get(missingDep);
|
|
7749
|
-
const usedDep = dependencies.get(missingDep);
|
|
7750
|
-
if (!usedDep?.references || usedDep?.references[0]?.resolved !== topScopeRef) return;
|
|
7751
|
-
const def = topScopeRef?.defs[0];
|
|
7752
|
-
if (def == null || def.name == null || def.type !== "Parameter") return;
|
|
7753
|
-
let isFunctionCall = false;
|
|
7754
|
-
let id;
|
|
7755
|
-
for (const reference of usedDep.references) {
|
|
7756
|
-
id = reference.identifier;
|
|
7757
|
-
if (id != null && id.parent != null && (id.parent.type === "CallExpression" || id.parent.type === "OptionalCallExpression") && id.parent.callee === id) {
|
|
7758
|
-
isFunctionCall = true;
|
|
7759
|
-
break;
|
|
7760
|
-
}
|
|
7761
|
-
}
|
|
7762
|
-
if (!isFunctionCall) return;
|
|
7763
|
-
missingCallbackDep = missingDep;
|
|
7764
|
-
});
|
|
7765
|
-
if (missingCallbackDep !== null) extraWarning = ` If '${missingCallbackDep}' changes too often, find the parent component that defines it and wrap that definition in useCallback.`;
|
|
7766
|
-
}
|
|
7767
|
-
if (!extraWarning && missingDependencies.size > 0) {
|
|
7768
|
-
let setStateRecommendation = null;
|
|
7769
|
-
for (const missingDep of missingDependencies) {
|
|
7770
|
-
if (setStateRecommendation !== null) break;
|
|
7771
|
-
const references = dependencies.get(missingDep).references;
|
|
7772
|
-
let id;
|
|
7773
|
-
let maybeCall;
|
|
7774
|
-
for (const reference of references) {
|
|
7775
|
-
id = reference.identifier;
|
|
7776
|
-
maybeCall = id.parent;
|
|
7777
|
-
while (maybeCall != null && maybeCall !== componentScope.block) {
|
|
7778
|
-
if (maybeCall.type === "CallExpression") {
|
|
7779
|
-
const correspondingStateVariable = setStateCallSites.get(maybeCall.callee);
|
|
7780
|
-
if (correspondingStateVariable != null) {
|
|
7781
|
-
if ("name" in correspondingStateVariable && correspondingStateVariable.name === missingDep) setStateRecommendation = {
|
|
7782
|
-
missingDep,
|
|
7783
|
-
setter: "name" in maybeCall.callee ? maybeCall.callee.name : "",
|
|
7784
|
-
form: "updater"
|
|
7785
|
-
};
|
|
7786
|
-
else if (stateVariables.has(id)) setStateRecommendation = {
|
|
7787
|
-
missingDep,
|
|
7788
|
-
setter: "name" in maybeCall.callee ? maybeCall.callee.name : "",
|
|
7789
|
-
form: "reducer"
|
|
7790
|
-
};
|
|
7791
|
-
else {
|
|
7792
|
-
const resolved = reference.resolved;
|
|
7793
|
-
if (resolved != null) {
|
|
7794
|
-
const def = resolved.defs[0];
|
|
7795
|
-
if (def != null && def.type === "Parameter") setStateRecommendation = {
|
|
7796
|
-
missingDep,
|
|
7797
|
-
setter: "name" in maybeCall.callee ? maybeCall.callee.name : "",
|
|
7798
|
-
form: "inlineReducer"
|
|
7799
|
-
};
|
|
7800
|
-
}
|
|
7801
|
-
}
|
|
7802
|
-
break;
|
|
7803
|
-
}
|
|
7804
|
-
}
|
|
7805
|
-
maybeCall = maybeCall.parent;
|
|
7806
|
-
}
|
|
7807
|
-
if (setStateRecommendation !== null) break;
|
|
7808
|
-
}
|
|
7809
|
-
}
|
|
7810
|
-
if (setStateRecommendation !== null) switch (setStateRecommendation.form) {
|
|
7811
|
-
case "reducer":
|
|
7812
|
-
extraWarning = ` You can also replace multiple useState variables with useReducer if '${setStateRecommendation.setter}' needs the current value of '${setStateRecommendation.missingDep}'.`;
|
|
7813
|
-
break;
|
|
7814
|
-
case "inlineReducer":
|
|
7815
|
-
extraWarning = ` If '${setStateRecommendation.setter}' needs the current value of '${setStateRecommendation.missingDep}', you can also switch to useReducer instead of useState and read '${setStateRecommendation.missingDep}' in the reducer.`;
|
|
7816
|
-
break;
|
|
7817
|
-
case "updater":
|
|
7818
|
-
extraWarning = ` You can also do a functional update '${setStateRecommendation.setter}(${setStateRecommendation.missingDep.slice(0, 1)} => ...)' if you only need '${setStateRecommendation.missingDep}' in the '${setStateRecommendation.setter}' call.`;
|
|
7819
|
-
break;
|
|
7820
|
-
default: throw new Error("Unknown case.");
|
|
7821
|
-
}
|
|
7822
|
-
}
|
|
7823
|
-
reportProblem({
|
|
7824
|
-
node: declaredDependenciesNode,
|
|
7825
|
-
message: `React Hook ${getSourceCode().getText(reactiveHook)} has ` + (getWarningMessage(missingDependencies, "a", "missing", "include") || getWarningMessage(unnecessaryDependencies, "an", "unnecessary", "exclude") || getWarningMessage(duplicateDependencies, "a", "duplicate", "omit")) + extraWarning,
|
|
7826
|
-
suggest: [{
|
|
7827
|
-
desc: `Update the dependencies array to be: [${suggestedDeps.map(formatDependency).join(", ")}]`,
|
|
7828
|
-
fix(fixer) {
|
|
7829
|
-
return fixer.replaceText(declaredDependenciesNode, `[${suggestedDeps.map(formatDependency).join(", ")}]`);
|
|
7830
|
-
}
|
|
7831
|
-
}]
|
|
7832
|
-
});
|
|
7833
|
-
}
|
|
7834
|
-
function visitCallExpression(node) {
|
|
7835
|
-
const configuredHook = getReactiveHookConfig(node, options.hooks);
|
|
7836
|
-
const callbackIndex = configuredHook ? configuredHook.closureIndex : getReactiveHookCallbackIndex(node.callee, options);
|
|
7837
|
-
if (callbackIndex === -1) return;
|
|
7838
|
-
let callback = node.arguments[callbackIndex];
|
|
7839
|
-
const reactiveHook = node.callee;
|
|
7840
|
-
const nodeWithoutNamespace = getNodeWithoutReactNamespace(reactiveHook);
|
|
7841
|
-
const reactiveHookName = configuredHook?.name ?? ("name" in nodeWithoutNamespace ? nodeWithoutNamespace.name : "");
|
|
7842
|
-
const dependenciesIndex = configuredHook ? configuredHook.dependenciesIndex : callbackIndex + 1;
|
|
7843
|
-
const maybeNode = node.arguments[dependenciesIndex];
|
|
7844
|
-
const declaredDependenciesNode = maybeNode && !(maybeNode.type === "Identifier" && maybeNode.name === "undefined") ? maybeNode : void 0;
|
|
7845
|
-
const isEffect = /Effect($|[^a-z])/g.test(reactiveHookName);
|
|
7846
|
-
if (!callback) {
|
|
7847
|
-
if (configuredHook) {
|
|
7848
|
-
const builtInCallbackIndex = getReactiveHookCallbackIndex(reactiveHook, { additionalHooks: void 0 });
|
|
7849
|
-
const builtInCallback = builtInCallbackIndex === -1 ? null : node.arguments[builtInCallbackIndex];
|
|
7850
|
-
if (builtInCallbackIndex === -1 || builtInCallback != null) return;
|
|
7851
|
-
}
|
|
7852
|
-
reportProblem({
|
|
7853
|
-
node: reactiveHook,
|
|
7854
|
-
message: `React Hook ${reactiveHookName} requires an effect callback. Did you forget to pass a callback to the hook?`
|
|
7855
|
-
});
|
|
7856
|
-
return;
|
|
7857
|
-
}
|
|
7858
|
-
if (!maybeNode && isEffect && options.requireExplicitEffectDeps) reportProblem({
|
|
7859
|
-
node: reactiveHook,
|
|
7860
|
-
message: `React Hook ${reactiveHookName} always requires dependencies. Please add a dependency array or an explicit \`undefined\``
|
|
7861
|
-
});
|
|
7862
|
-
const isAutoDepsHook = options.experimental_autoDependenciesHooks.includes(reactiveHookName);
|
|
7863
|
-
if ((!declaredDependenciesNode || isAutoDepsHook && declaredDependenciesNode.type === "Literal" && declaredDependenciesNode.value === null) && !isEffect) {
|
|
7864
|
-
if (reactiveHookName === "useMemo" || reactiveHookName === "useCallback") reportProblem({
|
|
7865
|
-
node: reactiveHook,
|
|
7866
|
-
message: `React Hook ${reactiveHookName} does nothing when called with only one argument. Did you forget to pass an array of dependencies?`
|
|
7867
|
-
});
|
|
7868
|
-
return;
|
|
7869
|
-
}
|
|
7870
|
-
while (callback.type === "TSAsExpression" || callback.type === "AsExpression") callback = callback.expression;
|
|
7871
|
-
switch (callback.type) {
|
|
7872
|
-
case "FunctionExpression":
|
|
7873
|
-
case "ArrowFunctionExpression":
|
|
7874
|
-
visitFunctionWithDependencies(callback, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect, isAutoDepsHook, node, dependenciesIndex);
|
|
7875
|
-
return;
|
|
7876
|
-
case "Identifier":
|
|
7877
|
-
if (!declaredDependenciesNode || isAutoDepsHook && declaredDependenciesNode.type === "Literal" && declaredDependenciesNode.value === null) return;
|
|
7878
|
-
if ("elements" in declaredDependenciesNode && declaredDependenciesNode.elements && declaredDependenciesNode.elements.some((el) => el && el.type === "Identifier" && el.name === callback.name)) return;
|
|
7879
|
-
const variable = getScope(callback).set.get(callback.name);
|
|
7880
|
-
if (variable == null || variable.defs == null) return;
|
|
7881
|
-
const def = variable.defs[0];
|
|
7882
|
-
if (!def || !def.node) break;
|
|
7883
|
-
if (def.type === "Parameter") {
|
|
7884
|
-
reportProblem({
|
|
7885
|
-
node: reactiveHook,
|
|
7886
|
-
message: getUnknownDependenciesMessage(reactiveHookName)
|
|
7887
|
-
});
|
|
7888
|
-
return;
|
|
7889
|
-
}
|
|
7890
|
-
if (def.type !== "Variable" && def.type !== "FunctionName") break;
|
|
7891
|
-
switch (def.node.type) {
|
|
7892
|
-
case "FunctionDeclaration":
|
|
7893
|
-
visitFunctionWithDependencies(def.node, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect, isAutoDepsHook, node, dependenciesIndex);
|
|
7894
|
-
return;
|
|
7895
|
-
case "VariableDeclarator":
|
|
7896
|
-
const init = def.node.init;
|
|
7897
|
-
if (!init) break;
|
|
7898
|
-
switch (init.type) {
|
|
7899
|
-
case "ArrowFunctionExpression":
|
|
7900
|
-
case "FunctionExpression":
|
|
7901
|
-
visitFunctionWithDependencies(init, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect, isAutoDepsHook, node, dependenciesIndex);
|
|
7902
|
-
return;
|
|
7903
|
-
}
|
|
7904
|
-
}
|
|
7905
|
-
break;
|
|
7906
|
-
default:
|
|
7907
|
-
if (configuredHook) return;
|
|
7908
|
-
reportProblem({
|
|
7909
|
-
node: reactiveHook,
|
|
7910
|
-
message: getUnknownDependenciesMessage(reactiveHookName)
|
|
7911
|
-
});
|
|
7912
|
-
return;
|
|
7913
|
-
}
|
|
7914
|
-
reportProblem({
|
|
7915
|
-
node: reactiveHook,
|
|
7916
|
-
message: `React Hook ${reactiveHookName} has a missing dependency: '${callback.name}'. Either include it or remove the dependency array.`,
|
|
7917
|
-
suggest: [{
|
|
7918
|
-
desc: `Update the dependencies array to be: [${callback.name}]`,
|
|
7919
|
-
fix(fixer) {
|
|
7920
|
-
return fixer.replaceText(declaredDependenciesNode, `[${callback.name}]`);
|
|
7921
|
-
}
|
|
7922
|
-
}]
|
|
7923
|
-
});
|
|
7924
|
-
}
|
|
7925
|
-
return { CallExpression: visitCallExpression };
|
|
7926
|
-
}
|
|
7927
|
-
};
|
|
7928
|
-
function collectRecommendations({ dependencies, declaredDependencies, stableDependencies, externalDependencies, isEffect }) {
|
|
7929
|
-
const depTree = createDepTree();
|
|
7930
|
-
function createDepTree() {
|
|
7931
|
-
return {
|
|
7932
|
-
isUsed: false,
|
|
7933
|
-
isSatisfiedRecursively: false,
|
|
7934
|
-
isSubtreeUsed: false,
|
|
7935
|
-
children: /* @__PURE__ */ new Map()
|
|
7936
|
-
};
|
|
7937
|
-
}
|
|
7938
|
-
dependencies.forEach((_, key) => {
|
|
7939
|
-
const node = getOrCreateNodeByPath(depTree, key);
|
|
7940
|
-
node.isUsed = true;
|
|
7941
|
-
markAllParentsByPath(depTree, key, (parent) => {
|
|
7942
|
-
parent.isSubtreeUsed = true;
|
|
7943
|
-
});
|
|
7944
|
-
});
|
|
7945
|
-
declaredDependencies.forEach(({ key }) => {
|
|
7946
|
-
const node = getOrCreateNodeByPath(depTree, key);
|
|
7947
|
-
node.isSatisfiedRecursively = true;
|
|
7948
|
-
});
|
|
7949
|
-
stableDependencies.forEach((key) => {
|
|
7950
|
-
const node = getOrCreateNodeByPath(depTree, key);
|
|
7951
|
-
node.isSatisfiedRecursively = true;
|
|
7952
|
-
});
|
|
7953
|
-
function getOrCreateNodeByPath(rootNode, path) {
|
|
7954
|
-
const keys = path.split(".");
|
|
7955
|
-
let node = rootNode;
|
|
7956
|
-
for (const key of keys) {
|
|
7957
|
-
let child = node.children.get(key);
|
|
7958
|
-
if (!child) {
|
|
7959
|
-
child = createDepTree();
|
|
7960
|
-
node.children.set(key, child);
|
|
7961
|
-
}
|
|
7962
|
-
node = child;
|
|
7963
|
-
}
|
|
7964
|
-
return node;
|
|
7965
|
-
}
|
|
7966
|
-
function markAllParentsByPath(rootNode, path, fn) {
|
|
7967
|
-
const keys = path.split(".");
|
|
7968
|
-
let node = rootNode;
|
|
7969
|
-
for (const key of keys) {
|
|
7970
|
-
const child = node.children.get(key);
|
|
7971
|
-
if (!child) return;
|
|
7972
|
-
fn(child);
|
|
7973
|
-
node = child;
|
|
7974
|
-
}
|
|
7975
|
-
}
|
|
7976
|
-
const missingDependencies = /* @__PURE__ */ new Set();
|
|
7977
|
-
const satisfyingDependencies = /* @__PURE__ */ new Set();
|
|
7978
|
-
scanTreeRecursively(depTree, missingDependencies, satisfyingDependencies, (key) => key);
|
|
7979
|
-
function scanTreeRecursively(node, missingPaths, satisfyingPaths, keyToPath) {
|
|
7980
|
-
node.children.forEach((child, key) => {
|
|
7981
|
-
const path = keyToPath(key);
|
|
7982
|
-
if (child.isSatisfiedRecursively) {
|
|
7983
|
-
if (child.isSubtreeUsed) satisfyingPaths.add(path);
|
|
7984
|
-
return;
|
|
7985
|
-
}
|
|
7986
|
-
if (child.isUsed) {
|
|
7987
|
-
missingPaths.add(path);
|
|
7988
|
-
return;
|
|
7989
|
-
}
|
|
7990
|
-
scanTreeRecursively(child, missingPaths, satisfyingPaths, (childKey) => path + "." + childKey);
|
|
7991
|
-
});
|
|
7992
|
-
}
|
|
7993
|
-
const suggestedDependencies = [];
|
|
7994
|
-
const unnecessaryDependencies = /* @__PURE__ */ new Set();
|
|
7995
|
-
const duplicateDependencies = /* @__PURE__ */ new Set();
|
|
7996
|
-
declaredDependencies.forEach(({ key }) => {
|
|
7997
|
-
if (satisfyingDependencies.has(key)) {
|
|
7998
|
-
if (suggestedDependencies.indexOf(key) === -1) suggestedDependencies.push(key);
|
|
7999
|
-
else duplicateDependencies.add(key);
|
|
8000
|
-
} else if (isEffect && !key.endsWith(".current") && !externalDependencies.has(key)) {
|
|
8001
|
-
if (suggestedDependencies.indexOf(key) === -1) suggestedDependencies.push(key);
|
|
8002
|
-
} else unnecessaryDependencies.add(key);
|
|
8003
|
-
});
|
|
8004
|
-
missingDependencies.forEach((key) => {
|
|
8005
|
-
suggestedDependencies.push(key);
|
|
8006
|
-
});
|
|
8007
|
-
return {
|
|
8008
|
-
suggestedDependencies,
|
|
8009
|
-
unnecessaryDependencies,
|
|
8010
|
-
duplicateDependencies,
|
|
8011
|
-
missingDependencies
|
|
8012
|
-
};
|
|
8013
|
-
}
|
|
8014
|
-
function getConstructionExpressionType(node) {
|
|
8015
|
-
switch (node.type) {
|
|
8016
|
-
case "ObjectExpression": return "object";
|
|
8017
|
-
case "ArrayExpression": return "array";
|
|
8018
|
-
case "ArrowFunctionExpression":
|
|
8019
|
-
case "FunctionExpression": return "function";
|
|
8020
|
-
case "ClassExpression": return "class";
|
|
8021
|
-
case "ConditionalExpression":
|
|
8022
|
-
if (getConstructionExpressionType(node.consequent) != null || getConstructionExpressionType(node.alternate) != null) return "conditional";
|
|
8023
|
-
return null;
|
|
8024
|
-
case "LogicalExpression":
|
|
8025
|
-
if (getConstructionExpressionType(node.left) != null || getConstructionExpressionType(node.right) != null) return "logical expression";
|
|
8026
|
-
return null;
|
|
8027
|
-
case "JSXFragment": return "JSX fragment";
|
|
8028
|
-
case "JSXElement": return "JSX element";
|
|
8029
|
-
case "AssignmentExpression":
|
|
8030
|
-
if (getConstructionExpressionType(node.right) != null) return "assignment expression";
|
|
8031
|
-
return null;
|
|
8032
|
-
case "NewExpression": return "object construction";
|
|
8033
|
-
case "Literal":
|
|
8034
|
-
if (node.value instanceof RegExp) return "regular expression";
|
|
8035
|
-
return null;
|
|
8036
|
-
case "TypeCastExpression":
|
|
8037
|
-
case "AsExpression":
|
|
8038
|
-
case "TSAsExpression": return getConstructionExpressionType(node.expression);
|
|
8039
|
-
}
|
|
8040
|
-
return null;
|
|
8041
|
-
}
|
|
8042
|
-
function scanForConstructions({ declaredDependencies, declaredDependenciesNode, componentScope, scope }) {
|
|
8043
|
-
const constructions = declaredDependencies.map(({ key }) => {
|
|
8044
|
-
const ref = componentScope.variables.find((v) => v.name === key);
|
|
8045
|
-
if (ref == null) return null;
|
|
8046
|
-
const node = ref.defs[0];
|
|
8047
|
-
if (node == null) return null;
|
|
8048
|
-
if (node.type === "Variable" && node.node.type === "VariableDeclarator" && node.node.id.type === "Identifier" && node.node.init != null) {
|
|
8049
|
-
const constantExpressionType = getConstructionExpressionType(node.node.init);
|
|
8050
|
-
if (constantExpressionType) return [ref, constantExpressionType];
|
|
8051
|
-
}
|
|
8052
|
-
if (node.type === "FunctionName" && node.node.type === "FunctionDeclaration") return [ref, "function"];
|
|
8053
|
-
if (node.type === "ClassName" && node.node.type === "ClassDeclaration") return [ref, "class"];
|
|
8054
|
-
return null;
|
|
8055
|
-
}).filter(Boolean);
|
|
8056
|
-
function isUsedOutsideOfHook(ref) {
|
|
8057
|
-
let foundWriteExpr = false;
|
|
8058
|
-
for (const reference of ref.references) {
|
|
8059
|
-
if (reference.writeExpr) {
|
|
8060
|
-
if (foundWriteExpr) return true;
|
|
8061
|
-
else {
|
|
8062
|
-
foundWriteExpr = true;
|
|
8063
|
-
continue;
|
|
8064
|
-
}
|
|
8065
|
-
}
|
|
8066
|
-
let currentScope = reference.from;
|
|
8067
|
-
while (currentScope !== scope && currentScope != null) currentScope = currentScope.upper;
|
|
8068
|
-
if (currentScope !== scope) {
|
|
8069
|
-
if (!isAncestorNodeOf(declaredDependenciesNode, reference.identifier)) return true;
|
|
8070
|
-
}
|
|
8071
|
-
}
|
|
8072
|
-
return false;
|
|
8073
|
-
}
|
|
8074
|
-
return constructions.map(([ref, depType]) => ({
|
|
8075
|
-
construction: ref.defs[0],
|
|
8076
|
-
depType,
|
|
8077
|
-
isUsedOutsideOfHook: isUsedOutsideOfHook(ref)
|
|
8078
|
-
}));
|
|
8079
|
-
}
|
|
8080
|
-
function getDependency(node) {
|
|
8081
|
-
if (node.parent && (node.parent.type === "MemberExpression" || node.parent.type === "OptionalMemberExpression") && node.parent.object === node && "name" in node.parent.property && node.parent.property.name !== "current" && !node.parent.computed && !(node.parent.parent != null && (node.parent.parent.type === "CallExpression" || node.parent.parent.type === "OptionalCallExpression") && node.parent.parent.callee === node.parent)) return getDependency(node.parent);
|
|
8082
|
-
else if (node.type === "MemberExpression" && node.parent && node.parent.type === "AssignmentExpression" && node.parent.left === node) return node.object;
|
|
8083
|
-
else return node;
|
|
8084
|
-
}
|
|
8085
|
-
function markNode(node, optionalChains, result) {
|
|
8086
|
-
if (optionalChains) {
|
|
8087
|
-
if ("optional" in node && node.optional) {
|
|
8088
|
-
if (!optionalChains.has(result)) optionalChains.set(result, true);
|
|
8089
|
-
} else optionalChains.set(result, false);
|
|
8090
|
-
}
|
|
8091
|
-
}
|
|
8092
|
-
function analyzePropertyChain(node, optionalChains) {
|
|
8093
|
-
if (node.type === "Identifier" || node.type === "JSXIdentifier") {
|
|
8094
|
-
const result = node.name;
|
|
8095
|
-
if (optionalChains) optionalChains.set(result, false);
|
|
8096
|
-
return result;
|
|
8097
|
-
} else if (node.type === "MemberExpression" && !node.computed) {
|
|
8098
|
-
const result = `${analyzePropertyChain(node.object, optionalChains)}.${analyzePropertyChain(node.property, null)}`;
|
|
8099
|
-
markNode(node, optionalChains, result);
|
|
8100
|
-
return result;
|
|
8101
|
-
} else if (node.type === "OptionalMemberExpression" && !node.computed) {
|
|
8102
|
-
const result = `${analyzePropertyChain(node.object, optionalChains)}.${analyzePropertyChain(node.property, null)}`;
|
|
8103
|
-
markNode(node, optionalChains, result);
|
|
8104
|
-
return result;
|
|
8105
|
-
} else if (node.type === "ChainExpression" && (!("computed" in node) || !node.computed)) {
|
|
8106
|
-
const expression = node.expression;
|
|
8107
|
-
if (expression.type === "CallExpression") throw new Error(`Unsupported node type: ${expression.type}`);
|
|
8108
|
-
const result = `${analyzePropertyChain(expression.object, optionalChains)}.${analyzePropertyChain(expression.property, null)}`;
|
|
8109
|
-
markNode(expression, optionalChains, result);
|
|
8110
|
-
return result;
|
|
8111
|
-
} else throw new Error(`Unsupported node type: ${node.type}`);
|
|
8112
|
-
}
|
|
8113
|
-
function getNodeWithoutReactNamespace(node) {
|
|
8114
|
-
if (node.type === "MemberExpression" && node.object.type === "Identifier" && node.object.name === "React" && node.property.type === "Identifier" && !node.computed) return node.property;
|
|
8115
|
-
return node;
|
|
8116
|
-
}
|
|
8117
|
-
function getReactiveHookCallbackIndex(calleeNode, options) {
|
|
8118
|
-
const node = getNodeWithoutReactNamespace(calleeNode);
|
|
8119
|
-
if (node.type !== "Identifier") return -1;
|
|
8120
|
-
switch (node.name) {
|
|
8121
|
-
case "useEffect":
|
|
8122
|
-
case "useLayoutEffect":
|
|
8123
|
-
case "useCallback":
|
|
8124
|
-
case "useMemo": return 0;
|
|
8125
|
-
case "useImperativeHandle": return 1;
|
|
8126
|
-
default: if (node === calleeNode && options && options.additionalHooks) {
|
|
8127
|
-
let name;
|
|
8128
|
-
try {
|
|
8129
|
-
name = analyzePropertyChain(node, null);
|
|
8130
|
-
} catch (error) {
|
|
8131
|
-
if (error instanceof Error && /Unsupported node type/.test(error.message)) return 0;
|
|
8132
|
-
else throw error;
|
|
8133
|
-
}
|
|
8134
|
-
if (options.additionalHooks instanceof RegExp) options.additionalHooks.lastIndex = 0;
|
|
8135
|
-
return options.additionalHooks.test(name) ? 0 : -1;
|
|
8136
|
-
} else return -1;
|
|
8137
|
-
}
|
|
8138
|
-
}
|
|
8139
|
-
function fastFindReferenceWithParent(start, target) {
|
|
8140
|
-
const queue = [start];
|
|
8141
|
-
let item;
|
|
8142
|
-
while (queue.length) {
|
|
8143
|
-
item = queue.shift();
|
|
8144
|
-
if (isSameIdentifier(item, target)) return item;
|
|
8145
|
-
if (!isAncestorNodeOf(item, target)) continue;
|
|
8146
|
-
for (const [key, value] of Object.entries(item)) {
|
|
8147
|
-
if (key === "parent") continue;
|
|
8148
|
-
if (isNodeLike(value)) {
|
|
8149
|
-
value.parent = item;
|
|
8150
|
-
queue.push(value);
|
|
8151
|
-
} else if (Array.isArray(value)) value.forEach((val) => {
|
|
8152
|
-
if (isNodeLike(val)) {
|
|
8153
|
-
val.parent = item;
|
|
8154
|
-
queue.push(val);
|
|
8155
|
-
}
|
|
8156
|
-
});
|
|
8157
|
-
}
|
|
8158
|
-
}
|
|
8159
|
-
return null;
|
|
8160
|
-
}
|
|
8161
|
-
function joinEnglish(arr) {
|
|
8162
|
-
let s = "";
|
|
8163
|
-
for (let i = 0; i < arr.length; i++) {
|
|
8164
|
-
s += arr[i];
|
|
8165
|
-
if (i === 0 && arr.length === 2) s += " and ";
|
|
8166
|
-
else if (i === arr.length - 2 && arr.length > 2) s += ", and ";
|
|
8167
|
-
else if (i < arr.length - 1) s += ", ";
|
|
8168
|
-
}
|
|
8169
|
-
return s;
|
|
8170
|
-
}
|
|
8171
|
-
function isNodeLike(val) {
|
|
8172
|
-
return typeof val === "object" && val !== null && !Array.isArray(val) && "type" in val && typeof val.type === "string";
|
|
8173
|
-
}
|
|
8174
|
-
function isSameIdentifier(a, b) {
|
|
8175
|
-
return (a.type === "Identifier" || a.type === "JSXIdentifier") && a.type === b.type && a.name === b.name && !!a.range && !!b.range && a.range[0] === b.range[0] && a.range[1] === b.range[1];
|
|
8176
|
-
}
|
|
8177
|
-
function isAncestorNodeOf(a, b) {
|
|
8178
|
-
return !!a.range && !!b.range && a.range[0] <= b.range[0] && a.range[1] >= b.range[1];
|
|
8179
|
-
}
|
|
8180
|
-
function isUseEffectEventIdentifier(node) {
|
|
8181
|
-
return node.type === "Identifier" && node.name === "useEffectEvent";
|
|
8182
|
-
}
|
|
8183
|
-
function getUnknownDependenciesMessage(reactiveHookName) {
|
|
8184
|
-
return `React Hook ${reactiveHookName} received a function whose dependencies are unknown. Pass an inline function instead.`;
|
|
8185
|
-
}
|
|
8186
7276
|
// src/rules/unsupported-syntax/lib.ts
|
|
8187
7277
|
function isEvalCall(node) {
|
|
8188
7278
|
const callee = Extract.unwrap(node.callee);
|
|
@@ -8273,12 +7363,12 @@ function createFactCollector(isUseMemoCall) {
|
|
|
8273
7363
|
Traverse.simpleTraverse(callback.body, { enter(node) {
|
|
8274
7364
|
switch (node.type) {
|
|
8275
7365
|
case AST_NODE_TYPES.AssignmentExpression:
|
|
8276
|
-
for (const target of
|
|
7366
|
+
for (const target of resolveAssignmentTargets(node.left)) pushWrite(node, target);
|
|
8277
7367
|
return;
|
|
8278
7368
|
case AST_NODE_TYPES.ForInStatement:
|
|
8279
7369
|
case AST_NODE_TYPES.ForOfStatement:
|
|
8280
7370
|
if (node.left.type === AST_NODE_TYPES.VariableDeclaration) return;
|
|
8281
|
-
for (const target of
|
|
7371
|
+
for (const target of resolveAssignmentTargets(node.left)) pushWrite(node, target);
|
|
8282
7372
|
return;
|
|
8283
7373
|
}
|
|
8284
7374
|
} });
|
|
@@ -8423,10 +7513,10 @@ var use_state_default = createRule({
|
|
|
8423
7513
|
create
|
|
8424
7514
|
});
|
|
8425
7515
|
function create(context) {
|
|
8426
|
-
const options =
|
|
8427
|
-
const enforceAssignment = options.enforceAssignment
|
|
8428
|
-
const enforceLazyInitialization = options.enforceLazyInitialization
|
|
8429
|
-
const enforceSetterName = options.enforceSetterName
|
|
7516
|
+
const options = context.options[0] ?? {};
|
|
7517
|
+
const enforceAssignment = options.enforceAssignment ?? true;
|
|
7518
|
+
const enforceLazyInitialization = options.enforceLazyInitialization ?? true;
|
|
7519
|
+
const enforceSetterName = options.enforceSetterName ?? true;
|
|
8430
7520
|
const { additionalStateHooks } = getSettingsFromContext(context);
|
|
8431
7521
|
return { CallExpression(node) {
|
|
8432
7522
|
if (!core.isUseStateLikeCall(node, additionalStateHooks)) return;
|
|
@@ -8513,7 +7603,7 @@ const plugin = {
|
|
|
8513
7603
|
},
|
|
8514
7604
|
rules: {
|
|
8515
7605
|
"error-boundaries": error_boundaries_default,
|
|
8516
|
-
"exhaustive-deps": rule$
|
|
7606
|
+
"exhaustive-deps": rule$1,
|
|
8517
7607
|
globals: globals_default,
|
|
8518
7608
|
immutability: immutability_default,
|
|
8519
7609
|
"no-access-state-in-setstate": no_access_state_in_setstate_default,
|
|
@@ -8558,11 +7648,10 @@ const plugin = {
|
|
|
8558
7648
|
"no-use-context": no_use_context_default,
|
|
8559
7649
|
purity: purity_default,
|
|
8560
7650
|
refs: refs_default,
|
|
8561
|
-
"rules-of-hooks": rule
|
|
7651
|
+
"rules-of-hooks": rule,
|
|
8562
7652
|
"set-state-in-effect": set_state_in_effect_default,
|
|
8563
7653
|
"set-state-in-render": set_state_in_render_default,
|
|
8564
7654
|
"static-components": static_components_default,
|
|
8565
|
-
"unstable-exhaustive-deps": rule,
|
|
8566
7655
|
"unsupported-syntax": unsupported_syntax_default,
|
|
8567
7656
|
"use-memo": use_memo_default,
|
|
8568
7657
|
"use-state": use_state_default
|