@eslint-react/var 3.0.0-next.64 → 3.0.0-next.66

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
@@ -1,4 +1,3 @@
1
- import { Scope } from "@typescript-eslint/scope-manager";
2
1
  import { TSESTree } from "@typescript-eslint/types";
3
2
  import { RuleContext } from "@eslint-react/shared";
4
3
 
@@ -34,6 +33,7 @@ type ObjectType = {
34
33
  };
35
34
  /**
36
35
  * Detect the ObjectType of a given node
36
+ * @param context The context of the rule
37
37
  * @param node The node to check
38
38
  * @returns The ObjectType of the node, or undefined if not detected
39
39
  */
@@ -68,12 +68,12 @@ declare function isAssignmentTargetEqual(context: RuleContext, a: TSESTree.Node,
68
68
  //#region src/is-value-equal.d.ts
69
69
  /**
70
70
  * Determine whether node value equals to another node value
71
+ * @param context rule context
71
72
  * @param a node to compare
72
73
  * @param b node to compare
73
- * @param initialScopes initial scopes of the two nodes
74
74
  * @returns `true` if node value equal
75
75
  */
76
- declare function isValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
76
+ declare function isValueEqual(context: RuleContext, a: TSESTree.Node, b: TSESTree.Node): boolean;
77
77
  //#endregion
78
78
  //#region src/resolve.d.ts
79
79
  /**
@@ -98,13 +98,17 @@ declare function isValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes:
98
98
  *
99
99
  * @param context The ESLint rule context used for scope lookup.
100
100
  * @param node The identifier to resolve.
101
- * @param at Which definition to use when multiple exist (default: `0`; pass `-1` for the last).
102
- * @param localOnly When `true`, look up the variable only in the node's own scope (faster, but
101
+ * @param options Optional settings:
102
+ * - `at`: Index of the definition to resolve (default: `0` for the first definition).
103
+ * - `localOnly`: If `true`, only consider variables declared in the same scope as the identifier
103
104
  * will miss variables declared in an outer scope). When `false` (default), traverse the scope
104
105
  * chain upward via `findVariable` so that references to outer-scope bindings are resolved
105
106
  * correctly.
106
107
  * @returns The resolved node, or `null` if the identifier cannot be resolved to a value node.
107
108
  */
108
- declare function resolve(context: RuleContext, node: TSESTree.Identifier, at?: number, localOnly?: boolean): TSESTree.Node | null;
109
+ declare function resolve(context: RuleContext, node: TSESTree.Identifier, options?: Partial<{
110
+ at: number;
111
+ localOnly: boolean;
112
+ }>): TSESTree.Node | null;
109
113
  //#endregion
110
114
  export { AssignmentTarget, ObjectType, computeObjectType, findEnclosingAssignmentTarget, isAssignmentTargetEqual, isValueEqual, resolve };
package/dist/index.js CHANGED
@@ -26,14 +26,16 @@ import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils
26
26
  *
27
27
  * @param context The ESLint rule context used for scope lookup.
28
28
  * @param node The identifier to resolve.
29
- * @param at Which definition to use when multiple exist (default: `0`; pass `-1` for the last).
30
- * @param localOnly When `true`, look up the variable only in the node's own scope (faster, but
29
+ * @param options Optional settings:
30
+ * - `at`: Index of the definition to resolve (default: `0` for the first definition).
31
+ * - `localOnly`: If `true`, only consider variables declared in the same scope as the identifier
31
32
  * will miss variables declared in an outer scope). When `false` (default), traverse the scope
32
33
  * chain upward via `findVariable` so that references to outer-scope bindings are resolved
33
34
  * correctly.
34
35
  * @returns The resolved node, or `null` if the identifier cannot be resolved to a value node.
35
36
  */
36
- function resolve(context, node, at = 0, localOnly = false) {
37
+ function resolve(context, node, options) {
38
+ const { at = 0, localOnly = false } = options ?? {};
37
39
  const scope = context.sourceCode.getScope(node);
38
40
  const variable = localOnly ? scope.set.get(node.name) : findVariable(scope, node);
39
41
  if (variable == null) return null;
@@ -64,6 +66,7 @@ function resolve(context, node, at = 0, localOnly = false) {
64
66
  //#region src/compute-object-type.ts
65
67
  /**
66
68
  * Detect the ObjectType of a given node
69
+ * @param context The context of the rule
67
70
  * @param node The node to check
68
71
  * @returns The ObjectType of the node, or undefined if not detected
69
72
  */
@@ -106,7 +109,10 @@ function computeObjectType(context, node) {
106
109
  return null;
107
110
  case AST_NODE_TYPES.Identifier: {
108
111
  if ((context.sourceCode.getScope(node).set.get(node.name)?.defs.at(-1))?.type === DefinitionType.Parameter) return null;
109
- const initNode = resolve(context, node, -1, true);
112
+ const initNode = resolve(context, node, {
113
+ at: -1,
114
+ localOnly: true
115
+ });
110
116
  if (initNode == null) return null;
111
117
  return computeObjectType(context, initNode);
112
118
  }
@@ -162,15 +168,15 @@ const thisBlockTypes = [
162
168
  ];
163
169
  /**
164
170
  * Determine whether node value equals to another node value
171
+ * @param context rule context
165
172
  * @param a node to compare
166
173
  * @param b node to compare
167
- * @param initialScopes initial scopes of the two nodes
168
174
  * @returns `true` if node value equal
169
175
  */
170
- function isValueEqual(a, b, initialScopes) {
176
+ function isValueEqual(context, a, b) {
171
177
  a = ast.isTypeExpression(a) ? ast.getUnderlyingExpression(a) : a;
172
178
  b = ast.isTypeExpression(b) ? ast.getUnderlyingExpression(b) : b;
173
- const [aScope, bScope] = initialScopes;
179
+ const [aScope, bScope] = [context.sourceCode.getScope(a), context.sourceCode.getScope(b)];
174
180
  switch (true) {
175
181
  case a === b: return true;
176
182
  case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal: return a.value === b.value;
@@ -217,7 +223,7 @@ function isValueEqual(a, b, initialScopes) {
217
223
  default: return aVar != null && bVar != null && aVar === bVar;
218
224
  }
219
225
  }
220
- 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);
226
+ case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return ast.isNodeEqual(a.property, b.property) && isValueEqual(context, a.object, b.object);
221
227
  case a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression:
222
228
  if (aScope.block === bScope.block) return true;
223
229
  return ast.findParentNode(a, ast.isOneOf(thisBlockTypes)) === ast.findParentNode(b, ast.isOneOf(thisBlockTypes));
@@ -241,7 +247,7 @@ function isValueEqual(a, b, initialScopes) {
241
247
  * @internal
242
248
  */
243
249
  function isAssignmentTargetEqual(context, a, b) {
244
- return ast.isNodeEqual(a, b) || isValueEqual(a, b, [context.sourceCode.getScope(a), context.sourceCode.getScope(b)]);
250
+ return ast.isNodeEqual(a, b) || isValueEqual(context, a, b);
245
251
  }
246
252
 
247
253
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/var",
3
- "version": "3.0.0-next.64",
3
+ "version": "3.0.0-next.66",
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/ast": "3.0.0-next.64",
38
- "@eslint-react/eff": "3.0.0-next.64",
39
- "@eslint-react/shared": "3.0.0-next.64"
37
+ "@eslint-react/ast": "3.0.0-next.66",
38
+ "@eslint-react/eff": "3.0.0-next.66",
39
+ "@eslint-react/shared": "3.0.0-next.66"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@typescript-eslint/typescript-estree": "canary",