@eslint-react/var 5.30.0-beta.2 → 5.30.0-beta.20

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.
Files changed (3) hide show
  1. package/dist/index.d.ts +188 -68
  2. package/dist/index.js +238 -127
  3. package/package.json +8 -8
package/dist/index.d.ts CHANGED
@@ -8,26 +8,23 @@ import { RuleContext } from "@eslint-react/eslint";
8
8
  * specifier (or pre-registered builtin namespace), and every query on
9
9
  * {@link ImportLookup} is answered from them.
10
10
  */
11
- export interface ImportEntry {
12
- /**
13
- * The import form.
14
- *
15
- * `"named"` covers both named imports and default imports; a default import
16
- * is distinguished by its {@link ImportEntry.name} being `"default"`.
17
- */
18
- kind: "named" | "namespace";
19
- /**
20
- * The name of the imported export.
21
- *
22
- * `"default"` for default imports and `""` for namespace imports, which
23
- * bind the module as a whole rather than a single export.
24
- */
25
- name: string;
11
+ export type ImportEntry = {
12
+ /** A named or default import; both are bound to a single export. */
13
+ readonly kind: "named" | "default";
14
+ /** The name of the imported export (`"default"` for default imports). */
15
+ readonly name: string;
26
16
  /** The local name the import is bound to (the alias when one is used). */
27
- local: string;
17
+ readonly local: string;
28
18
  /** The module specifier as written in the source, e.g. `"react-dom/client"`. */
29
- specifier: string;
30
- }
19
+ readonly specifier: string;
20
+ } | {
21
+ /** A namespace import, which binds the module as a whole. */
22
+ readonly kind: "namespace";
23
+ /** The local name the import is bound to. */
24
+ readonly local: string;
25
+ /** The module specifier as written in the source. */
26
+ readonly specifier: string;
27
+ };
31
28
  /**
32
29
  * A read-only index over the local import bindings of a program.
33
30
  *
@@ -41,30 +38,40 @@ export interface ImportLookup {
41
38
  * Look up the import entry a local name is bound to.
42
39
  *
43
40
  * @param local - The local binding name.
44
- * @returns The matching entry, or `undefined` if the name is not imported.
41
+ * @returns The matching entry, or `null` if the name is not imported.
45
42
  */
46
- binding(local: string): ImportEntry | undefined;
43
+ binding(local: string): ImportEntry | null;
47
44
  /**
48
45
  * Look up all local bindings of a given imported export name.
49
46
  *
50
- * @param name - The imported export name (`"default"` for default imports).
47
+ * @param name - The imported export name (`"default"` for default imports,
48
+ * `""` for namespace imports).
51
49
  * @returns The matching entries in source order, possibly empty.
52
50
  */
53
51
  bindingsOf(name: string): readonly ImportEntry[];
54
52
  /**
55
53
  * Check whether a local name is bound to a specific imported export.
56
54
  *
55
+ * `"default"` matches default imports; namespace imports never match because
56
+ * they bind the module as a whole rather than a single export.
57
+ *
57
58
  * @param local - The local binding name.
58
59
  * @param name - The imported export name.
59
60
  */
60
61
  has(local: string, name: string): boolean;
61
62
  /**
62
- * Check whether a local name is a default or namespace binding, i.e. one
63
- * that refers to the module as a whole rather than to a single named export.
63
+ * Check whether a local name is a default import.
64
+ *
65
+ * @param local - The local binding name.
66
+ */
67
+ hasDefault(local: string): boolean;
68
+ /**
69
+ * Check whether a local name refers to the module as a whole, i.e. it is a
70
+ * default or namespace binding rather than a single named export.
64
71
  *
65
72
  * @param local - The local binding name.
66
73
  */
67
- hasNamespace(local: string): boolean;
74
+ isModuleBinding(local: string): boolean;
68
75
  }
69
76
  /** Options for {@link createImportLookup}. */
70
77
  export interface ImportLookupOptions {
@@ -77,7 +84,8 @@ export interface ImportLookupOptions {
77
84
  * The base import source to track, e.g. `"react-dom"`.
78
85
  *
79
86
  * Subpath imports are grouped under their base, so `"react-dom/client"`
80
- * matches a source of `"react-dom"`.
87
+ * matches a source of `"react-dom"`. Scoped package names such as
88
+ * `"@scope/pkg"` are matched as a whole.
81
89
  */
82
90
  source: string;
83
91
  }
@@ -90,6 +98,8 @@ export interface ImportLookupOptions {
90
98
  * immediately after creation. Aliases (`import { flushSync as fs }`) are
91
99
  * handled naturally: the alias is the entry's local name.
92
100
  *
101
+ * Type-only imports are ignored because they never bind a runtime value.
102
+ *
93
103
  * @param program - The program whose top-level import declarations to scan.
94
104
  * @param options - The source to track and optional builtin namespaces.
95
105
  * @returns An {@link ImportLookup} over the matching import bindings.
@@ -119,6 +129,12 @@ export declare function createImportLookup(program: TSESTree.Program, options: I
119
129
  export declare function isAssignmentTargetEqual(context: RuleContext, a: TSESTree.Node, b: TSESTree.Node): boolean;
120
130
  /**
121
131
  * Check if a variable is initialized from a React import.
132
+ *
133
+ * A local whose name is `react` (case-insensitive) is treated as the React
134
+ * global. Otherwise the variable's import or require source must start with
135
+ * `importSource`; this is a prefix match, so `"react-dom"` also matches the
136
+ * default `"react"`. Pass a more specific source to narrow the match.
137
+ *
122
138
  * @param name The variable name.
123
139
  * @param initialScope The initial scope.
124
140
  * @param importSource Alternative import source of React (ex: "preact/compat").
@@ -128,6 +144,12 @@ export declare function isAssignmentTargetEqual(context: RuleContext, a: TSESTre
128
144
  export declare function isInitializedFromReact(name: string, initialScope: Scope, importSource?: string): boolean;
129
145
  /**
130
146
  * Check if a variable is initialized from a React Native import.
147
+ *
148
+ * Locals named `react_native`, `reactnative`, or `rn` (case-insensitive) are
149
+ * treated as React Native globals. Otherwise the variable's import or require
150
+ * source must start with `importSource`; this is a prefix match, so
151
+ * `"react-native-web"` also matches the default `"react-native"`.
152
+ *
131
153
  * @param name The variable name.
132
154
  * @param initialScope The initial scope.
133
155
  * @param importSource Alternative import source of React Native (ex: "react-native-web").
@@ -135,14 +157,68 @@ export declare function isInitializedFromReact(name: string, initialScope: Scope
135
157
  * @internal
136
158
  */
137
159
  export declare function isInitializedFromReactNative(name: string, initialScope: Scope, importSource?: string): boolean;
160
+ /**
161
+ * Check if two nodes refer to the same value or share provenance.
162
+ *
163
+ * This extends {@link isValueEqual} with two rules for identifiers:
164
+ *
165
+ * - Parameters at the same position of callbacks passed to structurally equal
166
+ * callees are equal (e.g. `xs.forEach((a) => ...)` and `xs.forEach((b) => ...)`).
167
+ * - Variables bound by different `for-of` statements iterating over the same
168
+ * source are equal (e.g. `for (const event of xs)` and `for (const evt of xs)`).
169
+ * Variables from the same `for-of` statement remain distinct.
170
+ *
171
+ * Use this to match a value at its use site against the site that tears it
172
+ * down (event listeners, timers, observers, and similar resources).
173
+ *
174
+ * @param context The ESLint rule context.
175
+ * @param a The first node to compare.
176
+ * @param b The second node to compare.
177
+ * @returns `true` if the two nodes refer to the same value or share provenance.
178
+ */
179
+ export declare function isProvenanceEqual(context: RuleContext, a: TSESTree.Node, b: TSESTree.Node): boolean;
138
180
  /**
139
181
  * Check if two nodes have equal values.
182
+ *
183
+ * Identifiers are compared by variable identity through the scope manager:
184
+ * identical names in different scopes are different values, and two distinct
185
+ * variables holding the same literal are not equal.
186
+ *
187
+ * This function does not apply provenance heuristics. Use
188
+ * {@link isProvenanceEqual} when bindings that share provenance (for example
189
+ * callback parameters or loop variables over the same source) should also be
190
+ * treated as equal.
191
+ *
140
192
  * @param context The ESLint rule context.
141
193
  * @param a The first node to compare.
142
194
  * @param b The second node to compare.
143
195
  * @returns `true` if the two nodes have equal values.
144
196
  */
145
197
  export declare function isValueEqual(context: RuleContext, a: TSESTree.Node, b: TSESTree.Node): boolean;
198
+ /**
199
+ * Options shared by the identifier-resolution helpers.
200
+ *
201
+ * Used by {@link resolve}, {@link resolveOrigin}, and the internal binding
202
+ * helpers.
203
+ */
204
+ interface ResolveOptions {
205
+ /**
206
+ * Index of the definition to select (default: `0`; negative indexes count from the end).
207
+ *
208
+ * `resolve` and `resolveOrigin` default to the first definition.
209
+ * `resolveOriginChain` applies this index at every step. `resolveObjectType`
210
+ * defaults to `-1` (the latest definition).
211
+ */
212
+ at?: number;
213
+ /**
214
+ * Only look up the variable in the identifier's own scope (default: `false`).
215
+ *
216
+ * When `false`, the scope chain is traversed upward so references to
217
+ * outer-scope bindings are resolved. `resolveObjectType` exposes the same
218
+ * concept as `scope: "local" | "chain"`.
219
+ */
220
+ localOnly?: boolean;
221
+ }
146
222
  /**
147
223
  * Resolve an identifier to the AST node that represents its value,
148
224
  * suitable for use in ESLint rule analysis.
@@ -165,73 +241,100 @@ export declare function isValueEqual(context: RuleContext, a: TSESTree.Node, b:
165
241
  *
166
242
  * @param context The ESLint rule context.
167
243
  * @param node The identifier to resolve.
168
- * @param options Optional settings:
169
- * - `at`: Index of the definition to resolve (default: `0` for the first definition).
170
- * - `localOnly`: If `true`, only consider variables declared in the same scope as the identifier
171
- * (this will miss variables declared in an outer scope). When `false` (default), traverse the
172
- * scope chain upward via `findVariable` so that references to outer-scope bindings are resolved
173
- * correctly.
244
+ * @param options Optional resolution settings; see {@link ResolveOptions}.
174
245
  * @returns The resolved node, or `null` if the identifier cannot be resolved to a value node.
175
246
  *
176
247
  * For origin/pedigree tracking that maps destructured bindings to the declarator's
177
248
  * initializer, see {@link resolveOrigin}.
178
249
  */
179
- export declare function resolve(context: RuleContext, node: TSESTree.Identifier, options?: Partial<{
180
- at: number;
181
- localOnly: boolean;
182
- }>): TSESTree.Node | null;
250
+ export declare function resolve(context: RuleContext, node: TSESTree.Identifier, options?: ResolveOptions): TSESTree.Node | null;
251
+ /**
252
+ * Resolve every write target of an assignment or loop target, expanding
253
+ * destructuring patterns such as `[local, globalValue] = source` or
254
+ * `({ a: obj.x } = source)`. Defaults (`{ a = source }`) are not targets;
255
+ * their left side is.
256
+ * @param node The assignment left side or for-in/of loop target to inspect. Declaration-form loop targets are not expanded and return an empty array.
257
+ * @returns The identifier and member expression targets being written.
258
+ */
259
+ export declare function resolveAssignmentTargets(node: TSESTree.Node): (TSESTree.Identifier | TSESTree.MemberExpression)[];
183
260
  /** The possible assignment targets returned by {@link resolveEnclosingAssignmentTarget}. */
184
- export type EnclosingAssignmentTarget = ReturnType<typeof resolveEnclosingAssignmentTarget>;
261
+ export type EnclosingAssignmentTarget = TSESTree.DefaultExportDeclarations | TSESTree.PropertyName | null;
185
262
  /**
186
263
  * Resolve the enclosing assignment target (variable, property, etc.) of the node.
187
264
  * @param node The starting node for the upward search.
188
265
  * @returns The enclosing assignment target node, or `null` when not found.
189
266
  */
190
- export declare function resolveEnclosingAssignmentTarget(node: TSESTree.Node): TSESTree.ArrayExpression | TSESTree.ArrayPattern | TSESTree.ArrowFunctionExpressionWithBlockBody | TSESTree.ArrowFunctionExpressionWithExpressionBody | TSESTree.AssignmentExpression | TSESTree.AwaitExpression | TSESTree.PrivateInExpression | TSESTree.SymmetricBinaryExpression | TSESTree.CallExpression | TSESTree.ChainExpression | TSESTree.ClassDeclarationWithOptionalName | TSESTree.ClassExpression | TSESTree.ConditionalExpression | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName | 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.TSDeclareFunctionNoDeclare | TSESTree.TSDeclareFunctionWithDeclare | TSESTree.TSEnumDeclaration | TSESTree.TSInstantiationExpression | TSESTree.TSInterfaceDeclaration | TSESTree.TSModuleDeclarationGlobal | TSESTree.TSModuleDeclarationModuleWithIdentifierId | TSESTree.TSModuleDeclarationModuleWithStringIdDeclared | TSESTree.TSModuleDeclarationModuleWithStringIdNotDeclared | TSESTree.TSModuleDeclarationNamespace | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAliasDeclaration | TSESTree.TSTypeAssertion | TSESTree.UnaryExpressionBitwiseNot | TSESTree.UnaryExpressionDelete | TSESTree.UnaryExpressionMinus | TSESTree.UnaryExpressionNot | TSESTree.UnaryExpressionPlus | TSESTree.UnaryExpressionTypeof | TSESTree.UnaryExpressionVoid | TSESTree.UpdateExpression | TSESTree.ConstDeclaration | TSESTree.LetOrVarDeclaredDeclaration | TSESTree.LetOrVarNonDeclaredDeclaration | TSESTree.UsingInForOfDeclaration | TSESTree.UsingInNormalContextDeclaration | TSESTree.YieldNoStarExpression | TSESTree.YieldStarExpression | null;
267
+ export declare function resolveEnclosingAssignmentTarget(node: TSESTree.Node): EnclosingAssignmentTarget;
191
268
  /**
192
269
  * Resolve the import source of a variable by walking its latest definition.
193
270
  * @param name The variable name.
194
271
  * @param initialScope The initial scope.
195
- * @param seen The set of already visited variable names (for cycle detection).
196
272
  * @returns The import source, or `null` when it cannot be resolved.
197
273
  */
198
- export declare function resolveImportSource(name: string, initialScope: Scope, seen?: Set<string>): string | null;
274
+ export declare function resolveImportSource(name: string, initialScope: Scope): string | null;
199
275
  /**
200
276
  * Represents the type classification of an object node.
277
+ *
278
+ * `via` records how a known kind was constructed (a literal, a call, a
279
+ * declaration, ...), so consumers can distinguish e.g. `[]` from `Array()`
280
+ * without inspecting `node.type`. `unknown` carries a typed `reason` instead.
201
281
  */
202
282
  export type ObjectType = {
203
- kind: "jsx";
204
- node: TSESTree.JSXElement | TSESTree.JSXFragment;
283
+ readonly kind: "jsx";
284
+ readonly node: TSESTree.JSXElement | TSESTree.JSXFragment;
285
+ readonly via: "jsx";
205
286
  } | {
206
- kind: "array";
207
- node: TSESTree.ArrayExpression | TSESTree.CallExpression;
287
+ readonly kind: "array";
288
+ readonly node: TSESTree.ArrayExpression | TSESTree.CallExpression;
289
+ readonly via: "literal" | "call";
208
290
  } | {
209
- kind: "plain";
210
- node: TSESTree.ObjectExpression | TSESTree.CallExpression;
291
+ readonly kind: "plain";
292
+ readonly node: TSESTree.ObjectExpression | TSESTree.CallExpression;
293
+ readonly via: "literal" | "call";
211
294
  } | {
212
- kind: "class";
213
- node: TSESTree.ClassDeclaration | TSESTree.ClassExpression;
295
+ readonly kind: "class";
296
+ readonly node: TSESTree.ClassDeclaration | TSESTree.ClassExpression;
297
+ readonly via: "declaration" | "expression";
214
298
  } | {
215
- kind: "instance";
216
- node: TSESTree.NewExpression | TSESTree.ThisExpression;
299
+ readonly kind: "instance";
300
+ readonly node: TSESTree.NewExpression | TSESTree.ThisExpression;
301
+ readonly via: "new" | "this";
217
302
  } | {
218
- kind: "function";
219
- node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression;
303
+ readonly kind: "function";
304
+ readonly node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression;
305
+ readonly via: "declaration" | "expression" | "arrow";
220
306
  } | {
221
- kind: "regexp";
222
- node: TSESTree.RegExpLiteral | TSESTree.CallExpression;
307
+ readonly kind: "regexp";
308
+ readonly node: TSESTree.RegExpLiteral | TSESTree.CallExpression;
309
+ readonly via: "literal" | "call";
223
310
  } | {
224
- kind: "unknown";
225
- node: TSESTree.Node;
226
- reason?: string;
311
+ readonly kind: "unknown";
312
+ readonly node: TSESTree.Node;
313
+ /** Why the node could not be classified. */
314
+ readonly reason: "call-expression";
227
315
  };
316
+ /** Options for {@link resolveObjectType}. */
317
+ export interface ResolveObjectTypeOptions {
318
+ /** Index of the definition to resolve (default: `-1`, the latest definition). */
319
+ at?: number;
320
+ /**
321
+ * Scope traversal mode.
322
+ *
323
+ * - `"local"`: only follow bindings declared in the same scope as the identifier (default).
324
+ * - `"chain"`: also follow bindings declared in enclosing scopes.
325
+ *
326
+ * This maps internally to `localOnly` in {@link ResolveOptions}.
327
+ */
328
+ scope?: "local" | "chain";
329
+ }
228
330
  /**
229
331
  * Resolve the object type of the node.
230
332
  * @param context The ESLint rule context.
231
333
  * @param node The node to resolve.
334
+ * @param options Optional resolution settings; see {@link ResolveObjectTypeOptions}.
232
335
  * @returns The object type of the node, or `null` when it cannot be resolved.
233
336
  */
234
- export declare function resolveObjectType(context: RuleContext, node: TSESTree.Node | null | undefined): ObjectType | null;
337
+ export declare function resolveObjectType(context: RuleContext, node: TSESTree.Node | null | undefined, options?: ResolveObjectTypeOptions): ObjectType | null;
235
338
  /**
236
339
  * Resolve an identifier to the AST node its value **originates from**,
237
340
  * suitable for origin/pedigree tracking in ESLint rule analysis.
@@ -261,19 +364,36 @@ export declare function resolveObjectType(context: RuleContext, node: TSESTree.N
261
364
  * `ImportDeclaration` carries the module source) instead of `null`.
262
365
  *
263
366
  * Use this for origin/pedigree tracking ("what produced this value?"); use
264
- * {@link resolve} when the precise value of the binding is needed.
367
+ * {@link resolve} when the precise value of the binding is needed. To follow a
368
+ * chain of aliases to a fixed point, use {@link resolveOriginChain}.
265
369
  *
266
370
  * @param context The ESLint rule context.
267
371
  * @param node The identifier to resolve.
268
- * @param options Optional settings:
269
- * - `at`: Index of the definition to resolve (default: `0` for the first definition).
270
- * - `localOnly`: If `true`, only consider variables declared in the same scope as the identifier
271
- * (this will miss variables declared in an outer scope). When `false` (default), traverse the
272
- * scope chain upward via `findVariable` so that references to outer-scope bindings are resolved
273
- * correctly.
372
+ * @param options Optional resolution settings; see {@link ResolveOptions}.
274
373
  * @returns The resolved origin node, or `null` if the identifier cannot be resolved.
275
374
  */
276
- export declare function resolveOrigin(context: RuleContext, node: TSESTree.Identifier, options?: Partial<{
277
- at: number;
278
- localOnly: boolean;
279
- }>): TSESTree.Node | null;
375
+ export declare function resolveOrigin(context: RuleContext, node: TSESTree.Identifier, options?: ResolveOptions): TSESTree.Node | null;
376
+ /** Options for {@link resolveOriginChain}. */
377
+ export interface ResolveOriginChainOptions extends ResolveOptions {
378
+ /** Maximum number of resolution steps before giving up (default: `32`). */
379
+ maxDepth?: number;
380
+ }
381
+ /**
382
+ * Follow an identifier through an alias chain to the node its value ultimately
383
+ * originates from.
384
+ *
385
+ * Each step uses {@link resolveOrigin} and unwraps TypeScript type and chain
386
+ * expressions, so `const a = b as T` continues through `b`. Resolution stops
387
+ * when a step yields a non-identifier node, a `null` origin, a repeated node
388
+ * (cycle), or after `maxDepth` steps.
389
+ *
390
+ * Use this instead of calling {@link resolveOrigin} in a loop: it provides
391
+ * cycle protection and a bounded traversal.
392
+ *
393
+ * @param context The ESLint rule context.
394
+ * @param node The identifier to start from.
395
+ * @param options Optional chain-resolution settings; see {@link ResolveOriginChainOptions}.
396
+ * @returns The first non-identifier origin node, or `null` when the chain cannot be resolved.
397
+ */
398
+ export declare function resolveOriginChain(context: RuleContext, node: TSESTree.Identifier, options?: ResolveOriginChainOptions): TSESTree.Node | null;
399
+ export type { ResolveOptions };
package/dist/index.js CHANGED
@@ -109,23 +109,33 @@ function createImportLookup(program, options) {
109
109
  const add = (entry) => {
110
110
  entries.push(entry);
111
111
  byLocal.set(entry.local, entry);
112
- getOrInsertComputed(byName, entry.name, () => []).push(entry);
112
+ const name = entry.kind === "namespace" ? "" : entry.name;
113
+ getOrInsertComputed(byName, name, () => []).push(entry);
113
114
  };
114
115
  for (const local of builtinNamespaces) add({
115
116
  kind: "namespace",
116
- name: "",
117
117
  local,
118
118
  specifier: source
119
119
  });
120
120
  for (const statement of program.body) {
121
121
  if (statement.type !== AST_NODE_TYPES.ImportDeclaration) continue;
122
+ if (statement.importKind === "type") continue;
122
123
  const specifier = statement.source.value;
123
- const [baseSource = ""] = specifier.split("/");
124
- if (baseSource !== source) continue;
124
+ if (specifier !== source && !specifier.startsWith(`${source}/`)) continue;
125
125
  for (const specifierNode of statement.specifiers) switch (specifierNode.type) {
126
126
  case AST_NODE_TYPES.ImportSpecifier: {
127
127
  const { imported, local } = specifierNode;
128
+ if (specifierNode.importKind === "type") continue;
128
129
  if (imported.type !== AST_NODE_TYPES.Identifier) continue;
130
+ if (imported.name === "default") {
131
+ add({
132
+ kind: "default",
133
+ name: "default",
134
+ local: local.name,
135
+ specifier
136
+ });
137
+ continue;
138
+ }
129
139
  add({
130
140
  kind: "named",
131
141
  name: imported.name,
@@ -136,7 +146,7 @@ function createImportLookup(program, options) {
136
146
  }
137
147
  case AST_NODE_TYPES.ImportDefaultSpecifier:
138
148
  add({
139
- kind: "named",
149
+ kind: "default",
140
150
  name: "default",
141
151
  local: specifierNode.local.name,
142
152
  specifier
@@ -145,7 +155,6 @@ function createImportLookup(program, options) {
145
155
  case AST_NODE_TYPES.ImportNamespaceSpecifier:
146
156
  add({
147
157
  kind: "namespace",
148
- name: "",
149
158
  local: specifierNode.local.name,
150
159
  specifier
151
160
  });
@@ -157,113 +166,142 @@ function createImportLookup(program, options) {
157
166
  return entries;
158
167
  },
159
168
  binding(local) {
160
- return byLocal.get(local);
169
+ return byLocal.get(local) ?? null;
161
170
  },
162
171
  bindingsOf(name) {
163
172
  return byName.get(name) ?? [];
164
173
  },
165
174
  has(local, name) {
166
- return byLocal.get(local)?.name === name;
175
+ const entry = byLocal.get(local);
176
+ if (entry == null) return false;
177
+ return entry.kind === "namespace" ? false : entry.name === name;
178
+ },
179
+ hasDefault(local) {
180
+ return byLocal.get(local)?.kind === "default";
167
181
  },
168
- hasNamespace(local) {
182
+ isModuleBinding(local) {
169
183
  const entry = byLocal.get(local);
170
- return entry != null && (entry.kind === "namespace" || entry.name === "default");
184
+ return entry != null && entry.kind !== "named";
171
185
  }
172
186
  };
173
187
  }
174
- // src/resolve-origin.ts
175
- function resolveOrigin(context, node, options) {
188
+ // src/resolve-binding.ts
189
+ function getBindingFromScope(scope, node, options) {
176
190
  const { at = 0, localOnly = false } = options ?? {};
177
- const scope = context.sourceCode.getScope(node);
178
191
  const variable = localOnly ? scope.set.get(node.name) : findVariable(scope, node);
179
192
  if (variable == null) return null;
180
- const def = variable.defs.at(at);
181
- if (def == null) return null;
182
- switch (def.type) {
183
- case DefinitionType.FunctionName: return def.node;
184
- case DefinitionType.ClassName: return def.node;
193
+ return {
194
+ definition: variable.defs.at(at) ?? null,
195
+ variable
196
+ };
197
+ }
198
+ function getBinding(context, node, options) {
199
+ return getBindingFromScope(context.sourceCode.getScope(node), node, options);
200
+ }
201
+ function projectBinding(definition, projection) {
202
+ switch (definition.type) {
203
+ case DefinitionType.FunctionName:
204
+ case DefinitionType.ClassName: return definition.node;
185
205
  case DefinitionType.Variable: {
186
- const { init } = def.node;
206
+ const { id, init } = definition.node;
187
207
  if (init == null) return null;
208
+ if (projection === "value" && id !== definition.name) return null;
188
209
  if (hasProperty(init, "declarations")) return null;
189
210
  return init;
190
211
  }
191
- case DefinitionType.Parameter: return Check.isFunction(def.node) ? def.node : null;
192
- case DefinitionType.TSEnumName: return def.node;
193
- case DefinitionType.TSEnumMember: return def.node.initializer ?? null;
194
- case DefinitionType.ImportBinding: return def.node;
195
- case DefinitionType.CatchClause: return null;
196
- case DefinitionType.TSModuleName: return null;
212
+ case DefinitionType.Parameter: return projection === "origin" && Check.isFunction(definition.node) ? definition.node : null;
213
+ case DefinitionType.TSEnumName: return definition.node;
214
+ case DefinitionType.TSEnumMember: return definition.node.initializer ?? null;
215
+ case DefinitionType.ImportBinding: return projection === "origin" ? definition.node : null;
216
+ case DefinitionType.CatchClause:
217
+ case DefinitionType.ImplicitGlobalVariable:
218
+ case DefinitionType.TSModuleName:
197
219
  case DefinitionType.Type: return null;
198
- case DefinitionType.ImplicitGlobalVariable: return null;
199
220
  default: return null;
200
221
  }
201
222
  }
202
- // src/is-value-equal.ts
223
+ // src/value-equality.ts
224
+ function isRegExpLiteral(node) {
225
+ return hasProperty(node, "regex");
226
+ }
203
227
  const thisBlockTypes = [
204
- AST_NODE_TYPES.FunctionDeclaration,
205
228
  AST_NODE_TYPES.FunctionExpression,
229
+ AST_NODE_TYPES.FunctionDeclaration,
206
230
  AST_NODE_TYPES.ClassBody,
207
231
  AST_NODE_TYPES.Program
208
232
  ];
209
- function isValueEqual(context, a, b) {
233
+ function isEqualWith(context, a, b, options) {
234
+ const { identifierComparison = "identity" } = options ?? {};
210
235
  a = Check.isTypeExpression(a) ? Extract.unwrap(a) : a;
211
236
  b = Check.isTypeExpression(b) ? Extract.unwrap(b) : b;
237
+ a = a.type === AST_NODE_TYPES.ChainExpression ? a.expression : a;
238
+ b = b.type === AST_NODE_TYPES.ChainExpression ? b.expression : b;
212
239
  const [aScope, bScope] = [context.sourceCode.getScope(a), context.sourceCode.getScope(b)];
213
240
  switch (true) {
214
241
  case a === b: return true;
215
- case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal: return a.value === b.value;
242
+ case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal:
243
+ if (isRegExpLiteral(a)) return isRegExpLiteral(b) && a.regex.pattern === b.regex.pattern && a.regex.flags === b.regex.flags;
244
+ return !isRegExpLiteral(b) && a.value === b.value;
216
245
  case a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement: return a.value.cooked === b.value.cooked;
217
246
  case Check.isIdentifier(a) && Check.isIdentifier(b): {
218
- const aDefNode = resolveOrigin(context, a);
219
- const bDefNode = resolveOrigin(context, b);
220
- const aDefNodeParent = aDefNode?.parent;
221
- const bDefNodeParent = bDefNode?.parent;
222
- const aVar = findVariable(aScope, a);
223
- const bVar = findVariable(bScope, b);
224
- const aDef = aVar?.defs.at(0);
225
- const bDef = bVar?.defs.at(0);
226
- const aDefParentParent = aDef?.parent?.parent;
227
- const bDefParentParent = bDef?.parent?.parent;
228
- switch (true) {
229
- case aDefNodeParent?.type === AST_NODE_TYPES.CallExpression && bDefNodeParent?.type === AST_NODE_TYPES.CallExpression && Check.isFunction(aDefNode) && Check.isFunction(bDefNode): {
230
- if (!Compare.isEqual(aDefNodeParent.callee, bDefNodeParent.callee)) return false;
231
- const aParams = aDefNode.params;
232
- const bParams = bDefNode.params;
233
- const aPos = aParams.findIndex((x) => Compare.isEqual(x, a));
234
- const bPos = bParams.findIndex((x) => Compare.isEqual(x, b));
235
- return aPos !== -1 && bPos !== -1 && aPos === bPos;
247
+ const aBinding = getBindingFromScope(aScope, a);
248
+ const bBinding = getBindingFromScope(bScope, b);
249
+ if (identifierComparison === "provenance") {
250
+ const aDefNode = aBinding?.definition == null ? null : projectBinding(aBinding.definition, "origin");
251
+ const bDefNode = bBinding?.definition == null ? null : projectBinding(bBinding.definition, "origin");
252
+ const aDefNodeParent = aDefNode?.parent;
253
+ const bDefNodeParent = bDefNode?.parent;
254
+ const aDef = aBinding?.definition;
255
+ const bDef = bBinding?.definition;
256
+ const aDefParentParent = aDef?.parent?.parent;
257
+ const bDefParentParent = bDef?.parent?.parent;
258
+ switch (true) {
259
+ case aDefNodeParent?.type === AST_NODE_TYPES.CallExpression && bDefNodeParent?.type === AST_NODE_TYPES.CallExpression && Check.isFunction(aDefNode) && Check.isFunction(bDefNode): {
260
+ if (!Compare.isEqual(aDefNodeParent.callee, bDefNodeParent.callee)) return false;
261
+ const aParams = aDefNode.params;
262
+ const bParams = bDefNode.params;
263
+ const aPos = aParams.findIndex((x) => Compare.isEqual(x, a));
264
+ const bPos = bParams.findIndex((x) => Compare.isEqual(x, b));
265
+ return aPos !== -1 && bPos !== -1 && aPos === bPos;
266
+ }
267
+ case aDefParentParent?.type === AST_NODE_TYPES.ForOfStatement && bDefParentParent?.type === AST_NODE_TYPES.ForOfStatement: {
268
+ const aLeft = aDefParentParent.left;
269
+ const bLeft = bDefParentParent.left;
270
+ if (aLeft.type !== bLeft.type) return false;
271
+ const aRight = aDefParentParent.right;
272
+ const bRight = bDefParentParent.right;
273
+ if (!Compare.isEqual(aRight, bRight)) return false;
274
+ if (aDefParentParent === bDefParentParent) return aBinding != null && bBinding != null && aBinding.variable === bBinding.variable;
275
+ return true;
276
+ }
236
277
  }
237
- case aDefParentParent?.type === AST_NODE_TYPES.ForOfStatement && bDefParentParent?.type === AST_NODE_TYPES.ForOfStatement: {
238
- const aLeft = aDefParentParent.left;
239
- const bLeft = bDefParentParent.left;
240
- if (aLeft.type !== bLeft.type) return false;
241
- const aRight = aDefParentParent.right;
242
- const bRight = bDefParentParent.right;
243
- if (!Compare.isEqual(aRight, bRight)) return false;
244
- if (aDefParentParent === bDefParentParent) return aVar != null && bVar != null && aVar === bVar;
245
- return true;
246
- }
247
- default: return aVar != null && bVar != null && aVar === bVar;
248
278
  }
279
+ return aBinding != null && bBinding != null && aBinding.variable === bBinding.variable;
249
280
  }
250
- case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return (a.computed && b.computed ? isValueEqual(context, a.property, b.property) : Compare.isEqual(a.property, b.property)) && isValueEqual(context, a.object, b.object);
281
+ case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression:
282
+ if (a.computed !== b.computed || a.optional !== b.optional) return false;
283
+ return (a.computed ? isEqualWith(context, a.property, b.property, options) : Compare.isEqual(a.property, b.property)) && isEqualWith(context, a.object, b.object, options);
251
284
  case a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression:
252
285
  if (aScope.block === bScope.block) return true;
253
286
  return Traverse.findParent(a, Check.isOneOf(thisBlockTypes)) === Traverse.findParent(b, Check.isOneOf(thisBlockTypes));
254
287
  default: {
255
288
  const aStatic = getStaticValue(a, aScope);
256
289
  const bStatic = getStaticValue(b, bScope);
257
- return aStatic != null && bStatic != null && aStatic.value === bStatic.value;
290
+ if (aStatic == null || bStatic == null) return false;
291
+ return aStatic.value === bStatic.value;
258
292
  }
259
293
  }
260
294
  }
295
+ // src/is-provenance-equal.ts
296
+ function isProvenanceEqual(context, a, b) {
297
+ return isEqualWith(context, a, b, { identifierComparison: "provenance" });
298
+ }
261
299
  // src/is-assignment-target-equal.ts
262
300
  function isAssignmentTargetEqual(context, a, b) {
263
301
  const unwrappedA = Check.isTypeExpression(a) ? Extract.unwrap(a) : a;
264
302
  const unwrappedB = Check.isTypeExpression(b) ? Extract.unwrap(b) : b;
265
- if (Check.isIdentifier(unwrappedA) && Check.isIdentifier(unwrappedB)) return isValueEqual(context, unwrappedA, unwrappedB);
266
- return Compare.isEqual(unwrappedA, unwrappedB) || isValueEqual(context, unwrappedA, unwrappedB);
303
+ if (Check.isIdentifier(unwrappedA) && Check.isIdentifier(unwrappedB)) return isProvenanceEqual(context, unwrappedA, unwrappedB);
304
+ return Compare.isEqual(unwrappedA, unwrappedB) || isProvenanceEqual(context, unwrappedA, unwrappedB);
267
305
  }
268
306
  // src/resolve-import-source.ts
269
307
  function getRequireExpressionArguments(node) {
@@ -276,7 +314,10 @@ function getRequireExpressionArguments(node) {
276
314
  if (expr.type === AST_NODE_TYPES.MemberExpression) return getRequireExpressionArguments(expr.object);
277
315
  return null;
278
316
  }
279
- function resolveImportSource(name, initialScope, seen = /* @__PURE__ */ new Set()) {
317
+ function resolveImportSource(name, initialScope) {
318
+ return resolveImportSourceInner(name, initialScope, /* @__PURE__ */ new Set());
319
+ }
320
+ function resolveImportSourceInner(name, initialScope, seen) {
280
321
  if (seen.has(name)) return null;
281
322
  seen.add(name);
282
323
  const latestDef = findVariable(initialScope, name)?.defs.at(-1);
@@ -284,8 +325,8 @@ function resolveImportSource(name, initialScope, seen = /* @__PURE__ */ new Set(
284
325
  const { node, parent } = latestDef;
285
326
  if (node.type === AST_NODE_TYPES.VariableDeclarator && node.init != null) {
286
327
  const init = Extract.unwrap(node.init);
287
- if (init.type === AST_NODE_TYPES.MemberExpression && init.object.type === AST_NODE_TYPES.Identifier) return resolveImportSource(init.object.name, initialScope, seen);
288
- if (init.type === AST_NODE_TYPES.Identifier) return resolveImportSource(init.name, initialScope, seen);
328
+ if (init.type === AST_NODE_TYPES.MemberExpression && init.object.type === AST_NODE_TYPES.Identifier) return resolveImportSourceInner(init.object.name, initialScope, seen);
329
+ if (init.type === AST_NODE_TYPES.Identifier) return resolveImportSourceInner(init.name, initialScope, seen);
289
330
  const arg0 = getRequireExpressionArguments(init)?.[0];
290
331
  if (arg0 == null || !isMatching({
291
332
  type: AST_NODE_TYPES.Literal,
@@ -308,36 +349,36 @@ function isInitializedFromReactNative(name, initialScope, importSource = "react-
308
349
  "rn"
309
350
  ].includes(name.toLowerCase()) || Boolean(resolveImportSource(name, initialScope)?.startsWith(importSource));
310
351
  }
352
+ // src/is-value-equal.ts
353
+ function isValueEqual(context, a, b) {
354
+ return isEqualWith(context, a, b, { identifierComparison: "identity" });
355
+ }
311
356
  // src/resolve.ts
312
357
  function resolve(context, node, options) {
313
- const { at = 0, localOnly = false } = options ?? {};
314
- const scope = context.sourceCode.getScope(node);
315
- const variable = localOnly ? scope.set.get(node.name) : findVariable(scope, node);
316
- if (variable == null) return null;
317
- const def = variable.defs.at(at);
318
- if (def == null) return null;
319
- switch (def.type) {
320
- case DefinitionType.FunctionName: return def.node;
321
- case DefinitionType.ClassName: return def.node;
322
- case DefinitionType.Variable: {
323
- const { id, init } = def.node;
324
- if (id !== def.name) return null;
325
- if (init == null) return null;
326
- if (hasProperty(init, "declarations")) return null;
327
- return init;
328
- }
329
- case DefinitionType.Parameter: return null;
330
- case DefinitionType.TSEnumName: return def.node;
331
- case DefinitionType.TSEnumMember: return def.node.initializer ?? null;
332
- case DefinitionType.ImportBinding: return null;
333
- case DefinitionType.CatchClause: return null;
334
- case DefinitionType.TSModuleName: return null;
335
- case DefinitionType.Type: return null;
336
- case DefinitionType.ImplicitGlobalVariable: return null;
337
- default: return null;
358
+ const binding = getBinding(context, node, options);
359
+ if (binding?.definition == null) return null;
360
+ return projectBinding(binding.definition, "value");
361
+ }
362
+ // src/resolve-assignment-targets.ts
363
+ function resolveAssignmentTargets(node) {
364
+ const target = Extract.unwrap(node);
365
+ switch (target.type) {
366
+ case AST_NODE_TYPES.Identifier:
367
+ case AST_NODE_TYPES.MemberExpression: return [target];
368
+ case AST_NODE_TYPES.ArrayPattern: return target.elements.flatMap((element) => element == null ? [] : resolveAssignmentTargets(element));
369
+ case AST_NODE_TYPES.AssignmentPattern: return resolveAssignmentTargets(target.left);
370
+ case AST_NODE_TYPES.ObjectPattern: return target.properties.flatMap((property) => {
371
+ if (property.type === AST_NODE_TYPES.RestElement) return resolveAssignmentTargets(property.argument);
372
+ return resolveAssignmentTargets(property.value);
373
+ });
374
+ case AST_NODE_TYPES.RestElement: return resolveAssignmentTargets(target.argument);
375
+ default: return [];
338
376
  }
339
377
  }
340
378
  // src/resolve-enclosing-assignment-target.ts
379
+ function getParent(node) {
380
+ return node.parent;
381
+ }
341
382
  function resolveEnclosingAssignmentTarget(node) {
342
383
  switch (node.type) {
343
384
  case AST_NODE_TYPES.VariableDeclarator: return node.id;
@@ -346,97 +387,142 @@ function resolveEnclosingAssignmentTarget(node) {
346
387
  case AST_NODE_TYPES.ExportDefaultDeclaration: return node.declaration;
347
388
  case AST_NODE_TYPES.BlockStatement:
348
389
  case AST_NODE_TYPES.Program: return null;
349
- default: return resolveEnclosingAssignmentTarget(node.parent);
390
+ default: {
391
+ const parent = getParent(node);
392
+ if (parent == null) return null;
393
+ return resolveEnclosingAssignmentTarget(parent);
394
+ }
350
395
  }
351
396
  }
352
397
  // src/resolve-object-type.ts
353
- function resolveObjectType(context, node) {
398
+ function isUnshadowedGlobal(context, node) {
399
+ const variable = findVariable(context.sourceCode.getScope(node), node);
400
+ return variable == null || variable.defs.length === 0;
401
+ }
402
+ function resolveObjectType(context, node, options) {
403
+ return resolveObjectTypeInner(context, node, /* @__PURE__ */ new Set(), {
404
+ at: options?.at ?? -1,
405
+ localOnly: (options?.scope ?? "local") === "local"
406
+ });
407
+ }
408
+ function resolveObjectTypeInner(context, node, seen, options) {
354
409
  if (node == null) return null;
355
410
  switch (node.type) {
356
411
  case AST_NODE_TYPES.JSXElement:
357
412
  case AST_NODE_TYPES.JSXFragment: return {
358
413
  kind: "jsx",
359
- node
414
+ node,
415
+ via: "jsx"
360
416
  };
361
417
  case AST_NODE_TYPES.ArrayExpression: return {
362
418
  kind: "array",
363
- node
419
+ node,
420
+ via: "literal"
364
421
  };
365
422
  case AST_NODE_TYPES.ObjectExpression: return {
366
423
  kind: "plain",
367
- node
424
+ node,
425
+ via: "literal"
426
+ };
427
+ case AST_NODE_TYPES.ClassDeclaration: return {
428
+ kind: "class",
429
+ node,
430
+ via: "declaration"
368
431
  };
369
- case AST_NODE_TYPES.ClassDeclaration:
370
432
  case AST_NODE_TYPES.ClassExpression: return {
371
433
  kind: "class",
372
- node
434
+ node,
435
+ via: "expression"
436
+ };
437
+ case AST_NODE_TYPES.NewExpression: return {
438
+ kind: "instance",
439
+ node,
440
+ via: "new"
373
441
  };
374
- case AST_NODE_TYPES.NewExpression:
375
442
  case AST_NODE_TYPES.ThisExpression: return {
376
443
  kind: "instance",
377
- node
444
+ node,
445
+ via: "this"
446
+ };
447
+ case AST_NODE_TYPES.FunctionDeclaration: return {
448
+ kind: "function",
449
+ node,
450
+ via: "declaration"
451
+ };
452
+ case AST_NODE_TYPES.FunctionExpression: return {
453
+ kind: "function",
454
+ node,
455
+ via: "expression"
378
456
  };
379
- case AST_NODE_TYPES.FunctionDeclaration:
380
- case AST_NODE_TYPES.FunctionExpression:
381
457
  case AST_NODE_TYPES.ArrowFunctionExpression: return {
382
458
  kind: "function",
383
- node
459
+ node,
460
+ via: "arrow"
384
461
  };
385
462
  case AST_NODE_TYPES.Literal:
386
463
  if (hasProperty(node, "regex")) return {
387
464
  kind: "regexp",
388
- node
465
+ node,
466
+ via: "literal"
389
467
  };
390
468
  return null;
391
469
  case AST_NODE_TYPES.Identifier: {
392
- if ((context.sourceCode.getScope(node).set.get(node.name)?.defs.at(-1))?.type === DefinitionType.Parameter) return null;
393
- const initNode = resolve(context, node, {
394
- at: -1,
395
- localOnly: true
470
+ if (seen.has(node)) return null;
471
+ seen.add(node);
472
+ const binding = getBindingFromScope(context.sourceCode.getScope(node), node, {
473
+ at: options.at,
474
+ localOnly: options.localOnly
396
475
  });
476
+ if (binding?.definition?.type === DefinitionType.Parameter) return null;
477
+ const initNode = binding?.definition == null ? null : projectBinding(binding.definition, "value");
397
478
  if (initNode == null) return null;
398
- return resolveObjectType(context, initNode);
479
+ return resolveObjectTypeInner(context, initNode, seen, options);
399
480
  }
400
- case AST_NODE_TYPES.MemberExpression: return resolveObjectType(context, node.object);
481
+ case AST_NODE_TYPES.MemberExpression: return resolveObjectTypeInner(context, node.object, seen, options);
401
482
  case AST_NODE_TYPES.AssignmentExpression:
402
- case AST_NODE_TYPES.AssignmentPattern: return resolveObjectType(context, node.right);
403
- case AST_NODE_TYPES.LogicalExpression: return resolveObjectType(context, node.left) ?? resolveObjectType(context, node.right);
404
- case AST_NODE_TYPES.ConditionalExpression: return resolveObjectType(context, node.consequent) ?? resolveObjectType(context, node.alternate);
483
+ case AST_NODE_TYPES.AssignmentPattern: return resolveObjectTypeInner(context, node.right, seen, options);
484
+ case AST_NODE_TYPES.LogicalExpression: return resolveObjectTypeInner(context, node.left, new Set(seen), options) ?? resolveObjectTypeInner(context, node.right, new Set(seen), options);
485
+ case AST_NODE_TYPES.ConditionalExpression: return resolveObjectTypeInner(context, node.consequent, new Set(seen), options) ?? resolveObjectTypeInner(context, node.alternate, new Set(seen), options);
405
486
  case AST_NODE_TYPES.SequenceExpression:
406
487
  if (node.expressions.length === 0) return null;
407
- return resolveObjectType(context, node.expressions.at(-1) ?? null);
488
+ return resolveObjectTypeInner(context, node.expressions.at(-1) ?? null, seen, options);
408
489
  case AST_NODE_TYPES.CallExpression: {
409
490
  const callee = Extract.unwrap(node.callee);
410
491
  switch (true) {
411
- case Check.isIdentifier(callee, "Boolean"): return null;
412
- case Check.isIdentifier(callee, "String"): return null;
492
+ case Check.isIdentifier(callee, "Boolean"):
493
+ case Check.isIdentifier(callee, "String"):
413
494
  case Check.isIdentifier(callee, "Number"): return null;
414
- case Check.isIdentifier(callee, "Object"): return {
495
+ case Check.isIdentifier(callee, "Object") && isUnshadowedGlobal(context, callee): return {
415
496
  kind: "plain",
416
- node
497
+ node,
498
+ via: "call"
417
499
  };
418
- case Check.isIdentifier(callee, "Array"): return {
500
+ case Check.isIdentifier(callee, "Array") && isUnshadowedGlobal(context, callee): return {
419
501
  kind: "array",
420
- node
502
+ node,
503
+ via: "call"
421
504
  };
422
- case Check.isIdentifier(callee, "RegExp"): return {
505
+ case Check.isIdentifier(callee, "RegExp") && isUnshadowedGlobal(context, callee): return {
423
506
  kind: "regexp",
424
- node
507
+ node,
508
+ via: "call"
425
509
  };
426
510
  }
427
- if (callee.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(callee.object)) {
511
+ if (callee.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(callee.object) && isUnshadowedGlobal(context, callee.object)) {
428
512
  const objName = callee.object.name;
429
513
  const methodName = Extract.getCalleeName(node);
430
514
  switch (objName) {
431
515
  case "Array":
432
516
  if (methodName === "from" || methodName === "of") return {
433
517
  kind: "array",
434
- node
518
+ node,
519
+ via: "call"
435
520
  };
436
521
  break;
437
522
  case "Object": if (methodName === "create" || methodName === "assign" || methodName === "fromEntries") return {
438
523
  kind: "plain",
439
- node
524
+ node,
525
+ via: "call"
440
526
  };
441
527
  }
442
528
  }
@@ -448,7 +534,32 @@ function resolveObjectType(context, node) {
448
534
  }
449
535
  default:
450
536
  if (!hasProperty(node, "expression") || typeof node.expression !== "object") return null;
451
- return resolveObjectType(context, node.expression);
537
+ return resolveObjectTypeInner(context, node.expression, seen, options);
538
+ }
539
+ }
540
+ // src/resolve-origin.ts
541
+ function resolveOrigin(context, node, options) {
542
+ const binding = getBinding(context, node, options);
543
+ if (binding?.definition == null) return null;
544
+ return projectBinding(binding.definition, "origin");
545
+ }
546
+ // src/resolve-origin-chain.ts
547
+ const DEFAULT_MAX_DEPTH = 32;
548
+ function resolveOriginChain(context, node, options) {
549
+ const { at = 0, localOnly = false, maxDepth = DEFAULT_MAX_DEPTH } = options ?? {};
550
+ const seen = /* @__PURE__ */ new Set();
551
+ let current = node;
552
+ for (let depth = 0; depth < maxDepth; depth++) {
553
+ if (!Check.isIdentifier(current)) return current;
554
+ if (seen.has(current)) return null;
555
+ seen.add(current);
556
+ const origin = resolveOrigin(context, current, {
557
+ at,
558
+ localOnly
559
+ });
560
+ if (origin == null) return null;
561
+ current = Extract.unwrap(origin);
452
562
  }
563
+ return Check.isIdentifier(current) ? null : current;
453
564
  }
454
- export { createImportLookup, isAssignmentTargetEqual, isInitializedFromReact, isInitializedFromReactNative, isValueEqual, resolve, resolveEnclosingAssignmentTarget, resolveImportSource, resolveObjectType, resolveOrigin };
565
+ export { createImportLookup, isAssignmentTargetEqual, isInitializedFromReact, isInitializedFromReactNative, isProvenanceEqual, isValueEqual, resolve, resolveAssignmentTargets, resolveEnclosingAssignmentTarget, resolveImportSource, resolveObjectType, resolveOrigin, resolveOriginChain };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/var",
3
- "version": "5.30.0-beta.2",
3
+ "version": "5.30.0-beta.20",
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": {
@@ -29,11 +29,11 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@eslint-react/ast": "5.30.0-beta.2",
33
- "@eslint-react/eslint": "5.30.0-beta.2",
34
- "@typescript-eslint/scope-manager": "^8.72.0",
35
- "@typescript-eslint/types": "^8.72.0",
36
- "@typescript-eslint/utils": "^8.72.0",
32
+ "@eslint-react/ast": "5.30.0-beta.20",
33
+ "@eslint-react/eslint": "5.30.0-beta.20",
34
+ "@typescript-eslint/scope-manager": "^8.72.1",
35
+ "@typescript-eslint/types": "^8.72.1",
36
+ "@typescript-eslint/utils": "^8.72.1",
37
37
  "ts-pattern": "^5.9.2"
38
38
  },
39
39
  "devDependencies": {
@@ -42,8 +42,8 @@
42
42
  "@local/testkit": "0.0.0",
43
43
  "@tsconfig/node20": "^20.1.10",
44
44
  "@types/node": "^20.19.43",
45
- "eslint": "^10.13.0",
46
- "tsdown": "^0.23.2",
45
+ "eslint": "^10.14.0",
46
+ "tsdown": "^0.23.4",
47
47
  "typescript": "6.0.3",
48
48
  "vitest": "^5.0.5"
49
49
  },