Skip to content

Commit 40ce5f6

Browse files
authored
fix(utilities/var): fix variable init node retrieval, fixes #964 (#965)
1 parent 1df5702 commit 40ce5f6

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-unstable-context-value.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,6 @@ ruleTester.run(RULE_NAME, rule, {
269269
const Provider = ({foo, children}: {foo: {}, children: React.ReactNode}) => {
270270
return <Context value={foo}>{children}</Context>;
271271
};
272-
`
272+
`,
273273
],
274274
});

packages/utilities/var/src/get-variable-node.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as AST from "@eslint-react/ast";
21
import { _ } from "@eslint-react/eff";
32
import type { Variable } from "@typescript-eslint/scope-manager";
43
import { DefinitionType } from "@typescript-eslint/scope-manager";
@@ -25,9 +24,6 @@ export function getVariableNode(variable: Variable | _, at: number):
2524
case def.type === DefinitionType.ClassName
2625
&& def.node.type === T.ClassDeclaration:
2726
return def.node;
28-
case def.type === DefinitionType.Parameter
29-
&& AST.isFunction(def.node):
30-
return def.node;
3127
case "init" in def.node
3228
&& def.node.init != null
3329
&& !("declarations" in def.node.init):

packages/utilities/var/src/is-node-value-equal.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as AST from "@eslint-react/ast";
2-
import type { Scope } from "@typescript-eslint/scope-manager";
2+
import { _ } from "@eslint-react/eff";
3+
import { DefinitionType, type Scope, type Variable } from "@typescript-eslint/scope-manager";
34
import type { TSESTree } from "@typescript-eslint/types";
45
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
56

@@ -46,8 +47,8 @@ export function isNodeValueEqual(
4647
&& b.type === T.Identifier: {
4748
const aVar = findVariable(a, aScope);
4849
const bVar = findVariable(b, bScope);
49-
const aVarNode = getVariableNode(aVar, 0);
50-
const bVarNode = getVariableNode(bVar, 0);
50+
const aVarNode = getVariableNodeLoose(aVar, 0);
51+
const bVarNode = getVariableNodeLoose(bVar, 0);
5152
const aVarNodeParent = aVarNode?.parent;
5253
const bVarNodeParent = bVarNode?.parent;
5354
const aDef = aVar?.defs.at(0);
@@ -105,3 +106,12 @@ export function isNodeValueEqual(
105106
}
106107
}
107108
}
109+
110+
function getVariableNodeLoose(variable: Variable | _, at: number): ReturnType<typeof getVariableNode> {
111+
if (variable == null) return _;
112+
const node = getVariableNode(variable, at);
113+
if (node != null) return node;
114+
const def = variable.defs.at(at);
115+
if (def?.type === DefinitionType.Parameter && AST.isFunction(def.node)) return def.node;
116+
return _;
117+
}

0 commit comments

Comments
 (0)