@eslint-react/var 5.30.0-beta.1 → 5.30.0-beta.10
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 +179 -68
- package/dist/index.js +219 -128
- package/package.json +6 -7
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
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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 `
|
|
41
|
+
* @returns The matching entry, or `null` if the name is not imported.
|
|
45
42
|
*/
|
|
46
|
-
binding(local: string): ImportEntry |
|
|
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
|
|
63
|
-
*
|
|
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
|
-
|
|
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,91 @@ 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?:
|
|
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;
|
|
183
251
|
/** The possible assignment targets returned by {@link resolveEnclosingAssignmentTarget}. */
|
|
184
|
-
export type EnclosingAssignmentTarget =
|
|
252
|
+
export type EnclosingAssignmentTarget = TSESTree.DefaultExportDeclarations | TSESTree.PropertyName | null;
|
|
185
253
|
/**
|
|
186
254
|
* Resolve the enclosing assignment target (variable, property, etc.) of the node.
|
|
187
255
|
* @param node The starting node for the upward search.
|
|
188
256
|
* @returns The enclosing assignment target node, or `null` when not found.
|
|
189
257
|
*/
|
|
190
|
-
export declare function resolveEnclosingAssignmentTarget(node: TSESTree.Node):
|
|
258
|
+
export declare function resolveEnclosingAssignmentTarget(node: TSESTree.Node): EnclosingAssignmentTarget;
|
|
191
259
|
/**
|
|
192
260
|
* Resolve the import source of a variable by walking its latest definition.
|
|
193
261
|
* @param name The variable name.
|
|
194
262
|
* @param initialScope The initial scope.
|
|
195
|
-
* @param seen The set of already visited variable names (for cycle detection).
|
|
196
263
|
* @returns The import source, or `null` when it cannot be resolved.
|
|
197
264
|
*/
|
|
198
|
-
export declare function resolveImportSource(name: string, initialScope: Scope
|
|
265
|
+
export declare function resolveImportSource(name: string, initialScope: Scope): string | null;
|
|
199
266
|
/**
|
|
200
267
|
* Represents the type classification of an object node.
|
|
268
|
+
*
|
|
269
|
+
* `via` records how a known kind was constructed (a literal, a call, a
|
|
270
|
+
* declaration, ...), so consumers can distinguish e.g. `[]` from `Array()`
|
|
271
|
+
* without inspecting `node.type`. `unknown` carries a typed `reason` instead.
|
|
201
272
|
*/
|
|
202
273
|
export type ObjectType = {
|
|
203
|
-
kind: "jsx";
|
|
204
|
-
node: TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
274
|
+
readonly kind: "jsx";
|
|
275
|
+
readonly node: TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
276
|
+
readonly via: "jsx";
|
|
205
277
|
} | {
|
|
206
|
-
kind: "array";
|
|
207
|
-
node: TSESTree.ArrayExpression | TSESTree.CallExpression;
|
|
278
|
+
readonly kind: "array";
|
|
279
|
+
readonly node: TSESTree.ArrayExpression | TSESTree.CallExpression;
|
|
280
|
+
readonly via: "literal" | "call";
|
|
208
281
|
} | {
|
|
209
|
-
kind: "plain";
|
|
210
|
-
node: TSESTree.ObjectExpression | TSESTree.CallExpression;
|
|
282
|
+
readonly kind: "plain";
|
|
283
|
+
readonly node: TSESTree.ObjectExpression | TSESTree.CallExpression;
|
|
284
|
+
readonly via: "literal" | "call";
|
|
211
285
|
} | {
|
|
212
|
-
kind: "class";
|
|
213
|
-
node: TSESTree.ClassDeclaration | TSESTree.ClassExpression;
|
|
286
|
+
readonly kind: "class";
|
|
287
|
+
readonly node: TSESTree.ClassDeclaration | TSESTree.ClassExpression;
|
|
288
|
+
readonly via: "declaration" | "expression";
|
|
214
289
|
} | {
|
|
215
|
-
kind: "instance";
|
|
216
|
-
node: TSESTree.NewExpression | TSESTree.ThisExpression;
|
|
290
|
+
readonly kind: "instance";
|
|
291
|
+
readonly node: TSESTree.NewExpression | TSESTree.ThisExpression;
|
|
292
|
+
readonly via: "new" | "this";
|
|
217
293
|
} | {
|
|
218
|
-
kind: "function";
|
|
219
|
-
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression;
|
|
294
|
+
readonly kind: "function";
|
|
295
|
+
readonly node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression;
|
|
296
|
+
readonly via: "declaration" | "expression" | "arrow";
|
|
220
297
|
} | {
|
|
221
|
-
kind: "regexp";
|
|
222
|
-
node: TSESTree.RegExpLiteral | TSESTree.CallExpression;
|
|
298
|
+
readonly kind: "regexp";
|
|
299
|
+
readonly node: TSESTree.RegExpLiteral | TSESTree.CallExpression;
|
|
300
|
+
readonly via: "literal" | "call";
|
|
223
301
|
} | {
|
|
224
|
-
kind: "unknown";
|
|
225
|
-
node: TSESTree.Node;
|
|
226
|
-
|
|
302
|
+
readonly kind: "unknown";
|
|
303
|
+
readonly node: TSESTree.Node;
|
|
304
|
+
/** Why the node could not be classified. */
|
|
305
|
+
readonly reason: "call-expression";
|
|
227
306
|
};
|
|
307
|
+
/** Options for {@link resolveObjectType}. */
|
|
308
|
+
export interface ResolveObjectTypeOptions {
|
|
309
|
+
/** Index of the definition to resolve (default: `-1`, the latest definition). */
|
|
310
|
+
at?: number;
|
|
311
|
+
/**
|
|
312
|
+
* Scope traversal mode.
|
|
313
|
+
*
|
|
314
|
+
* - `"local"`: only follow bindings declared in the same scope as the identifier (default).
|
|
315
|
+
* - `"chain"`: also follow bindings declared in enclosing scopes.
|
|
316
|
+
*
|
|
317
|
+
* This maps internally to `localOnly` in {@link ResolveOptions}.
|
|
318
|
+
*/
|
|
319
|
+
scope?: "local" | "chain";
|
|
320
|
+
}
|
|
228
321
|
/**
|
|
229
322
|
* Resolve the object type of the node.
|
|
230
323
|
* @param context The ESLint rule context.
|
|
231
324
|
* @param node The node to resolve.
|
|
325
|
+
* @param options Optional resolution settings; see {@link ResolveObjectTypeOptions}.
|
|
232
326
|
* @returns The object type of the node, or `null` when it cannot be resolved.
|
|
233
327
|
*/
|
|
234
|
-
export declare function resolveObjectType(context: RuleContext, node: TSESTree.Node | null | undefined): ObjectType | null;
|
|
328
|
+
export declare function resolveObjectType(context: RuleContext, node: TSESTree.Node | null | undefined, options?: ResolveObjectTypeOptions): ObjectType | null;
|
|
235
329
|
/**
|
|
236
330
|
* Resolve an identifier to the AST node its value **originates from**,
|
|
237
331
|
* suitable for origin/pedigree tracking in ESLint rule analysis.
|
|
@@ -261,19 +355,36 @@ export declare function resolveObjectType(context: RuleContext, node: TSESTree.N
|
|
|
261
355
|
* `ImportDeclaration` carries the module source) instead of `null`.
|
|
262
356
|
*
|
|
263
357
|
* Use this for origin/pedigree tracking ("what produced this value?"); use
|
|
264
|
-
* {@link resolve} when the precise value of the binding is needed.
|
|
358
|
+
* {@link resolve} when the precise value of the binding is needed. To follow a
|
|
359
|
+
* chain of aliases to a fixed point, use {@link resolveOriginChain}.
|
|
265
360
|
*
|
|
266
361
|
* @param context The ESLint rule context.
|
|
267
362
|
* @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.
|
|
363
|
+
* @param options Optional resolution settings; see {@link ResolveOptions}.
|
|
274
364
|
* @returns The resolved origin node, or `null` if the identifier cannot be resolved.
|
|
275
365
|
*/
|
|
276
|
-
export declare function resolveOrigin(context: RuleContext, node: TSESTree.Identifier, options?:
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
366
|
+
export declare function resolveOrigin(context: RuleContext, node: TSESTree.Identifier, options?: ResolveOptions): TSESTree.Node | null;
|
|
367
|
+
/** Options for {@link resolveOriginChain}. */
|
|
368
|
+
export interface ResolveOriginChainOptions extends ResolveOptions {
|
|
369
|
+
/** Maximum number of resolution steps before giving up (default: `32`). */
|
|
370
|
+
maxDepth?: number;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Follow an identifier through an alias chain to the node its value ultimately
|
|
374
|
+
* originates from.
|
|
375
|
+
*
|
|
376
|
+
* Each step uses {@link resolveOrigin} and unwraps TypeScript type and chain
|
|
377
|
+
* expressions, so `const a = b as T` continues through `b`. Resolution stops
|
|
378
|
+
* when a step yields a non-identifier node, a `null` origin, a repeated node
|
|
379
|
+
* (cycle), or after `maxDepth` steps.
|
|
380
|
+
*
|
|
381
|
+
* Use this instead of calling {@link resolveOrigin} in a loop: it provides
|
|
382
|
+
* cycle protection and a bounded traversal.
|
|
383
|
+
*
|
|
384
|
+
* @param context The ESLint rule context.
|
|
385
|
+
* @param node The identifier to start from.
|
|
386
|
+
* @param options Optional chain-resolution settings; see {@link ResolveOriginChainOptions}.
|
|
387
|
+
* @returns The first non-identifier origin node, or `null` when the chain cannot be resolved.
|
|
388
|
+
*/
|
|
389
|
+
export declare function resolveOriginChain(context: RuleContext, node: TSESTree.Identifier, options?: ResolveOriginChainOptions): TSESTree.Node | null;
|
|
390
|
+
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
|
-
|
|
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
|
-
|
|
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: "
|
|
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,138 @@ 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
|
-
|
|
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
|
-
|
|
182
|
+
isModuleBinding(local) {
|
|
169
183
|
const entry = byLocal.get(local);
|
|
170
|
-
return entry != null &&
|
|
184
|
+
return entry != null && entry.kind !== "named";
|
|
171
185
|
}
|
|
172
186
|
};
|
|
173
187
|
}
|
|
174
|
-
// src/resolve-
|
|
175
|
-
function
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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 } =
|
|
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(
|
|
192
|
-
case DefinitionType.TSEnumName: return
|
|
193
|
-
case DefinitionType.TSEnumMember: return
|
|
194
|
-
case DefinitionType.ImportBinding: return
|
|
195
|
-
case DefinitionType.CatchClause:
|
|
196
|
-
case DefinitionType.
|
|
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/
|
|
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
|
|
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;
|
|
212
237
|
const [aScope, bScope] = [context.sourceCode.getScope(a), context.sourceCode.getScope(b)];
|
|
213
238
|
switch (true) {
|
|
214
239
|
case a === b: return true;
|
|
215
|
-
case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal:
|
|
240
|
+
case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal:
|
|
241
|
+
if (isRegExpLiteral(a)) return isRegExpLiteral(b) && a.regex.pattern === b.regex.pattern && a.regex.flags === b.regex.flags;
|
|
242
|
+
return !isRegExpLiteral(b) && a.value === b.value;
|
|
216
243
|
case a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement: return a.value.cooked === b.value.cooked;
|
|
217
244
|
case Check.isIdentifier(a) && Check.isIdentifier(b): {
|
|
218
|
-
const
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
245
|
+
const aBinding = getBindingFromScope(aScope, a);
|
|
246
|
+
const bBinding = getBindingFromScope(bScope, b);
|
|
247
|
+
if (identifierComparison === "provenance") {
|
|
248
|
+
const aDefNode = aBinding?.definition == null ? null : projectBinding(aBinding.definition, "origin");
|
|
249
|
+
const bDefNode = bBinding?.definition == null ? null : projectBinding(bBinding.definition, "origin");
|
|
250
|
+
const aDefNodeParent = aDefNode?.parent;
|
|
251
|
+
const bDefNodeParent = bDefNode?.parent;
|
|
252
|
+
const aDef = aBinding?.definition;
|
|
253
|
+
const bDef = bBinding?.definition;
|
|
254
|
+
const aDefParentParent = aDef?.parent?.parent;
|
|
255
|
+
const bDefParentParent = bDef?.parent?.parent;
|
|
256
|
+
switch (true) {
|
|
257
|
+
case aDefNodeParent?.type === AST_NODE_TYPES.CallExpression && bDefNodeParent?.type === AST_NODE_TYPES.CallExpression && Check.isFunction(aDefNode) && Check.isFunction(bDefNode): {
|
|
258
|
+
if (!Compare.isEqual(aDefNodeParent.callee, bDefNodeParent.callee)) return false;
|
|
259
|
+
const aParams = aDefNode.params;
|
|
260
|
+
const bParams = bDefNode.params;
|
|
261
|
+
const aPos = aParams.findIndex((x) => Compare.isEqual(x, a));
|
|
262
|
+
const bPos = bParams.findIndex((x) => Compare.isEqual(x, b));
|
|
263
|
+
return aPos !== -1 && bPos !== -1 && aPos === bPos;
|
|
264
|
+
}
|
|
265
|
+
case aDefParentParent?.type === AST_NODE_TYPES.ForOfStatement && bDefParentParent?.type === AST_NODE_TYPES.ForOfStatement: {
|
|
266
|
+
const aLeft = aDefParentParent.left;
|
|
267
|
+
const bLeft = bDefParentParent.left;
|
|
268
|
+
if (aLeft.type !== bLeft.type) return false;
|
|
269
|
+
const aRight = aDefParentParent.right;
|
|
270
|
+
const bRight = bDefParentParent.right;
|
|
271
|
+
if (!Compare.isEqual(aRight, bRight)) return false;
|
|
272
|
+
if (aDefParentParent === bDefParentParent) return aBinding != null && bBinding != null && aBinding.variable === bBinding.variable;
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
236
275
|
}
|
|
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
276
|
}
|
|
277
|
+
return aBinding != null && bBinding != null && aBinding.variable === bBinding.variable;
|
|
249
278
|
}
|
|
250
|
-
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return (a.computed && b.computed ?
|
|
279
|
+
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return (a.computed && b.computed ? isEqualWith(context, a.property, b.property, options) : Compare.isEqual(a.property, b.property)) && isEqualWith(context, a.object, b.object, options);
|
|
251
280
|
case a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression:
|
|
252
281
|
if (aScope.block === bScope.block) return true;
|
|
253
282
|
return Traverse.findParent(a, Check.isOneOf(thisBlockTypes)) === Traverse.findParent(b, Check.isOneOf(thisBlockTypes));
|
|
254
283
|
default: {
|
|
255
284
|
const aStatic = getStaticValue(a, aScope);
|
|
256
285
|
const bStatic = getStaticValue(b, bScope);
|
|
257
|
-
|
|
286
|
+
if (aStatic == null || bStatic == null) return false;
|
|
287
|
+
return aStatic.value === bStatic.value;
|
|
258
288
|
}
|
|
259
289
|
}
|
|
260
290
|
}
|
|
291
|
+
// src/is-provenance-equal.ts
|
|
292
|
+
function isProvenanceEqual(context, a, b) {
|
|
293
|
+
return isEqualWith(context, a, b, { identifierComparison: "provenance" });
|
|
294
|
+
}
|
|
261
295
|
// src/is-assignment-target-equal.ts
|
|
262
296
|
function isAssignmentTargetEqual(context, a, b) {
|
|
263
297
|
const unwrappedA = Check.isTypeExpression(a) ? Extract.unwrap(a) : a;
|
|
264
298
|
const unwrappedB = Check.isTypeExpression(b) ? Extract.unwrap(b) : b;
|
|
265
|
-
if (Check.isIdentifier(unwrappedA) && Check.isIdentifier(unwrappedB)) return
|
|
266
|
-
return Compare.isEqual(unwrappedA, unwrappedB) ||
|
|
299
|
+
if (Check.isIdentifier(unwrappedA) && Check.isIdentifier(unwrappedB)) return isProvenanceEqual(context, unwrappedA, unwrappedB);
|
|
300
|
+
return Compare.isEqual(unwrappedA, unwrappedB) || isProvenanceEqual(context, unwrappedA, unwrappedB);
|
|
267
301
|
}
|
|
268
302
|
// src/resolve-import-source.ts
|
|
269
303
|
function getRequireExpressionArguments(node) {
|
|
@@ -276,7 +310,10 @@ function getRequireExpressionArguments(node) {
|
|
|
276
310
|
if (expr.type === AST_NODE_TYPES.MemberExpression) return getRequireExpressionArguments(expr.object);
|
|
277
311
|
return null;
|
|
278
312
|
}
|
|
279
|
-
function resolveImportSource(name, initialScope
|
|
313
|
+
function resolveImportSource(name, initialScope) {
|
|
314
|
+
return resolveImportSourceInner(name, initialScope, /* @__PURE__ */ new Set());
|
|
315
|
+
}
|
|
316
|
+
function resolveImportSourceInner(name, initialScope, seen) {
|
|
280
317
|
if (seen.has(name)) return null;
|
|
281
318
|
seen.add(name);
|
|
282
319
|
const latestDef = findVariable(initialScope, name)?.defs.at(-1);
|
|
@@ -284,8 +321,8 @@ function resolveImportSource(name, initialScope, seen = /* @__PURE__ */ new Set(
|
|
|
284
321
|
const { node, parent } = latestDef;
|
|
285
322
|
if (node.type === AST_NODE_TYPES.VariableDeclarator && node.init != null) {
|
|
286
323
|
const init = Extract.unwrap(node.init);
|
|
287
|
-
if (init.type === AST_NODE_TYPES.MemberExpression && init.object.type === AST_NODE_TYPES.Identifier) return
|
|
288
|
-
if (init.type === AST_NODE_TYPES.Identifier) return
|
|
324
|
+
if (init.type === AST_NODE_TYPES.MemberExpression && init.object.type === AST_NODE_TYPES.Identifier) return resolveImportSourceInner(init.object.name, initialScope, seen);
|
|
325
|
+
if (init.type === AST_NODE_TYPES.Identifier) return resolveImportSourceInner(init.name, initialScope, seen);
|
|
289
326
|
const arg0 = getRequireExpressionArguments(init)?.[0];
|
|
290
327
|
if (arg0 == null || !isMatching({
|
|
291
328
|
type: AST_NODE_TYPES.Literal,
|
|
@@ -308,36 +345,20 @@ function isInitializedFromReactNative(name, initialScope, importSource = "react-
|
|
|
308
345
|
"rn"
|
|
309
346
|
].includes(name.toLowerCase()) || Boolean(resolveImportSource(name, initialScope)?.startsWith(importSource));
|
|
310
347
|
}
|
|
348
|
+
// src/is-value-equal.ts
|
|
349
|
+
function isValueEqual(context, a, b) {
|
|
350
|
+
return isEqualWith(context, a, b, { identifierComparison: "identity" });
|
|
351
|
+
}
|
|
311
352
|
// src/resolve.ts
|
|
312
353
|
function resolve(context, node, options) {
|
|
313
|
-
const
|
|
314
|
-
|
|
315
|
-
|
|
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;
|
|
338
|
-
}
|
|
354
|
+
const binding = getBinding(context, node, options);
|
|
355
|
+
if (binding?.definition == null) return null;
|
|
356
|
+
return projectBinding(binding.definition, "value");
|
|
339
357
|
}
|
|
340
358
|
// src/resolve-enclosing-assignment-target.ts
|
|
359
|
+
function getParent(node) {
|
|
360
|
+
return node.parent;
|
|
361
|
+
}
|
|
341
362
|
function resolveEnclosingAssignmentTarget(node) {
|
|
342
363
|
switch (node.type) {
|
|
343
364
|
case AST_NODE_TYPES.VariableDeclarator: return node.id;
|
|
@@ -346,97 +367,142 @@ function resolveEnclosingAssignmentTarget(node) {
|
|
|
346
367
|
case AST_NODE_TYPES.ExportDefaultDeclaration: return node.declaration;
|
|
347
368
|
case AST_NODE_TYPES.BlockStatement:
|
|
348
369
|
case AST_NODE_TYPES.Program: return null;
|
|
349
|
-
default:
|
|
370
|
+
default: {
|
|
371
|
+
const parent = getParent(node);
|
|
372
|
+
if (parent == null) return null;
|
|
373
|
+
return resolveEnclosingAssignmentTarget(parent);
|
|
374
|
+
}
|
|
350
375
|
}
|
|
351
376
|
}
|
|
352
377
|
// src/resolve-object-type.ts
|
|
353
|
-
function
|
|
378
|
+
function isUnshadowedGlobal(context, node) {
|
|
379
|
+
const variable = findVariable(context.sourceCode.getScope(node), node);
|
|
380
|
+
return variable == null || variable.defs.length === 0;
|
|
381
|
+
}
|
|
382
|
+
function resolveObjectType(context, node, options) {
|
|
383
|
+
return resolveObjectTypeInner(context, node, /* @__PURE__ */ new Set(), {
|
|
384
|
+
at: options?.at ?? -1,
|
|
385
|
+
localOnly: (options?.scope ?? "local") === "local"
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
function resolveObjectTypeInner(context, node, seen, options) {
|
|
354
389
|
if (node == null) return null;
|
|
355
390
|
switch (node.type) {
|
|
356
391
|
case AST_NODE_TYPES.JSXElement:
|
|
357
392
|
case AST_NODE_TYPES.JSXFragment: return {
|
|
358
393
|
kind: "jsx",
|
|
359
|
-
node
|
|
394
|
+
node,
|
|
395
|
+
via: "jsx"
|
|
360
396
|
};
|
|
361
397
|
case AST_NODE_TYPES.ArrayExpression: return {
|
|
362
398
|
kind: "array",
|
|
363
|
-
node
|
|
399
|
+
node,
|
|
400
|
+
via: "literal"
|
|
364
401
|
};
|
|
365
402
|
case AST_NODE_TYPES.ObjectExpression: return {
|
|
366
403
|
kind: "plain",
|
|
367
|
-
node
|
|
404
|
+
node,
|
|
405
|
+
via: "literal"
|
|
406
|
+
};
|
|
407
|
+
case AST_NODE_TYPES.ClassDeclaration: return {
|
|
408
|
+
kind: "class",
|
|
409
|
+
node,
|
|
410
|
+
via: "declaration"
|
|
368
411
|
};
|
|
369
|
-
case AST_NODE_TYPES.ClassDeclaration:
|
|
370
412
|
case AST_NODE_TYPES.ClassExpression: return {
|
|
371
413
|
kind: "class",
|
|
372
|
-
node
|
|
414
|
+
node,
|
|
415
|
+
via: "expression"
|
|
416
|
+
};
|
|
417
|
+
case AST_NODE_TYPES.NewExpression: return {
|
|
418
|
+
kind: "instance",
|
|
419
|
+
node,
|
|
420
|
+
via: "new"
|
|
373
421
|
};
|
|
374
|
-
case AST_NODE_TYPES.NewExpression:
|
|
375
422
|
case AST_NODE_TYPES.ThisExpression: return {
|
|
376
423
|
kind: "instance",
|
|
377
|
-
node
|
|
424
|
+
node,
|
|
425
|
+
via: "this"
|
|
426
|
+
};
|
|
427
|
+
case AST_NODE_TYPES.FunctionDeclaration: return {
|
|
428
|
+
kind: "function",
|
|
429
|
+
node,
|
|
430
|
+
via: "declaration"
|
|
431
|
+
};
|
|
432
|
+
case AST_NODE_TYPES.FunctionExpression: return {
|
|
433
|
+
kind: "function",
|
|
434
|
+
node,
|
|
435
|
+
via: "expression"
|
|
378
436
|
};
|
|
379
|
-
case AST_NODE_TYPES.FunctionDeclaration:
|
|
380
|
-
case AST_NODE_TYPES.FunctionExpression:
|
|
381
437
|
case AST_NODE_TYPES.ArrowFunctionExpression: return {
|
|
382
438
|
kind: "function",
|
|
383
|
-
node
|
|
439
|
+
node,
|
|
440
|
+
via: "arrow"
|
|
384
441
|
};
|
|
385
442
|
case AST_NODE_TYPES.Literal:
|
|
386
443
|
if (hasProperty(node, "regex")) return {
|
|
387
444
|
kind: "regexp",
|
|
388
|
-
node
|
|
445
|
+
node,
|
|
446
|
+
via: "literal"
|
|
389
447
|
};
|
|
390
448
|
return null;
|
|
391
449
|
case AST_NODE_TYPES.Identifier: {
|
|
392
|
-
if (
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
450
|
+
if (seen.has(node)) return null;
|
|
451
|
+
seen.add(node);
|
|
452
|
+
const binding = getBindingFromScope(context.sourceCode.getScope(node), node, {
|
|
453
|
+
at: options.at,
|
|
454
|
+
localOnly: options.localOnly
|
|
396
455
|
});
|
|
456
|
+
if (binding?.definition?.type === DefinitionType.Parameter) return null;
|
|
457
|
+
const initNode = binding?.definition == null ? null : projectBinding(binding.definition, "value");
|
|
397
458
|
if (initNode == null) return null;
|
|
398
|
-
return
|
|
459
|
+
return resolveObjectTypeInner(context, initNode, seen, options);
|
|
399
460
|
}
|
|
400
|
-
case AST_NODE_TYPES.MemberExpression: return
|
|
461
|
+
case AST_NODE_TYPES.MemberExpression: return resolveObjectTypeInner(context, node.object, seen, options);
|
|
401
462
|
case AST_NODE_TYPES.AssignmentExpression:
|
|
402
|
-
case AST_NODE_TYPES.AssignmentPattern: return
|
|
403
|
-
case AST_NODE_TYPES.LogicalExpression: return
|
|
404
|
-
case AST_NODE_TYPES.ConditionalExpression: return
|
|
463
|
+
case AST_NODE_TYPES.AssignmentPattern: return resolveObjectTypeInner(context, node.right, seen, options);
|
|
464
|
+
case AST_NODE_TYPES.LogicalExpression: return resolveObjectTypeInner(context, node.left, new Set(seen), options) ?? resolveObjectTypeInner(context, node.right, new Set(seen), options);
|
|
465
|
+
case AST_NODE_TYPES.ConditionalExpression: return resolveObjectTypeInner(context, node.consequent, new Set(seen), options) ?? resolveObjectTypeInner(context, node.alternate, new Set(seen), options);
|
|
405
466
|
case AST_NODE_TYPES.SequenceExpression:
|
|
406
467
|
if (node.expressions.length === 0) return null;
|
|
407
|
-
return
|
|
468
|
+
return resolveObjectTypeInner(context, node.expressions.at(-1) ?? null, seen, options);
|
|
408
469
|
case AST_NODE_TYPES.CallExpression: {
|
|
409
470
|
const callee = Extract.unwrap(node.callee);
|
|
410
471
|
switch (true) {
|
|
411
|
-
case Check.isIdentifier(callee, "Boolean"):
|
|
412
|
-
case Check.isIdentifier(callee, "String"):
|
|
472
|
+
case Check.isIdentifier(callee, "Boolean"):
|
|
473
|
+
case Check.isIdentifier(callee, "String"):
|
|
413
474
|
case Check.isIdentifier(callee, "Number"): return null;
|
|
414
|
-
case Check.isIdentifier(callee, "Object"): return {
|
|
475
|
+
case Check.isIdentifier(callee, "Object") && isUnshadowedGlobal(context, callee): return {
|
|
415
476
|
kind: "plain",
|
|
416
|
-
node
|
|
477
|
+
node,
|
|
478
|
+
via: "call"
|
|
417
479
|
};
|
|
418
|
-
case Check.isIdentifier(callee, "Array"): return {
|
|
480
|
+
case Check.isIdentifier(callee, "Array") && isUnshadowedGlobal(context, callee): return {
|
|
419
481
|
kind: "array",
|
|
420
|
-
node
|
|
482
|
+
node,
|
|
483
|
+
via: "call"
|
|
421
484
|
};
|
|
422
|
-
case Check.isIdentifier(callee, "RegExp"): return {
|
|
485
|
+
case Check.isIdentifier(callee, "RegExp") && isUnshadowedGlobal(context, callee): return {
|
|
423
486
|
kind: "regexp",
|
|
424
|
-
node
|
|
487
|
+
node,
|
|
488
|
+
via: "call"
|
|
425
489
|
};
|
|
426
490
|
}
|
|
427
|
-
if (callee.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(callee.object)) {
|
|
491
|
+
if (callee.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(callee.object) && isUnshadowedGlobal(context, callee.object)) {
|
|
428
492
|
const objName = callee.object.name;
|
|
429
493
|
const methodName = Extract.getCalleeName(node);
|
|
430
494
|
switch (objName) {
|
|
431
495
|
case "Array":
|
|
432
496
|
if (methodName === "from" || methodName === "of") return {
|
|
433
497
|
kind: "array",
|
|
434
|
-
node
|
|
498
|
+
node,
|
|
499
|
+
via: "call"
|
|
435
500
|
};
|
|
436
501
|
break;
|
|
437
502
|
case "Object": if (methodName === "create" || methodName === "assign" || methodName === "fromEntries") return {
|
|
438
503
|
kind: "plain",
|
|
439
|
-
node
|
|
504
|
+
node,
|
|
505
|
+
via: "call"
|
|
440
506
|
};
|
|
441
507
|
}
|
|
442
508
|
}
|
|
@@ -448,7 +514,32 @@ function resolveObjectType(context, node) {
|
|
|
448
514
|
}
|
|
449
515
|
default:
|
|
450
516
|
if (!hasProperty(node, "expression") || typeof node.expression !== "object") return null;
|
|
451
|
-
return
|
|
517
|
+
return resolveObjectTypeInner(context, node.expression, seen, options);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
// src/resolve-origin.ts
|
|
521
|
+
function resolveOrigin(context, node, options) {
|
|
522
|
+
const binding = getBinding(context, node, options);
|
|
523
|
+
if (binding?.definition == null) return null;
|
|
524
|
+
return projectBinding(binding.definition, "origin");
|
|
525
|
+
}
|
|
526
|
+
// src/resolve-origin-chain.ts
|
|
527
|
+
const DEFAULT_MAX_DEPTH = 32;
|
|
528
|
+
function resolveOriginChain(context, node, options) {
|
|
529
|
+
const { at = 0, localOnly = false, maxDepth = DEFAULT_MAX_DEPTH } = options ?? {};
|
|
530
|
+
const seen = /* @__PURE__ */ new Set();
|
|
531
|
+
let current = node;
|
|
532
|
+
for (let depth = 0; depth < maxDepth; depth++) {
|
|
533
|
+
if (!Check.isIdentifier(current)) return current;
|
|
534
|
+
if (seen.has(current)) return null;
|
|
535
|
+
seen.add(current);
|
|
536
|
+
const origin = resolveOrigin(context, current, {
|
|
537
|
+
at,
|
|
538
|
+
localOnly
|
|
539
|
+
});
|
|
540
|
+
if (origin == null) return null;
|
|
541
|
+
current = Extract.unwrap(origin);
|
|
452
542
|
}
|
|
543
|
+
return Check.isIdentifier(current) ? null : current;
|
|
453
544
|
}
|
|
454
|
-
export { createImportLookup, isAssignmentTargetEqual, isInitializedFromReact, isInitializedFromReactNative, isValueEqual, resolve, resolveEnclosingAssignmentTarget, resolveImportSource, resolveObjectType, resolveOrigin };
|
|
545
|
+
export { createImportLookup, isAssignmentTargetEqual, isInitializedFromReact, isInitializedFromReactNative, isProvenanceEqual, isValueEqual, resolve, 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.
|
|
3
|
+
"version": "5.30.0-beta.10",
|
|
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.
|
|
33
|
-
"@eslint-react/eslint": "5.30.0-beta.
|
|
34
|
-
"@typescript-eslint/scope-manager": "^8.72.
|
|
35
|
-
"@typescript-eslint/types": "^8.72.
|
|
36
|
-
"@typescript-eslint/utils": "^8.72.
|
|
32
|
+
"@eslint-react/ast": "5.30.0-beta.10",
|
|
33
|
+
"@eslint-react/eslint": "5.30.0-beta.10",
|
|
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,7 +42,6 @@
|
|
|
42
42
|
"@local/testkit": "0.0.0",
|
|
43
43
|
"@tsconfig/node20": "^20.1.10",
|
|
44
44
|
"@types/node": "^20.19.43",
|
|
45
|
-
"@typescript-eslint/typescript-estree": "^8.72.0",
|
|
46
45
|
"eslint": "^10.13.0",
|
|
47
46
|
"tsdown": "^0.23.2",
|
|
48
47
|
"typescript": "6.0.3",
|