@eslint-react/var
Advanced tools
+1
-17
@@ -73,18 +73,2 @@ import { unit } from "@eslint-react/eff"; | ||
| //#endregion | ||
| //#region src/get-variable-initializer.d.ts | ||
| /** | ||
| * Get the initializer expression or statement of a variable definition at a specified index | ||
| * @param variable The variable to get the initializer from | ||
| * @param at The index of the variable definition to get the initializer from | ||
| * @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found | ||
| */ | ||
| declare function getVariableInitializer(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration; | ||
| /** | ||
| * Get the initializer expression or statement of a variable definition at a specified index, or the function declaration if the variable is a parameter of a function | ||
| * @param variable The variable to get the initializer from | ||
| * @param at The index of the variable definition to get the initializer from | ||
| * @returns The initializer expression or statement of the variable definition at the specified index, or the function declaration if the variable is a parameter of a function, or unit if not found | ||
| */ | ||
| declare function getVariableInitializerLoose(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration; | ||
| //#endregion | ||
| //#region src/is-assignment-target-equal.d.ts | ||
@@ -112,2 +96,2 @@ /** | ||
| //#endregion | ||
| export { AssignmentTarget, ObjectType, findEnclosingAssignmentTarget, findVariable, getObjectType, getVariableInitializer, getVariableInitializerLoose, isAssignmentTargetEqual, isValueEqual }; | ||
| export { AssignmentTarget, ObjectType, findEnclosingAssignmentTarget, findVariable, getObjectType, isAssignmentTargetEqual, isValueEqual }; |
+29
-41
@@ -5,4 +5,4 @@ import { dual, unit } from "@eslint-react/eff"; | ||
| import { getStaticValue } from "@typescript-eslint/utils/ast-utils"; | ||
| import { DefinitionType } from "@typescript-eslint/scope-manager"; | ||
| import * as ast from "@eslint-react/ast"; | ||
| import { DefinitionType } from "@typescript-eslint/scope-manager"; | ||
@@ -44,36 +44,2 @@ //#region src/find-enclosing-assignment-target.ts | ||
| //#endregion | ||
| //#region src/get-variable-initializer.ts | ||
| /** | ||
| * Get the initializer expression or statement of a variable definition at a specified index | ||
| * @param variable The variable to get the initializer from | ||
| * @param at The index of the variable definition to get the initializer from | ||
| * @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found | ||
| */ | ||
| function getVariableInitializer(variable, at) { | ||
| if (variable == null) return unit; | ||
| const def = variable.defs.at(at); | ||
| if (def == null) return unit; | ||
| switch (true) { | ||
| case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node; | ||
| case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node; | ||
| case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init; | ||
| default: return unit; | ||
| } | ||
| } | ||
| /** | ||
| * Get the initializer expression or statement of a variable definition at a specified index, or the function declaration if the variable is a parameter of a function | ||
| * @param variable The variable to get the initializer from | ||
| * @param at The index of the variable definition to get the initializer from | ||
| * @returns The initializer expression or statement of the variable definition at the specified index, or the function declaration if the variable is a parameter of a function, or unit if not found | ||
| */ | ||
| function getVariableInitializerLoose(variable, at) { | ||
| if (variable == null) return unit; | ||
| const node = getVariableInitializer(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 unit; | ||
| } | ||
| //#endregion | ||
| //#region src/get-object-type.ts | ||
@@ -123,5 +89,16 @@ /** | ||
| return unit; | ||
| case AST_NODE_TYPES.Identifier: | ||
| if (!("name" in node) || typeof node.name !== "string") return unit; | ||
| return getObjectType(getVariableInitializer(initialScope.set.get(node.name), -1), initialScope); | ||
| case AST_NODE_TYPES.Identifier: { | ||
| function resolve(v) { | ||
| if (v == null) return unit; | ||
| const def = v.defs.at(-1); | ||
| if (def == null) return unit; | ||
| if (def.type === DefinitionType.Variable) return def.node.init; | ||
| if (def.type === DefinitionType.Parameter) return unit; | ||
| if (def.type === DefinitionType.ImportBinding) return unit; | ||
| return def.node; | ||
| } | ||
| const initNode = resolve(initialScope.set.get(node.name)); | ||
| if (initNode == null) return unit; | ||
| return getObjectType(initNode, initialScope); | ||
| } | ||
| case AST_NODE_TYPES.MemberExpression: | ||
@@ -176,4 +153,15 @@ if (!("object" in node)) return unit; | ||
| const bVar = findVariable(b, bScope); | ||
| const aVarInit = getVariableInitializerLoose(aVar, 0); | ||
| const bVarInit = getVariableInitializerLoose(bVar, 0); | ||
| const resolve = (variable) => { | ||
| if (variable == null) return unit; | ||
| const def = variable.defs.at(0); | ||
| if (def != null) switch (true) { | ||
| case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node; | ||
| case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node; | ||
| case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init; | ||
| } | ||
| if (def?.type === DefinitionType.Parameter && ast.isFunction(def.node)) return def.node; | ||
| return unit; | ||
| }; | ||
| const aVarInit = resolve(aVar); | ||
| const bVarInit = resolve(bVar); | ||
| const aVarInitParent = aVarInit?.parent; | ||
@@ -233,2 +221,2 @@ const bVarInitParent = bVarInit?.parent; | ||
| //#endregion | ||
| export { findEnclosingAssignmentTarget, findVariable, getObjectType, getVariableInitializer, getVariableInitializerLoose, isAssignmentTargetEqual, isValueEqual }; | ||
| export { findEnclosingAssignmentTarget, findVariable, getObjectType, isAssignmentTargetEqual, isValueEqual }; |
+4
-4
| { | ||
| "name": "@eslint-react/var", | ||
| "version": "3.0.0-beta.60", | ||
| "version": "3.0.0-beta.61", | ||
| "description": "ESLint React's TSESTree AST utility module for static analysis of variables.", | ||
@@ -37,5 +37,5 @@ "homepage": "https://github.com/Rel1cx/eslint-react", | ||
| "ts-pattern": "^5.9.0", | ||
| "@eslint-react/ast": "3.0.0-beta.60", | ||
| "@eslint-react/shared": "3.0.0-beta.60", | ||
| "@eslint-react/eff": "3.0.0-beta.60" | ||
| "@eslint-react/ast": "3.0.0-beta.61", | ||
| "@eslint-react/eff": "3.0.0-beta.61", | ||
| "@eslint-react/shared": "3.0.0-beta.61" | ||
| }, | ||
@@ -42,0 +42,0 @@ "devDependencies": { |
16156
-12.22%308
-8.06%+ Added
+ Added
+ Added
- Removed
- Removed
- Removed