Skip to content

fix(utilities/var): fix variable init node retrieval, fixes #964 #965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,6 @@ ruleTester.run(RULE_NAME, rule, {
const Provider = ({foo, children}: {foo: {}, children: React.ReactNode}) => {
return <Context value={foo}>{children}</Context>;
};
`
`,
],
});
4 changes: 0 additions & 4 deletions packages/utilities/var/src/get-variable-node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as AST from "@eslint-react/ast";
import { _ } from "@eslint-react/eff";
import type { Variable } from "@typescript-eslint/scope-manager";
import { DefinitionType } from "@typescript-eslint/scope-manager";
Expand All @@ -25,9 +24,6 @@ export function getVariableNode(variable: Variable | _, at: number):
case def.type === DefinitionType.ClassName
&& def.node.type === T.ClassDeclaration:
return def.node;
case def.type === DefinitionType.Parameter
&& AST.isFunction(def.node):
return def.node;
case "init" in def.node
&& def.node.init != null
&& !("declarations" in def.node.init):
Expand Down
16 changes: 13 additions & 3 deletions packages/utilities/var/src/is-node-value-equal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as AST from "@eslint-react/ast";
import type { Scope } from "@typescript-eslint/scope-manager";
import { _ } from "@eslint-react/eff";
import { DefinitionType, type Scope, type Variable } from "@typescript-eslint/scope-manager";
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";

Expand Down Expand Up @@ -46,8 +47,8 @@ export function isNodeValueEqual(
&& b.type === T.Identifier: {
const aVar = findVariable(a, aScope);
const bVar = findVariable(b, bScope);
const aVarNode = getVariableNode(aVar, 0);
const bVarNode = getVariableNode(bVar, 0);
const aVarNode = getVariableNodeLoose(aVar, 0);
const bVarNode = getVariableNodeLoose(bVar, 0);
const aVarNodeParent = aVarNode?.parent;
const bVarNodeParent = bVarNode?.parent;
const aDef = aVar?.defs.at(0);
Expand Down Expand Up @@ -105,3 +106,12 @@ export function isNodeValueEqual(
}
}
}

function getVariableNodeLoose(variable: Variable | _, at: number): ReturnType<typeof getVariableNode> {
if (variable == null) return _;
const node = getVariableNode(variable, at);
if (node != null) return node;
const def = variable.defs.at(at);
if (def?.type === DefinitionType.Parameter && AST.isFunction(def.node)) return def.node;
return _;
}