@eslint-react/var
Advanced tools
+18
-33
| import { unit } from "@eslint-react/eff"; | ||
| import { Scope } from "@typescript-eslint/scope-manager"; | ||
| import { TSESTree } from "@typescript-eslint/types"; | ||
| import { Scope, Variable } from "@typescript-eslint/scope-manager"; | ||
| import { RuleContext } from "@eslint-react/shared"; | ||
| //#region src/find-enclosing-assignment-target.d.ts | ||
| //#region src/compute-object-type.d.ts | ||
| /** | ||
| * Finds the enclosing assignment target (variable, property, etc.) for a given node | ||
| * | ||
| * @todo Verify correctness and completeness of this function | ||
| * @param node The starting node | ||
| * @returns The enclosing assignment target node, or undefined if not found | ||
| */ | ||
| declare function findEnclosingAssignmentTarget(node: TSESTree.Node): TSESTree.ArrayExpression | TSESTree.ArrayPattern | TSESTree.ArrowFunctionExpression | TSESTree.AssignmentExpression | TSESTree.AwaitExpression | TSESTree.PrivateInExpression | TSESTree.SymmetricBinaryExpression | TSESTree.CallExpression | TSESTree.ChainExpression | TSESTree.ClassExpression | TSESTree.ConditionalExpression | TSESTree.FunctionExpression | TSESTree.Identifier | TSESTree.ImportExpression | TSESTree.JSXElement | TSESTree.JSXFragment | TSESTree.BigIntLiteral | TSESTree.BooleanLiteral | TSESTree.NullLiteral | TSESTree.NumberLiteral | TSESTree.RegExpLiteral | TSESTree.StringLiteral | TSESTree.LogicalExpression | TSESTree.MemberExpressionComputedName | TSESTree.MemberExpressionNonComputedName | TSESTree.MetaProperty | TSESTree.NewExpression | TSESTree.ObjectExpression | TSESTree.ObjectPattern | TSESTree.PrivateIdentifier | TSESTree.SequenceExpression | TSESTree.Super | TSESTree.TaggedTemplateExpression | TSESTree.TemplateLiteral | TSESTree.ThisExpression | TSESTree.TSAsExpression | TSESTree.TSInstantiationExpression | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion | TSESTree.UnaryExpressionBitwiseNot | TSESTree.UnaryExpressionDelete | TSESTree.UnaryExpressionMinus | TSESTree.UnaryExpressionNot | TSESTree.UnaryExpressionPlus | TSESTree.UnaryExpressionTypeof | TSESTree.UnaryExpressionVoid | TSESTree.UpdateExpression | TSESTree.YieldExpression | undefined; | ||
| /** | ||
| * Type representing the possible assignment targets returned by `findEnclosingAssignmentTarget` | ||
| */ | ||
| type AssignmentTarget = ReturnType<typeof findEnclosingAssignmentTarget>; | ||
| //#endregion | ||
| //#region src/find-variable.d.ts | ||
| /** | ||
| * Find a variable by name or identifier node in the scope chain | ||
| * @param initialScope The scope to start searching from | ||
| * @returns The found variable or unit if not found | ||
| * @overload | ||
| * @param nameOrNode The variable name or identifier node to find | ||
| * @param initialScope The scope to start searching from | ||
| * @returns The found variable or unit if not found | ||
| */ | ||
| declare const findVariable: { | ||
| (initialScope: Scope): (nameOrNode: string | TSESTree.Identifier | unit) => Variable | unit; | ||
| (nameOrNode: string | TSESTree.Identifier | unit, initialScope: Scope): Variable | unit; | ||
| }; | ||
| //#endregion | ||
| //#region src/get-object-type.d.ts | ||
| /** | ||
| * Represents the type classification of an object node | ||
@@ -71,4 +42,18 @@ */ | ||
| */ | ||
| declare function getObjectType(node: TSESTree.Node | unit, initialScope: Scope): ObjectType | unit; | ||
| declare function computeObjectType(node: TSESTree.Node | unit, initialScope: Scope): ObjectType | unit; | ||
| //#endregion | ||
| //#region src/find-enclosing-assignment-target.d.ts | ||
| /** | ||
| * Finds the enclosing assignment target (variable, property, etc.) for a given node | ||
| * | ||
| * @todo Verify correctness and completeness of this function | ||
| * @param node The starting node | ||
| * @returns The enclosing assignment target node, or undefined if not found | ||
| */ | ||
| declare function findEnclosingAssignmentTarget(node: TSESTree.Node): TSESTree.ArrayExpression | TSESTree.ArrayPattern | TSESTree.ArrowFunctionExpression | TSESTree.AssignmentExpression | TSESTree.AwaitExpression | TSESTree.PrivateInExpression | TSESTree.SymmetricBinaryExpression | TSESTree.CallExpression | TSESTree.ChainExpression | TSESTree.ClassExpression | TSESTree.ConditionalExpression | TSESTree.FunctionExpression | TSESTree.Identifier | TSESTree.ImportExpression | TSESTree.JSXElement | TSESTree.JSXFragment | TSESTree.BigIntLiteral | TSESTree.BooleanLiteral | TSESTree.NullLiteral | TSESTree.NumberLiteral | TSESTree.RegExpLiteral | TSESTree.StringLiteral | TSESTree.LogicalExpression | TSESTree.MemberExpressionComputedName | TSESTree.MemberExpressionNonComputedName | TSESTree.MetaProperty | TSESTree.NewExpression | TSESTree.ObjectExpression | TSESTree.ObjectPattern | TSESTree.PrivateIdentifier | TSESTree.SequenceExpression | TSESTree.Super | TSESTree.TaggedTemplateExpression | TSESTree.TemplateLiteral | TSESTree.ThisExpression | TSESTree.TSAsExpression | TSESTree.TSInstantiationExpression | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion | TSESTree.UnaryExpressionBitwiseNot | TSESTree.UnaryExpressionDelete | TSESTree.UnaryExpressionMinus | TSESTree.UnaryExpressionNot | TSESTree.UnaryExpressionPlus | TSESTree.UnaryExpressionTypeof | TSESTree.UnaryExpressionVoid | TSESTree.UpdateExpression | TSESTree.YieldExpression | undefined; | ||
| /** | ||
| * Type representing the possible assignment targets returned by `findEnclosingAssignmentTarget` | ||
| */ | ||
| type AssignmentTarget = ReturnType<typeof findEnclosingAssignmentTarget>; | ||
| //#endregion | ||
| //#region src/is-assignment-target-equal.d.ts | ||
@@ -96,2 +81,2 @@ /** | ||
| //#endregion | ||
| export { AssignmentTarget, ObjectType, findEnclosingAssignmentTarget, findVariable, getObjectType, isAssignmentTargetEqual, isValueEqual }; | ||
| export { AssignmentTarget, ObjectType, computeObjectType, findEnclosingAssignmentTarget, isAssignmentTargetEqual, isValueEqual }; |
+34
-51
@@ -1,45 +0,9 @@ | ||
| import { dual, unit } from "@eslint-react/eff"; | ||
| import { unit } from "@eslint-react/eff"; | ||
| import { DefinitionType } from "@typescript-eslint/scope-manager"; | ||
| import { AST_NODE_TYPES } from "@typescript-eslint/types"; | ||
| import * as astUtils from "@typescript-eslint/utils/ast-utils"; | ||
| import { getStaticValue } from "@typescript-eslint/utils/ast-utils"; | ||
| import { DefinitionType } from "@typescript-eslint/scope-manager"; | ||
| import * as ast from "@eslint-react/ast"; | ||
| import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils"; | ||
| //#region src/find-enclosing-assignment-target.ts | ||
| //#region src/compute-object-type.ts | ||
| /** | ||
| * Finds the enclosing assignment target (variable, property, etc.) for a given node | ||
| * | ||
| * @todo Verify correctness and completeness of this function | ||
| * @param node The starting node | ||
| * @returns The enclosing assignment target node, or undefined if not found | ||
| */ | ||
| function findEnclosingAssignmentTarget(node) { | ||
| switch (true) { | ||
| case node.type === AST_NODE_TYPES.VariableDeclarator: return node.id; | ||
| case node.type === AST_NODE_TYPES.AssignmentExpression: return node.left; | ||
| case node.type === AST_NODE_TYPES.PropertyDefinition: return node.key; | ||
| case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit; | ||
| default: return findEnclosingAssignmentTarget(node.parent); | ||
| } | ||
| } | ||
| //#endregion | ||
| //#region src/find-variable.ts | ||
| /** | ||
| * Find a variable by name or identifier node in the scope chain | ||
| * @param initialScope The scope to start searching from | ||
| * @returns The found variable or unit if not found | ||
| * @overload | ||
| * @param nameOrNode The variable name or identifier node to find | ||
| * @param initialScope The scope to start searching from | ||
| * @returns The found variable or unit if not found | ||
| */ | ||
| const findVariable = dual(2, (nameOrNode, initialScope) => { | ||
| if (nameOrNode == null) return unit; | ||
| return astUtils.findVariable(initialScope, nameOrNode) ?? unit; | ||
| }); | ||
| //#endregion | ||
| //#region src/get-object-type.ts | ||
| /** | ||
| * Detect the ObjectType of a given node | ||
@@ -50,3 +14,3 @@ * @param node The node to check | ||
| */ | ||
| function getObjectType(node, initialScope) { | ||
| function computeObjectType(node, initialScope) { | ||
| if (node == null) return unit; | ||
@@ -91,16 +55,16 @@ switch (node.type) { | ||
| if (initNode == null) return unit; | ||
| return getObjectType(initNode, initialScope); | ||
| return computeObjectType(initNode, initialScope); | ||
| } | ||
| case AST_NODE_TYPES.MemberExpression: | ||
| if (!("object" in node)) return unit; | ||
| return getObjectType(node.object, initialScope); | ||
| return computeObjectType(node.object, initialScope); | ||
| case AST_NODE_TYPES.AssignmentExpression: | ||
| case AST_NODE_TYPES.AssignmentPattern: | ||
| if (!("right" in node)) return unit; | ||
| return getObjectType(node.right, initialScope); | ||
| case AST_NODE_TYPES.LogicalExpression: return getObjectType(node.right, initialScope); | ||
| case AST_NODE_TYPES.ConditionalExpression: return getObjectType(node.consequent, initialScope) ?? getObjectType(node.alternate, initialScope); | ||
| return computeObjectType(node.right, initialScope); | ||
| case AST_NODE_TYPES.LogicalExpression: return computeObjectType(node.right, initialScope); | ||
| case AST_NODE_TYPES.ConditionalExpression: return computeObjectType(node.consequent, initialScope) ?? computeObjectType(node.alternate, initialScope); | ||
| case AST_NODE_TYPES.SequenceExpression: | ||
| if (node.expressions.length === 0) return unit; | ||
| return getObjectType(node.expressions[node.expressions.length - 1], initialScope); | ||
| return computeObjectType(node.expressions[node.expressions.length - 1], initialScope); | ||
| case AST_NODE_TYPES.CallExpression: return { | ||
@@ -113,3 +77,3 @@ kind: "unknown", | ||
| if (!("expression" in node) || typeof node.expression !== "object") return unit; | ||
| return getObjectType(node.expression, initialScope); | ||
| return computeObjectType(node.expression, initialScope); | ||
| } | ||
@@ -128,2 +92,21 @@ } | ||
| //#endregion | ||
| //#region src/find-enclosing-assignment-target.ts | ||
| /** | ||
| * Finds the enclosing assignment target (variable, property, etc.) for a given node | ||
| * | ||
| * @todo Verify correctness and completeness of this function | ||
| * @param node The starting node | ||
| * @returns The enclosing assignment target node, or undefined if not found | ||
| */ | ||
| function findEnclosingAssignmentTarget(node) { | ||
| switch (true) { | ||
| case node.type === AST_NODE_TYPES.VariableDeclarator: return node.id; | ||
| case node.type === AST_NODE_TYPES.AssignmentExpression: return node.left; | ||
| case node.type === AST_NODE_TYPES.PropertyDefinition: return node.key; | ||
| case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit; | ||
| default: return findEnclosingAssignmentTarget(node.parent); | ||
| } | ||
| } | ||
| //#endregion | ||
| //#region src/is-value-equal.ts | ||
@@ -152,4 +135,4 @@ const thisBlockTypes = [ | ||
| case a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier: { | ||
| const aVar = findVariable(a, aScope); | ||
| const bVar = findVariable(b, bScope); | ||
| const aVar = findVariable(aScope, a); | ||
| const bVar = findVariable(bScope, b); | ||
| const resolve = (variable) => { | ||
@@ -222,2 +205,2 @@ if (variable == null) return unit; | ||
| //#endregion | ||
| export { findEnclosingAssignmentTarget, findVariable, getObjectType, isAssignmentTargetEqual, isValueEqual }; | ||
| export { computeObjectType, findEnclosingAssignmentTarget, isAssignmentTargetEqual, isValueEqual }; |
+4
-4
| { | ||
| "name": "@eslint-react/var", | ||
| "version": "3.0.0-next.62", | ||
| "version": "3.0.0-next.63", | ||
| "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-next.62", | ||
| "@eslint-react/eff": "3.0.0-next.62", | ||
| "@eslint-react/shared": "3.0.0-next.62" | ||
| "@eslint-react/ast": "3.0.0-next.63", | ||
| "@eslint-react/eff": "3.0.0-next.63", | ||
| "@eslint-react/shared": "3.0.0-next.63" | ||
| }, | ||
@@ -42,0 +42,0 @@ "devDependencies": { |
14881
-7.74%277
-10.06%+ Added
+ Added
+ Added
- Removed
- Removed
- Removed