@eslint-react/var 5.30.0-beta.5 → 5.30.0-beta.7
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 -80
- package/dist/index.js +219 -134
- package/package.json +3 -3
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
|
-
* 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
64
|
*
|
|
65
65
|
* @param local - The local binding name.
|
|
66
66
|
*/
|
|
67
|
-
|
|
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.
|
|
71
|
+
*
|
|
72
|
+
* @param local - The local binding name.
|
|
73
|
+
*/
|
|
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,85 +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;
|
|
183
|
-
/**
|
|
184
|
-
* The result of {@link resolveBindingTarget}: an `AssignmentPattern` unwraps to its
|
|
185
|
-
* binding, while every other node is returned as is; `null` stays `null`.
|
|
186
|
-
*/
|
|
187
|
-
export type BindingTarget<T extends null | TSESTree.Node> = T extends null ? null : T extends TSESTree.AssignmentPattern ? TSESTree.BindingName : T;
|
|
188
|
-
/**
|
|
189
|
-
* Resolve the binding target of a pattern element, property value, or parameter:
|
|
190
|
-
* `AssignmentPattern` defaults unwrap to their left side, other nodes pass through.
|
|
191
|
-
* @param node The node to resolve.
|
|
192
|
-
* @returns The binding target, or `null` when `node` is `null`.
|
|
193
|
-
*/
|
|
194
|
-
export declare function resolveBindingTarget<T extends null | TSESTree.Node>(node: T): BindingTarget<T>;
|
|
250
|
+
export declare function resolve(context: RuleContext, node: TSESTree.Identifier, options?: ResolveOptions): TSESTree.Node | null;
|
|
195
251
|
/** The possible assignment targets returned by {@link resolveEnclosingAssignmentTarget}. */
|
|
196
|
-
export type EnclosingAssignmentTarget =
|
|
252
|
+
export type EnclosingAssignmentTarget = TSESTree.DefaultExportDeclarations | TSESTree.PropertyName | null;
|
|
197
253
|
/**
|
|
198
254
|
* Resolve the enclosing assignment target (variable, property, etc.) of the node.
|
|
199
255
|
* @param node The starting node for the upward search.
|
|
200
256
|
* @returns The enclosing assignment target node, or `null` when not found.
|
|
201
257
|
*/
|
|
202
|
-
export declare function resolveEnclosingAssignmentTarget(node: TSESTree.Node):
|
|
258
|
+
export declare function resolveEnclosingAssignmentTarget(node: TSESTree.Node): EnclosingAssignmentTarget;
|
|
203
259
|
/**
|
|
204
260
|
* Resolve the import source of a variable by walking its latest definition.
|
|
205
261
|
* @param name The variable name.
|
|
206
262
|
* @param initialScope The initial scope.
|
|
207
|
-
* @param seen The set of already visited variable names (for cycle detection).
|
|
208
263
|
* @returns The import source, or `null` when it cannot be resolved.
|
|
209
264
|
*/
|
|
210
|
-
export declare function resolveImportSource(name: string, initialScope: Scope
|
|
265
|
+
export declare function resolveImportSource(name: string, initialScope: Scope): string | null;
|
|
211
266
|
/**
|
|
212
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.
|
|
213
272
|
*/
|
|
214
273
|
export type ObjectType = {
|
|
215
|
-
kind: "jsx";
|
|
216
|
-
node: TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
274
|
+
readonly kind: "jsx";
|
|
275
|
+
readonly node: TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
276
|
+
readonly via: "jsx";
|
|
217
277
|
} | {
|
|
218
|
-
kind: "array";
|
|
219
|
-
node: TSESTree.ArrayExpression | TSESTree.CallExpression;
|
|
278
|
+
readonly kind: "array";
|
|
279
|
+
readonly node: TSESTree.ArrayExpression | TSESTree.CallExpression;
|
|
280
|
+
readonly via: "literal" | "call";
|
|
220
281
|
} | {
|
|
221
|
-
kind: "plain";
|
|
222
|
-
node: TSESTree.ObjectExpression | TSESTree.CallExpression;
|
|
282
|
+
readonly kind: "plain";
|
|
283
|
+
readonly node: TSESTree.ObjectExpression | TSESTree.CallExpression;
|
|
284
|
+
readonly via: "literal" | "call";
|
|
223
285
|
} | {
|
|
224
|
-
kind: "class";
|
|
225
|
-
node: TSESTree.ClassDeclaration | TSESTree.ClassExpression;
|
|
286
|
+
readonly kind: "class";
|
|
287
|
+
readonly node: TSESTree.ClassDeclaration | TSESTree.ClassExpression;
|
|
288
|
+
readonly via: "declaration" | "expression";
|
|
226
289
|
} | {
|
|
227
|
-
kind: "instance";
|
|
228
|
-
node: TSESTree.NewExpression | TSESTree.ThisExpression;
|
|
290
|
+
readonly kind: "instance";
|
|
291
|
+
readonly node: TSESTree.NewExpression | TSESTree.ThisExpression;
|
|
292
|
+
readonly via: "new" | "this";
|
|
229
293
|
} | {
|
|
230
|
-
kind: "function";
|
|
231
|
-
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";
|
|
232
297
|
} | {
|
|
233
|
-
kind: "regexp";
|
|
234
|
-
node: TSESTree.RegExpLiteral | TSESTree.CallExpression;
|
|
298
|
+
readonly kind: "regexp";
|
|
299
|
+
readonly node: TSESTree.RegExpLiteral | TSESTree.CallExpression;
|
|
300
|
+
readonly via: "literal" | "call";
|
|
235
301
|
} | {
|
|
236
|
-
kind: "unknown";
|
|
237
|
-
node: TSESTree.Node;
|
|
238
|
-
|
|
302
|
+
readonly kind: "unknown";
|
|
303
|
+
readonly node: TSESTree.Node;
|
|
304
|
+
/** Why the node could not be classified. */
|
|
305
|
+
readonly reason: "call-expression";
|
|
239
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
|
+
}
|
|
240
321
|
/**
|
|
241
322
|
* Resolve the object type of the node.
|
|
242
323
|
* @param context The ESLint rule context.
|
|
243
324
|
* @param node The node to resolve.
|
|
325
|
+
* @param options Optional resolution settings; see {@link ResolveObjectTypeOptions}.
|
|
244
326
|
* @returns The object type of the node, or `null` when it cannot be resolved.
|
|
245
327
|
*/
|
|
246
|
-
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;
|
|
247
329
|
/**
|
|
248
330
|
* Resolve an identifier to the AST node its value **originates from**,
|
|
249
331
|
* suitable for origin/pedigree tracking in ESLint rule analysis.
|
|
@@ -273,19 +355,36 @@ export declare function resolveObjectType(context: RuleContext, node: TSESTree.N
|
|
|
273
355
|
* `ImportDeclaration` carries the module source) instead of `null`.
|
|
274
356
|
*
|
|
275
357
|
* Use this for origin/pedigree tracking ("what produced this value?"); use
|
|
276
|
-
* {@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}.
|
|
277
360
|
*
|
|
278
361
|
* @param context The ESLint rule context.
|
|
279
362
|
* @param node The identifier to resolve.
|
|
280
|
-
* @param options Optional settings
|
|
281
|
-
* - `at`: Index of the definition to resolve (default: `0` for the first definition).
|
|
282
|
-
* - `localOnly`: If `true`, only consider variables declared in the same scope as the identifier
|
|
283
|
-
* (this will miss variables declared in an outer scope). When `false` (default), traverse the
|
|
284
|
-
* scope chain upward via `findVariable` so that references to outer-scope bindings are resolved
|
|
285
|
-
* correctly.
|
|
363
|
+
* @param options Optional resolution settings; see {@link ResolveOptions}.
|
|
286
364
|
* @returns The resolved origin node, or `null` if the identifier cannot be resolved.
|
|
287
365
|
*/
|
|
288
|
-
export declare function resolveOrigin(context: RuleContext, node: TSESTree.Identifier, options?:
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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
228
|
AST_NODE_TYPES.FunctionExpression,
|
|
205
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,42 +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
|
-
}
|
|
339
|
-
}
|
|
340
|
-
// src/resolve-binding-target.ts
|
|
341
|
-
function resolveBindingTarget(node) {
|
|
342
|
-
if (node == null) return null;
|
|
343
|
-
if (node.type === AST_NODE_TYPES.AssignmentPattern) return node.left;
|
|
344
|
-
return node;
|
|
354
|
+
const binding = getBinding(context, node, options);
|
|
355
|
+
if (binding?.definition == null) return null;
|
|
356
|
+
return projectBinding(binding.definition, "value");
|
|
345
357
|
}
|
|
346
358
|
// src/resolve-enclosing-assignment-target.ts
|
|
359
|
+
function getParent(node) {
|
|
360
|
+
return node.parent;
|
|
361
|
+
}
|
|
347
362
|
function resolveEnclosingAssignmentTarget(node) {
|
|
348
363
|
switch (node.type) {
|
|
349
364
|
case AST_NODE_TYPES.VariableDeclarator: return node.id;
|
|
@@ -352,97 +367,142 @@ function resolveEnclosingAssignmentTarget(node) {
|
|
|
352
367
|
case AST_NODE_TYPES.ExportDefaultDeclaration: return node.declaration;
|
|
353
368
|
case AST_NODE_TYPES.BlockStatement:
|
|
354
369
|
case AST_NODE_TYPES.Program: return null;
|
|
355
|
-
default:
|
|
370
|
+
default: {
|
|
371
|
+
const parent = getParent(node);
|
|
372
|
+
if (parent == null) return null;
|
|
373
|
+
return resolveEnclosingAssignmentTarget(parent);
|
|
374
|
+
}
|
|
356
375
|
}
|
|
357
376
|
}
|
|
358
377
|
// src/resolve-object-type.ts
|
|
359
|
-
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) {
|
|
360
389
|
if (node == null) return null;
|
|
361
390
|
switch (node.type) {
|
|
362
391
|
case AST_NODE_TYPES.JSXElement:
|
|
363
392
|
case AST_NODE_TYPES.JSXFragment: return {
|
|
364
393
|
kind: "jsx",
|
|
365
|
-
node
|
|
394
|
+
node,
|
|
395
|
+
via: "jsx"
|
|
366
396
|
};
|
|
367
397
|
case AST_NODE_TYPES.ArrayExpression: return {
|
|
368
398
|
kind: "array",
|
|
369
|
-
node
|
|
399
|
+
node,
|
|
400
|
+
via: "literal"
|
|
370
401
|
};
|
|
371
402
|
case AST_NODE_TYPES.ObjectExpression: return {
|
|
372
403
|
kind: "plain",
|
|
373
|
-
node
|
|
404
|
+
node,
|
|
405
|
+
via: "literal"
|
|
406
|
+
};
|
|
407
|
+
case AST_NODE_TYPES.ClassDeclaration: return {
|
|
408
|
+
kind: "class",
|
|
409
|
+
node,
|
|
410
|
+
via: "declaration"
|
|
374
411
|
};
|
|
375
|
-
case AST_NODE_TYPES.ClassDeclaration:
|
|
376
412
|
case AST_NODE_TYPES.ClassExpression: return {
|
|
377
413
|
kind: "class",
|
|
378
|
-
node
|
|
414
|
+
node,
|
|
415
|
+
via: "expression"
|
|
416
|
+
};
|
|
417
|
+
case AST_NODE_TYPES.NewExpression: return {
|
|
418
|
+
kind: "instance",
|
|
419
|
+
node,
|
|
420
|
+
via: "new"
|
|
379
421
|
};
|
|
380
|
-
case AST_NODE_TYPES.NewExpression:
|
|
381
422
|
case AST_NODE_TYPES.ThisExpression: return {
|
|
382
423
|
kind: "instance",
|
|
383
|
-
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"
|
|
384
436
|
};
|
|
385
|
-
case AST_NODE_TYPES.FunctionDeclaration:
|
|
386
|
-
case AST_NODE_TYPES.FunctionExpression:
|
|
387
437
|
case AST_NODE_TYPES.ArrowFunctionExpression: return {
|
|
388
438
|
kind: "function",
|
|
389
|
-
node
|
|
439
|
+
node,
|
|
440
|
+
via: "arrow"
|
|
390
441
|
};
|
|
391
442
|
case AST_NODE_TYPES.Literal:
|
|
392
443
|
if (hasProperty(node, "regex")) return {
|
|
393
444
|
kind: "regexp",
|
|
394
|
-
node
|
|
445
|
+
node,
|
|
446
|
+
via: "literal"
|
|
395
447
|
};
|
|
396
448
|
return null;
|
|
397
449
|
case AST_NODE_TYPES.Identifier: {
|
|
398
|
-
if (
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
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
|
|
402
455
|
});
|
|
456
|
+
if (binding?.definition?.type === DefinitionType.Parameter) return null;
|
|
457
|
+
const initNode = binding?.definition == null ? null : projectBinding(binding.definition, "value");
|
|
403
458
|
if (initNode == null) return null;
|
|
404
|
-
return
|
|
459
|
+
return resolveObjectTypeInner(context, initNode, seen, options);
|
|
405
460
|
}
|
|
406
|
-
case AST_NODE_TYPES.MemberExpression: return
|
|
461
|
+
case AST_NODE_TYPES.MemberExpression: return resolveObjectTypeInner(context, node.object, seen, options);
|
|
407
462
|
case AST_NODE_TYPES.AssignmentExpression:
|
|
408
|
-
case AST_NODE_TYPES.AssignmentPattern: return
|
|
409
|
-
case AST_NODE_TYPES.LogicalExpression: return
|
|
410
|
-
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);
|
|
411
466
|
case AST_NODE_TYPES.SequenceExpression:
|
|
412
467
|
if (node.expressions.length === 0) return null;
|
|
413
|
-
return
|
|
468
|
+
return resolveObjectTypeInner(context, node.expressions.at(-1) ?? null, seen, options);
|
|
414
469
|
case AST_NODE_TYPES.CallExpression: {
|
|
415
470
|
const callee = Extract.unwrap(node.callee);
|
|
416
471
|
switch (true) {
|
|
417
|
-
case Check.isIdentifier(callee, "Boolean"): return null;
|
|
418
|
-
case Check.isIdentifier(callee, "String"): return null;
|
|
419
|
-
case Check.isIdentifier(callee, "Number"): return null;
|
|
420
|
-
case Check.isIdentifier(callee, "Object"): return {
|
|
472
|
+
case Check.isIdentifier(callee, "Boolean") && isUnshadowedGlobal(context, callee): return null;
|
|
473
|
+
case Check.isIdentifier(callee, "String") && isUnshadowedGlobal(context, callee): return null;
|
|
474
|
+
case Check.isIdentifier(callee, "Number") && isUnshadowedGlobal(context, callee): return null;
|
|
475
|
+
case Check.isIdentifier(callee, "Object") && isUnshadowedGlobal(context, callee): return {
|
|
421
476
|
kind: "plain",
|
|
422
|
-
node
|
|
477
|
+
node,
|
|
478
|
+
via: "call"
|
|
423
479
|
};
|
|
424
|
-
case Check.isIdentifier(callee, "Array"): return {
|
|
480
|
+
case Check.isIdentifier(callee, "Array") && isUnshadowedGlobal(context, callee): return {
|
|
425
481
|
kind: "array",
|
|
426
|
-
node
|
|
482
|
+
node,
|
|
483
|
+
via: "call"
|
|
427
484
|
};
|
|
428
|
-
case Check.isIdentifier(callee, "RegExp"): return {
|
|
485
|
+
case Check.isIdentifier(callee, "RegExp") && isUnshadowedGlobal(context, callee): return {
|
|
429
486
|
kind: "regexp",
|
|
430
|
-
node
|
|
487
|
+
node,
|
|
488
|
+
via: "call"
|
|
431
489
|
};
|
|
432
490
|
}
|
|
433
|
-
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)) {
|
|
434
492
|
const objName = callee.object.name;
|
|
435
493
|
const methodName = Extract.getCalleeName(node);
|
|
436
494
|
switch (objName) {
|
|
437
495
|
case "Array":
|
|
438
496
|
if (methodName === "from" || methodName === "of") return {
|
|
439
497
|
kind: "array",
|
|
440
|
-
node
|
|
498
|
+
node,
|
|
499
|
+
via: "call"
|
|
441
500
|
};
|
|
442
501
|
break;
|
|
443
502
|
case "Object": if (methodName === "create" || methodName === "assign" || methodName === "fromEntries") return {
|
|
444
503
|
kind: "plain",
|
|
445
|
-
node
|
|
504
|
+
node,
|
|
505
|
+
via: "call"
|
|
446
506
|
};
|
|
447
507
|
}
|
|
448
508
|
}
|
|
@@ -454,7 +514,32 @@ function resolveObjectType(context, node) {
|
|
|
454
514
|
}
|
|
455
515
|
default:
|
|
456
516
|
if (!hasProperty(node, "expression") || typeof node.expression !== "object") return null;
|
|
457
|
-
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);
|
|
458
542
|
}
|
|
543
|
+
return Check.isIdentifier(current) ? null : current;
|
|
459
544
|
}
|
|
460
|
-
export { createImportLookup, isAssignmentTargetEqual, isInitializedFromReact, isInitializedFromReactNative, isValueEqual, resolve,
|
|
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.7",
|
|
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,8 +29,8 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@eslint-react/ast": "5.30.0-beta.
|
|
33
|
-
"@eslint-react/eslint": "5.30.0-beta.
|
|
32
|
+
"@eslint-react/ast": "5.30.0-beta.7",
|
|
33
|
+
"@eslint-react/eslint": "5.30.0-beta.7",
|
|
34
34
|
"@typescript-eslint/scope-manager": "^8.72.1",
|
|
35
35
|
"@typescript-eslint/types": "^8.72.1",
|
|
36
36
|
"@typescript-eslint/utils": "^8.72.1",
|