@eslint-react/var 3.0.0-next.4 → 3.0.0-next.41

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
@@ -3,7 +3,7 @@ import { TSESTree } from "@typescript-eslint/types";
3
3
  import { Scope, Variable } from "@typescript-eslint/scope-manager";
4
4
  import { RuleContext } from "@eslint-react/shared";
5
5
 
6
- //#region src/var-assignment-target.d.ts
6
+ //#region src/binding-assignment.d.ts
7
7
  /**
8
8
  * Finds the enclosing assignment target (variable, property, etc.) for a given node
9
9
  *
@@ -27,24 +27,7 @@ type AssignmentTarget = ReturnType<typeof findEnclosingAssignmentTarget>;
27
27
  */
28
28
  declare function isAssignmentTargetEqual(context: RuleContext, a: TSESTree.Node, b: TSESTree.Node): boolean;
29
29
  //#endregion
30
- //#region src/var-definition.d.ts
31
- /**
32
- * Get the definition node of a variable at a specific definition index
33
- * @param variable The variable to get the definition node from
34
- * @param at The index of the definition to retrieve (negative index supported)
35
- * @returns The definition node or unit if not found
36
- */
37
- declare function getVariableDefinitionNode(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.ClassDeclarationWithName | TSESTree.ClassDeclarationWithOptionalName | TSESTree.Expression | TSESTree.FunctionDeclaration | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName;
38
- /**
39
- * Get the definition node of a variable at a specific definition index (loose version)
40
- * Also returns the function node if the definition is a parameter
41
- * @param variable The variable to get the definition node from
42
- * @param at The index of the definition to retrieve
43
- * @returns The definition node or unit if not found
44
- */
45
- declare function getVariableDefinitionNodeLoose(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.ClassDeclarationWithName | TSESTree.ClassDeclarationWithOptionalName | TSESTree.Expression | TSESTree.FunctionDeclaration | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName;
46
- //#endregion
47
- //#region src/var-import-source.d.ts
30
+ //#region src/binding-import.d.ts
48
31
  /**
49
32
  * Find the import source of a variable
50
33
  * @param name The variable name
@@ -53,7 +36,50 @@ declare function getVariableDefinitionNodeLoose(variable: Variable | unit, at: n
53
36
  */
54
37
  declare function findImportSource(name: string, initialScope: Scope): string | undefined;
55
38
  //#endregion
56
- //#region src/var-node-equality.d.ts
39
+ //#region src/binding-initializer.d.ts
40
+ /**
41
+ * Get the initializer expression or statement of a variable definition at a specified index
42
+ * @param variable The variable to get the initializer from
43
+ * @param at The index of the variable definition to get the initializer from
44
+ * @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found
45
+ */
46
+ declare function getVariableInitializer(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration;
47
+ /**
48
+ * 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
49
+ * @param variable The variable to get the initializer from
50
+ * @param at The index of the variable definition to get the initializer from
51
+ * @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
52
+ */
53
+ declare function getVariableInitializerLoose(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration;
54
+ //#endregion
55
+ //#region src/scope.d.ts
56
+ /**
57
+ * Get all variables from the given scope up to the global scope
58
+ * @param initialScope The scope to start from
59
+ * @returns All variables from the given scope up to the global scope
60
+ */
61
+ declare function getVariables(initialScope: Scope): Variable[];
62
+ /**
63
+ * Find a variable by name or identifier node in the scope chain
64
+ * @param initialScope The scope to start searching from
65
+ * @returns The found variable or unit if not found
66
+ * @overload
67
+ * @param nameOrNode The variable name or identifier node to find
68
+ * @param initialScope The scope to start searching from
69
+ * @returns The found variable or unit if not found
70
+ */
71
+ declare const findVariable: {
72
+ (initialScope: Scope): (nameOrNode: string | TSESTree.Identifier | unit) => Variable | unit;
73
+ (nameOrNode: string | TSESTree.Identifier | unit, initialScope: Scope): Variable | unit;
74
+ };
75
+ /**
76
+ * Get all child scopes recursively from a given scope
77
+ * @param scope The scope to get child scopes from
78
+ * @returns Array of all child scopes including the input scope
79
+ */
80
+ declare function getChildScopes(scope: Scope): readonly Scope[];
81
+ //#endregion
82
+ //#region src/value-equality.d.ts
57
83
  /**
58
84
  * Determine whether node value equals to another node value
59
85
  * @param a node to compare
@@ -61,9 +87,21 @@ declare function findImportSource(name: string, initialScope: Scope): string | u
61
87
  * @param initialScopes initial scopes of the two nodes
62
88
  * @returns `true` if node value equal
63
89
  */
64
- declare function isNodeEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
90
+ declare function isValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
91
+ //#endregion
92
+ //#region src/value-property.d.ts
93
+ /**
94
+ * Find a property by name in an array of properties
95
+ * Handles spread elements by recursively resolving the referenced object
96
+ * @param name The property name to find
97
+ * @param properties The array of properties to search
98
+ * @param initialScope The scope to use for variable resolution
99
+ * @param seen Set of already seen variable names to prevent circular references
100
+ * @returns The found property or unit if not found
101
+ */
102
+ declare function findProperty(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | unit;
65
103
  //#endregion
66
- //#region src/var-object-type.d.ts
104
+ //#region src/value-type.d.ts
67
105
  /**
68
106
  * Represents the type classification of an object node
69
107
  */
@@ -101,43 +139,4 @@ type ObjectType = {
101
139
  */
102
140
  declare function getObjectType(node: TSESTree.Node | unit, initialScope: Scope): ObjectType | unit;
103
141
  //#endregion
104
- //#region src/var-property.d.ts
105
- /**
106
- * Find a property by name in an array of properties
107
- * Handles spread elements by recursively resolving the referenced object
108
- * @param name The property name to find
109
- * @param properties The array of properties to search
110
- * @param initialScope The scope to use for variable resolution
111
- * @param seen Set of already seen variable names to prevent circular references
112
- * @returns The found property or unit if not found
113
- */
114
- declare function findProperty(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | unit;
115
- //#endregion
116
- //#region src/var-scope.d.ts
117
- /**
118
- * Get all variables from the given scope up to the global scope
119
- * @param initialScope The scope to start from
120
- * @returns All variables from the given scope up to the global scope
121
- */
122
- declare function getVariables(initialScope: Scope): Variable[];
123
- /**
124
- * Find a variable by name or identifier node in the scope chain
125
- * @param initialScope The scope to start searching from
126
- * @returns The found variable or unit if not found
127
- * @overload
128
- * @param nameOrNode The variable name or identifier node to find
129
- * @param initialScope The scope to start searching from
130
- * @returns The found variable or unit if not found
131
- */
132
- declare const findVariable: {
133
- (initialScope: Scope): (nameOrNode: string | TSESTree.Identifier | unit) => Variable | unit;
134
- (nameOrNode: string | TSESTree.Identifier | unit, initialScope: Scope): Variable | unit;
135
- };
136
- /**
137
- * Get all child scopes recursively from a given scope
138
- * @param scope The scope to get child scopes from
139
- * @returns Array of all child scopes including the input scope
140
- */
141
- declare function getChildScopes(scope: Scope): readonly Scope[];
142
- //#endregion
143
- export { AssignmentTarget, ObjectType, findEnclosingAssignmentTarget, findImportSource, findProperty, findVariable, getChildScopes, getObjectType, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isAssignmentTargetEqual, isNodeEqual };
142
+ export { AssignmentTarget, ObjectType, findEnclosingAssignmentTarget, findImportSource, findProperty, findVariable, getChildScopes, getObjectType, getVariableInitializer, getVariableInitializerLoose, getVariables, isAssignmentTargetEqual, isValueEqual };
package/dist/index.js CHANGED
@@ -6,14 +6,14 @@ import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
6
6
  import { DefinitionType, ScopeType } from "@typescript-eslint/scope-manager";
7
7
  import { P, match } from "ts-pattern";
8
8
 
9
- //#region src/var-definition.ts
9
+ //#region src/binding-initializer.ts
10
10
  /**
11
- * Get the definition node of a variable at a specific definition index
12
- * @param variable The variable to get the definition node from
13
- * @param at The index of the definition to retrieve (negative index supported)
14
- * @returns The definition node or unit if not found
11
+ * Get the initializer expression or statement of a variable definition at a specified index
12
+ * @param variable The variable to get the initializer from
13
+ * @param at The index of the variable definition to get the initializer from
14
+ * @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found
15
15
  */
16
- function getVariableDefinitionNode(variable, at) {
16
+ function getVariableInitializer(variable, at) {
17
17
  if (variable == null) return unit;
18
18
  const def = variable.defs.at(at);
19
19
  if (def == null) return unit;
@@ -25,15 +25,14 @@ function getVariableDefinitionNode(variable, at) {
25
25
  }
26
26
  }
27
27
  /**
28
- * Get the definition node of a variable at a specific definition index (loose version)
29
- * Also returns the function node if the definition is a parameter
30
- * @param variable The variable to get the definition node from
31
- * @param at The index of the definition to retrieve
32
- * @returns The definition node or unit if not found
28
+ * 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
29
+ * @param variable The variable to get the initializer from
30
+ * @param at The index of the variable definition to get the initializer from
31
+ * @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
33
32
  */
34
- function getVariableDefinitionNodeLoose(variable, at) {
33
+ function getVariableInitializerLoose(variable, at) {
35
34
  if (variable == null) return unit;
36
- const node = getVariableDefinitionNode(variable, at);
35
+ const node = getVariableInitializer(variable, at);
37
36
  if (node != null) return node;
38
37
  const def = variable.defs.at(at);
39
38
  if (def?.type === DefinitionType.Parameter && ast.isFunction(def.node)) return def.node;
@@ -41,7 +40,7 @@ function getVariableDefinitionNodeLoose(variable, at) {
41
40
  }
42
41
 
43
42
  //#endregion
44
- //#region src/var-scope.ts
43
+ //#region src/scope.ts
45
44
  /**
46
45
  * Get all variables from the given scope up to the global scope
47
46
  * @param initialScope The scope to start from
@@ -81,7 +80,7 @@ function getChildScopes(scope) {
81
80
  }
82
81
 
83
82
  //#endregion
84
- //#region src/var-node-equality.ts
83
+ //#region src/value-equality.ts
85
84
  const thisBlockTypes = [
86
85
  AST_NODE_TYPES.FunctionDeclaration,
87
86
  AST_NODE_TYPES.FunctionExpression,
@@ -95,7 +94,7 @@ const thisBlockTypes = [
95
94
  * @param initialScopes initial scopes of the two nodes
96
95
  * @returns `true` if node value equal
97
96
  */
98
- function isNodeEqual(a, b, initialScopes) {
97
+ function isValueEqual(a, b, initialScopes) {
99
98
  a = ast.isTypeExpression(a) ? ast.getUnderlyingExpression(a) : a;
100
99
  b = ast.isTypeExpression(b) ? ast.getUnderlyingExpression(b) : b;
101
100
  const [aScope, bScope] = initialScopes;
@@ -106,8 +105,8 @@ function isNodeEqual(a, b, initialScopes) {
106
105
  case a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier: {
107
106
  const aVar = findVariable(a, aScope);
108
107
  const bVar = findVariable(b, bScope);
109
- const aVarNode = getVariableDefinitionNodeLoose(aVar, 0);
110
- const bVarNode = getVariableDefinitionNodeLoose(bVar, 0);
108
+ const aVarNode = getVariableInitializerLoose(aVar, 0);
109
+ const bVarNode = getVariableInitializerLoose(bVar, 0);
111
110
  const aVarNodeParent = aVarNode?.parent;
112
111
  const bVarNodeParent = bVarNode?.parent;
113
112
  const aDef = aVar?.defs.at(0);
@@ -134,7 +133,7 @@ function isNodeEqual(a, b, initialScopes) {
134
133
  default: return aVar != null && bVar != null && aVar === bVar;
135
134
  }
136
135
  }
137
- case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return ast.isNodeEqual(a.property, b.property) && isNodeEqual(a.object, b.object, initialScopes);
136
+ case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return ast.isNodeEqual(a.property, b.property) && isValueEqual(a.object, b.object, initialScopes);
138
137
  case a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression:
139
138
  if (aScope.block === bScope.block) return true;
140
139
  return ast.findParentNode(a, ast.isOneOf(thisBlockTypes)) === ast.findParentNode(b, ast.isOneOf(thisBlockTypes));
@@ -147,7 +146,7 @@ function isNodeEqual(a, b, initialScopes) {
147
146
  }
148
147
 
149
148
  //#endregion
150
- //#region src/var-assignment-target.ts
149
+ //#region src/binding-assignment.ts
151
150
  /** eslint-disable jsdoc/require-param */
152
151
  /**
153
152
  * Finds the enclosing assignment target (variable, property, etc.) for a given node
@@ -175,11 +174,11 @@ function findEnclosingAssignmentTarget(node) {
175
174
  * @internal
176
175
  */
177
176
  function isAssignmentTargetEqual(context, a, b) {
178
- return ast.isNodeEqual(a, b) || isNodeEqual(a, b, [context.sourceCode.getScope(a), context.sourceCode.getScope(b)]);
177
+ return ast.isNodeEqual(a, b) || isValueEqual(a, b, [context.sourceCode.getScope(a), context.sourceCode.getScope(b)]);
179
178
  }
180
179
 
181
180
  //#endregion
182
- //#region src/var-import-source.ts
181
+ //#region src/binding-import.ts
183
182
  /**
184
183
  * Get the arguments of a require expression
185
184
  * @param node The node to match
@@ -221,7 +220,38 @@ function findImportSource(name, initialScope) {
221
220
  }
222
221
 
223
222
  //#endregion
224
- //#region src/var-object-type.ts
223
+ //#region src/value-property.ts
224
+ /**
225
+ * Find a property by name in an array of properties
226
+ * Handles spread elements by recursively resolving the referenced object
227
+ * @param name The property name to find
228
+ * @param properties The array of properties to search
229
+ * @param initialScope The scope to use for variable resolution
230
+ * @param seen Set of already seen variable names to prevent circular references
231
+ * @returns The found property or unit if not found
232
+ */
233
+ function findProperty(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
234
+ return properties.findLast((prop) => {
235
+ if (prop.type === AST_NODE_TYPES.Property) return "name" in prop.key && prop.key.name === name;
236
+ if (prop.type === AST_NODE_TYPES.SpreadElement) switch (prop.argument.type) {
237
+ case AST_NODE_TYPES.Identifier: {
238
+ if (seen.has(prop.argument.name)) return false;
239
+ const initNode = getVariableInitializer(findVariable(prop.argument.name, initialScope), 0);
240
+ if (initNode?.type === AST_NODE_TYPES.ObjectExpression) {
241
+ seen.add(prop.argument.name);
242
+ return findProperty(name, initNode.properties, initialScope, seen) != null;
243
+ }
244
+ return false;
245
+ }
246
+ case AST_NODE_TYPES.ObjectExpression: return findProperty(name, prop.argument.properties, initialScope, seen) != null;
247
+ default: return false;
248
+ }
249
+ return false;
250
+ });
251
+ }
252
+
253
+ //#endregion
254
+ //#region src/value-type.ts
225
255
  /**
226
256
  * Detect the ObjectType of a given node
227
257
  * @param node The node to check
@@ -267,7 +297,7 @@ function getObjectType(node, initialScope) {
267
297
  return unit;
268
298
  case AST_NODE_TYPES.Identifier:
269
299
  if (!("name" in node) || typeof node.name !== "string") return unit;
270
- return getObjectType(getVariableDefinitionNode(initialScope.set.get(node.name), -1), initialScope);
300
+ return getObjectType(getVariableInitializer(initialScope.set.get(node.name), -1), initialScope);
271
301
  case AST_NODE_TYPES.MemberExpression:
272
302
  if (!("object" in node)) return unit;
273
303
  return getObjectType(node.object, initialScope);
@@ -292,35 +322,4 @@ function getObjectType(node, initialScope) {
292
322
  }
293
323
 
294
324
  //#endregion
295
- //#region src/var-property.ts
296
- /**
297
- * Find a property by name in an array of properties
298
- * Handles spread elements by recursively resolving the referenced object
299
- * @param name The property name to find
300
- * @param properties The array of properties to search
301
- * @param initialScope The scope to use for variable resolution
302
- * @param seen Set of already seen variable names to prevent circular references
303
- * @returns The found property or unit if not found
304
- */
305
- function findProperty(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
306
- return properties.findLast((prop) => {
307
- if (prop.type === AST_NODE_TYPES.Property) return "name" in prop.key && prop.key.name === name;
308
- if (prop.type === AST_NODE_TYPES.SpreadElement) switch (prop.argument.type) {
309
- case AST_NODE_TYPES.Identifier: {
310
- if (seen.has(prop.argument.name)) return false;
311
- const variableNode = getVariableDefinitionNode(findVariable(prop.argument.name, initialScope), 0);
312
- if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) {
313
- seen.add(prop.argument.name);
314
- return findProperty(name, variableNode.properties, initialScope, seen) != null;
315
- }
316
- return false;
317
- }
318
- case AST_NODE_TYPES.ObjectExpression: return findProperty(name, prop.argument.properties, initialScope, seen) != null;
319
- default: return false;
320
- }
321
- return false;
322
- });
323
- }
324
-
325
- //#endregion
326
- export { findEnclosingAssignmentTarget, findImportSource, findProperty, findVariable, getChildScopes, getObjectType, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isAssignmentTargetEqual, isNodeEqual };
325
+ export { findEnclosingAssignmentTarget, findImportSource, findProperty, findVariable, getChildScopes, getObjectType, getVariableInitializer, getVariableInitializerLoose, getVariables, isAssignmentTargetEqual, isValueEqual };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/var",
3
- "version": "3.0.0-next.4",
3
+ "version": "3.0.0-next.41",
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": {
@@ -30,13 +30,13 @@
30
30
  "./package.json"
31
31
  ],
32
32
  "dependencies": {
33
- "@typescript-eslint/scope-manager": "^8.56.0",
34
- "@typescript-eslint/types": "^8.56.0",
35
- "@typescript-eslint/utils": "^8.56.0",
33
+ "@typescript-eslint/scope-manager": "canary",
34
+ "@typescript-eslint/types": "canary",
35
+ "@typescript-eslint/utils": "canary",
36
36
  "ts-pattern": "^5.9.0",
37
- "@eslint-react/ast": "3.0.0-next.4",
38
- "@eslint-react/eff": "3.0.0-next.4",
39
- "@eslint-react/shared": "3.0.0-next.4"
37
+ "@eslint-react/ast": "3.0.0-next.41",
38
+ "@eslint-react/eff": "3.0.0-next.41",
39
+ "@eslint-react/shared": "3.0.0-next.41"
40
40
  },
41
41
  "devDependencies": {
42
42
  "tsdown": "^0.20.3",
@@ -52,6 +52,6 @@
52
52
  "scripts": {
53
53
  "build": "tsdown --dts-resolve",
54
54
  "lint:publish": "publint",
55
- "lint:ts": "tsc --noEmit"
55
+ "lint:ts": "tsl"
56
56
  }
57
57
  }