Skip to content

Commit 0880d16

Browse files
committed
Update utils for proxy rule
1 parent c0997f4 commit 0880d16

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/utils.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { TSESTree as T, TSESLint } from "@typescript-eslint/utils";
1+
import { TSESTree as T, TSESLint, ASTUtils } from "@typescript-eslint/utils";
2+
const { findVariable } = ASTUtils;
23

34
const domElementRegex = /^[a-z]/;
45
export const isDOMElementName = (name: string): boolean => domElementRegex.test(name);
@@ -42,6 +43,32 @@ export function findParent(node: T.Node, predicate: (node: T.Node) => boolean):
4243
return node.parent ? find(node.parent, predicate) : null;
4344
}
4445

46+
// Try to resolve a variable to its definition
47+
export function trace(node: T.Node, initialScope: TSESLint.Scope.Scope): T.Node {
48+
if (node.type === "Identifier") {
49+
const variable = findVariable(initialScope, node);
50+
if (!variable) return node;
51+
52+
const def = variable.defs[0];
53+
switch (def.type) {
54+
case "FunctionName":
55+
case "ClassName":
56+
case "ImportBinding":
57+
return def.node;
58+
case "Variable":
59+
if (
60+
((def.node.parent as T.VariableDeclaration).kind === "const" ||
61+
variable.references.every((ref) => ref.init || ref.isReadOnly())) &&
62+
def.node.id.type === "Identifier" &&
63+
def.node.init
64+
) {
65+
return trace(def.node.init, initialScope);
66+
}
67+
}
68+
}
69+
return node;
70+
}
71+
4572
export type FunctionNode = T.FunctionExpression | T.ArrowFunctionExpression | T.FunctionDeclaration;
4673
const FUNCTION_TYPES = ["FunctionExpression", "ArrowFunctionExpression", "FunctionDeclaration"];
4774
export const isFunctionNode = (node: T.Node | null | undefined): node is FunctionNode =>
@@ -95,12 +122,9 @@ export const trackImports = (fromModule = /^solid-js(?:\/?|\b)/) => {
95122
}
96123
}
97124
};
98-
const matchImport = (imports: string | Array<string>, str: string) => {
125+
const matchImport = (imports: string | Array<string>, str: string): string | undefined => {
99126
const importArr = Array.isArray(imports) ? imports : [imports];
100-
return importArr
101-
.map((i) => importMap.get(i))
102-
.filter(Boolean)
103-
.includes(str);
127+
return importArr.find((i) => importMap.get(i) === str);
104128
};
105129
return { matchImport, handleImportDeclaration };
106130
};

test/rules/reactivity.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jest.mock("../../src/utils", () => {
1111
const handleImportDeclaration = () => {};
1212
const matchImport = (imports: string | Array<string>, str: string) => {
1313
const importArr = Array.isArray(imports) ? imports : [imports];
14-
return importArr.includes(str);
14+
return importArr.find((i) => i === str);
1515
};
1616
return { matchImport, handleImportDeclaration };
1717
},

0 commit comments

Comments
 (0)