@eslint-react/var 3.0.0-next.60 → 3.0.0-next.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -70,22 +70,6 @@ type ObjectType = {
70
70
  */
71
71
  declare function getObjectType(node: TSESTree.Node | unit, initialScope: Scope): ObjectType | unit;
72
72
  //#endregion
73
- //#region src/get-variable-initializer.d.ts
74
- /**
75
- * Get the initializer expression or statement of a variable definition at a specified index
76
- * @param variable The variable to get the initializer from
77
- * @param at The index of the variable definition to get the initializer from
78
- * @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found
79
- */
80
- declare function getVariableInitializer(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration;
81
- /**
82
- * 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
83
- * @param variable The variable to get the initializer from
84
- * @param at The index of the variable definition to get the initializer from
85
- * @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
86
- */
87
- declare function getVariableInitializerLoose(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration;
88
- //#endregion
89
73
  //#region src/is-assignment-target-equal.d.ts
90
74
  /**
91
75
  * Check if two assignment targets are equal
@@ -108,4 +92,4 @@ declare function isAssignmentTargetEqual(context: RuleContext, a: TSESTree.Node,
108
92
  */
109
93
  declare function isValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
110
94
  //#endregion
111
- export { AssignmentTarget, ObjectType, findEnclosingAssignmentTarget, findVariable, getObjectType, getVariableInitializer, getVariableInitializerLoose, isAssignmentTargetEqual, isValueEqual };
95
+ export { AssignmentTarget, ObjectType, findEnclosingAssignmentTarget, findVariable, getObjectType, isAssignmentTargetEqual, isValueEqual };
package/dist/index.js CHANGED
@@ -2,8 +2,8 @@ import { dual, unit } from "@eslint-react/eff";
2
2
  import { AST_NODE_TYPES } from "@typescript-eslint/types";
3
3
  import * as astUtils from "@typescript-eslint/utils/ast-utils";
4
4
  import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
5
- import * as ast from "@eslint-react/ast";
6
5
  import { DefinitionType } from "@typescript-eslint/scope-manager";
6
+ import * as ast from "@eslint-react/ast";
7
7
 
8
8
  //#region src/find-enclosing-assignment-target.ts
9
9
  /**
@@ -39,40 +39,6 @@ const findVariable = dual(2, (nameOrNode, initialScope) => {
39
39
  return astUtils.findVariable(initialScope, nameOrNode) ?? unit;
40
40
  });
41
41
 
42
- //#endregion
43
- //#region src/get-variable-initializer.ts
44
- /**
45
- * Get the initializer expression or statement of a variable definition at a specified index
46
- * @param variable The variable to get the initializer from
47
- * @param at The index of the variable definition to get the initializer from
48
- * @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found
49
- */
50
- function getVariableInitializer(variable, at) {
51
- if (variable == null) return unit;
52
- const def = variable.defs.at(at);
53
- if (def == null) return unit;
54
- switch (true) {
55
- case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
56
- case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
57
- case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
58
- default: return unit;
59
- }
60
- }
61
- /**
62
- * 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
63
- * @param variable The variable to get the initializer from
64
- * @param at The index of the variable definition to get the initializer from
65
- * @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
66
- */
67
- function getVariableInitializerLoose(variable, at) {
68
- if (variable == null) return unit;
69
- const node = getVariableInitializer(variable, at);
70
- if (node != null) return node;
71
- const def = variable.defs.at(at);
72
- if (def?.type === DefinitionType.Parameter && ast.isFunction(def.node)) return def.node;
73
- return unit;
74
- }
75
-
76
42
  //#endregion
77
43
  //#region src/get-object-type.ts
78
44
  /**
@@ -118,9 +84,20 @@ function getObjectType(node, initialScope) {
118
84
  node
119
85
  };
120
86
  return unit;
121
- case AST_NODE_TYPES.Identifier:
122
- if (!("name" in node) || typeof node.name !== "string") return unit;
123
- return getObjectType(getVariableInitializer(initialScope.set.get(node.name), -1), initialScope);
87
+ case AST_NODE_TYPES.Identifier: {
88
+ function resolve(v) {
89
+ if (v == null) return unit;
90
+ const def = v.defs.at(-1);
91
+ if (def == null) return unit;
92
+ if (def.type === DefinitionType.Variable) return def.node.init;
93
+ if (def.type === DefinitionType.Parameter) return unit;
94
+ if (def.type === DefinitionType.ImportBinding) return unit;
95
+ return def.node;
96
+ }
97
+ const initNode = resolve(initialScope.set.get(node.name));
98
+ if (initNode == null) return unit;
99
+ return getObjectType(initNode, initialScope);
100
+ }
124
101
  case AST_NODE_TYPES.MemberExpression:
125
102
  if (!("object" in node)) return unit;
126
103
  return getObjectType(node.object, initialScope);
@@ -170,8 +147,19 @@ function isValueEqual(a, b, initialScopes) {
170
147
  case a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier: {
171
148
  const aVar = findVariable(a, aScope);
172
149
  const bVar = findVariable(b, bScope);
173
- const aVarInit = getVariableInitializerLoose(aVar, 0);
174
- const bVarInit = getVariableInitializerLoose(bVar, 0);
150
+ const resolve = (variable) => {
151
+ if (variable == null) return unit;
152
+ const def = variable.defs.at(0);
153
+ if (def != null) switch (true) {
154
+ case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
155
+ case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
156
+ case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
157
+ }
158
+ if (def?.type === DefinitionType.Parameter && ast.isFunction(def.node)) return def.node;
159
+ return unit;
160
+ };
161
+ const aVarInit = resolve(aVar);
162
+ const bVarInit = resolve(bVar);
175
163
  const aVarInitParent = aVarInit?.parent;
176
164
  const bVarInitParent = bVarInit?.parent;
177
165
  const aDef = aVar?.defs.at(0);
@@ -226,4 +214,4 @@ function isAssignmentTargetEqual(context, a, b) {
226
214
  }
227
215
 
228
216
  //#endregion
229
- export { findEnclosingAssignmentTarget, findVariable, getObjectType, getVariableInitializer, getVariableInitializerLoose, isAssignmentTargetEqual, isValueEqual };
217
+ export { findEnclosingAssignmentTarget, findVariable, getObjectType, isAssignmentTargetEqual, isValueEqual };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/var",
3
- "version": "3.0.0-next.60",
3
+ "version": "3.0.0-next.61",
4
4
  "description": "ESLint React's TSESTree AST utility module for static analysis of variables.",
5
5
  "homepage": "https://github.com/Rel1cx/eslint-react",
6
6
  "bugs": {
@@ -34,9 +34,9 @@
34
34
  "@typescript-eslint/types": "canary",
35
35
  "@typescript-eslint/utils": "canary",
36
36
  "ts-pattern": "^5.9.0",
37
- "@eslint-react/eff": "3.0.0-next.60",
38
- "@eslint-react/ast": "3.0.0-next.60",
39
- "@eslint-react/shared": "3.0.0-next.60"
37
+ "@eslint-react/shared": "3.0.0-next.61",
38
+ "@eslint-react/eff": "3.0.0-next.61",
39
+ "@eslint-react/ast": "3.0.0-next.61"
40
40
  },
41
41
  "devDependencies": {
42
42
  "tsdown": "^0.21.0-beta.2",