@eslint-react/ast 5.30.0-beta.10 → 5.30.0-beta.12
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 +68 -35
- package/dist/index.js +17 -4
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,11 @@ type TSESTreeClass = TSESTree.ClassDeclaration | TSESTree.ClassExpression;
|
|
|
11
11
|
type TSESTreeFunction = TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression;
|
|
12
12
|
/** Union of function-like and class-like node types. */
|
|
13
13
|
type TSESTreeFunctionOrClass = TSESTreeClass | TSESTreeFunction;
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* Union of `MethodDefinition` and `PropertyDefinition` node types.
|
|
16
|
+
*
|
|
17
|
+
* Abstract and accessor member definitions are not included.
|
|
18
|
+
*/
|
|
15
19
|
type TSESTreeMethodOrPropertyDefinition = TSESTree.MethodDefinition | TSESTree.PropertyDefinition;
|
|
16
20
|
/** Union of all JSX-related node types. */
|
|
17
21
|
type TSESTreeJSX = TSESTree.JSXAttribute | TSESTree.JSXClosingElement | TSESTree.JSXClosingFragment | TSESTree.JSXElement | TSESTree.JSXEmptyExpression | TSESTree.JSXExpressionContainer | TSESTree.JSXFragment | TSESTree.JSXIdentifier | TSESTree.JSXMemberExpression | TSESTree.JSXNamespacedName | TSESTree.JSXOpeningElement | TSESTree.JSXOpeningFragment | TSESTree.JSXSpreadAttribute | TSESTree.JSXSpreadChild | TSESTree.JSXText;
|
|
@@ -42,11 +46,11 @@ declare const isOneOf: <NodeTypes extends readonly AST_NODE_TYPES[]>(nodeTypes:
|
|
|
42
46
|
*/
|
|
43
47
|
declare function isIdentifier(node: TSESTree.Node, name?: string): node is TSESTree.Identifier;
|
|
44
48
|
/**
|
|
45
|
-
* Check if a node is an expression statement
|
|
46
|
-
*
|
|
49
|
+
* Check if a node is a directive (an expression statement in a directive prologue,
|
|
50
|
+
* ex: `"use client"`), optionally matching a specific name.
|
|
47
51
|
* @param node The node to check.
|
|
48
|
-
* @param name The directive name to match. When omitted, any
|
|
49
|
-
* @returns `true` if the node is
|
|
52
|
+
* @param name The directive name to match. When omitted, any directive matches.
|
|
53
|
+
* @returns `true` if the node is a directive and, when `name` is given, has that name.
|
|
50
54
|
*/
|
|
51
55
|
declare function isDirective(node: TSESTree.Node, name?: string): node is TSESTreeDirective;
|
|
52
56
|
/** Check if a node is a call expression. */
|
|
@@ -69,9 +73,19 @@ declare const isConditional: (node: TSESTree.Node | null | undefined) => node is
|
|
|
69
73
|
declare const isFunction: (node: TSESTree.Node | null | undefined) => node is TSESTree.ArrowFunctionExpressionWithBlockBody | TSESTree.ArrowFunctionExpressionWithExpressionBody | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName | TSESTree.FunctionExpression;
|
|
70
74
|
/** Check if a node is a function declaration, function expression, arrow function expression, class declaration, or class expression. */
|
|
71
75
|
declare const isFunctionOrClass: (node: TSESTree.Node | null | undefined) => node is TSESTree.ArrowFunctionExpressionWithBlockBody | TSESTree.ArrowFunctionExpressionWithExpressionBody | TSESTree.ClassDeclarationWithName | TSESTree.ClassDeclarationWithOptionalName | TSESTree.ClassExpression | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName | TSESTree.FunctionExpression;
|
|
72
|
-
/**
|
|
76
|
+
/**
|
|
77
|
+
* Check if a node is a `TSPropertySignature`, `PropertyDefinition`, `TSIndexSignature`, or `TSParameterProperty`.
|
|
78
|
+
*
|
|
79
|
+
* Abstract and accessor property definitions (`TSAbstractPropertyDefinition`, `AccessorProperty`,
|
|
80
|
+
* `TSAbstractAccessorProperty`) are not included.
|
|
81
|
+
*/
|
|
73
82
|
declare const isProperty: (node: TSESTree.Node | null | undefined) => node is TSESTree.PropertyDefinitionComputedName | TSESTree.PropertyDefinitionNonComputedName | TSESTree.TSIndexSignature | TSESTree.TSParameterProperty | TSESTree.TSPropertySignatureComputedName | TSESTree.TSPropertySignatureNonComputedName;
|
|
74
|
-
/**
|
|
83
|
+
/**
|
|
84
|
+
* Check if a node is a `MethodDefinition` or `PropertyDefinition`.
|
|
85
|
+
*
|
|
86
|
+
* Abstract and accessor member definitions (`TSAbstractMethodDefinition`, `TSAbstractPropertyDefinition`,
|
|
87
|
+
* `AccessorProperty`, `TSAbstractAccessorProperty`) are not included.
|
|
88
|
+
*/
|
|
75
89
|
declare const isPropertyOrMethod: (node: TSESTree.Node | null | undefined) => node is TSESTree.MethodDefinitionComputedName | TSESTree.MethodDefinitionNonComputedName | TSESTree.PropertyDefinitionComputedName | TSESTree.PropertyDefinitionNonComputedName;
|
|
76
90
|
/**
|
|
77
91
|
* Check if a node is the default value of an array pattern element, i.e. an
|
|
@@ -113,6 +127,13 @@ declare namespace compare_d_exports {
|
|
|
113
127
|
}
|
|
114
128
|
/**
|
|
115
129
|
* Check if two nodes are structurally equal.
|
|
130
|
+
*
|
|
131
|
+
* - Supports identifiers, literals, template literals/elements, member/call/chain expressions, JSX identifiers/namespaced names/member expressions/attributes, and `this`.
|
|
132
|
+
* - Node types outside that list (including functions and classes) are equal only when they are the same node.
|
|
133
|
+
* - Compares chain expressions through their inner expression, so optional segments remain significant (`a?.b` equals `a?.b` but not `a.b`).
|
|
134
|
+
* - Compares literals by value and regex literals by `pattern` and `flags`.
|
|
135
|
+
* - Ignores source locations (`loc`, `range`) and `parent` pointers.
|
|
136
|
+
* - Ignores type-level syntax: type annotations and type arguments are not compared, and type expressions are unwrapped, so `foo as string` equals `foo`.
|
|
116
137
|
* @param a The first node to compare.
|
|
117
138
|
* @param b The second node to compare.
|
|
118
139
|
* @returns `true` if the nodes are equal.
|
|
@@ -126,31 +147,34 @@ declare namespace collect_d_exports {
|
|
|
126
147
|
export { getCallExpressions, getIdentifiers, getNewExpressions, getReturnExpressions, getReturnStatements };
|
|
127
148
|
}
|
|
128
149
|
/**
|
|
129
|
-
* Get all identifiers
|
|
150
|
+
* Get all identifiers occurring within a node, in source order.
|
|
130
151
|
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
152
|
+
* This is a syntactic walk, not a scope-aware reference search: identifiers in patterns are
|
|
153
|
+
* collected too when the pattern is traversed (ex: `a` in `[a] = value`). The root is collected
|
|
154
|
+
* when it is an identifier. The property name of a non-computed member access is not a reference,
|
|
155
|
+
* so `obj.prop` collects only `obj`; non-computed object property keys are skipped as well.
|
|
156
|
+
* Function and class nodes are not traversed, and statement roots contribute only their listed
|
|
157
|
+
* expression-bearing keys.
|
|
158
|
+
* @param node The node to collect identifiers from.
|
|
159
|
+
* @returns All identifiers occurring in the node.
|
|
136
160
|
*/
|
|
137
161
|
declare function getIdentifiers(node: TSESTree.Node): readonly TSESTree.Identifier[];
|
|
138
162
|
/**
|
|
139
|
-
* Get all
|
|
163
|
+
* Get all `new` expressions within a node, in source order.
|
|
140
164
|
*
|
|
141
|
-
* Function and class
|
|
142
|
-
* expression-bearing
|
|
143
|
-
* @param node The
|
|
144
|
-
* @returns All
|
|
165
|
+
* The root is included when it is a `new` expression. Function and class nodes are not traversed,
|
|
166
|
+
* and statement roots contribute only their listed expression-bearing keys.
|
|
167
|
+
* @param node The node to collect `new` expressions from.
|
|
168
|
+
* @returns All matching `new` expressions.
|
|
145
169
|
*/
|
|
146
170
|
declare const getNewExpressions: (node: TSESTree.Node) => TSESTree.NewExpression[];
|
|
147
171
|
/**
|
|
148
|
-
* Get all
|
|
172
|
+
* Get all call expressions within a node, in source order.
|
|
149
173
|
*
|
|
150
|
-
* Function and class
|
|
151
|
-
* expression-bearing
|
|
152
|
-
* @param node The
|
|
153
|
-
* @returns All
|
|
174
|
+
* The root is included when it is a call expression. Function and class nodes are not traversed,
|
|
175
|
+
* and statement roots contribute only their listed expression-bearing keys.
|
|
176
|
+
* @param node The node to collect call expressions from.
|
|
177
|
+
* @returns All matching call expressions.
|
|
154
178
|
*/
|
|
155
179
|
declare const getCallExpressions: (node: TSESTree.Node) => TSESTree.CallExpression[];
|
|
156
180
|
/**
|
|
@@ -179,7 +203,7 @@ declare namespace extract_d_exports {
|
|
|
179
203
|
/**
|
|
180
204
|
* Recursively unwrap TypeScript type expressions and chain expressions to get the underlying expression.
|
|
181
205
|
* @param node The node to unwrap.
|
|
182
|
-
* @returns The innermost non-
|
|
206
|
+
* @returns The innermost non-wrapper node (`ChainExpression` and type expressions are stripped) — a reference into the original tree, never a copy.
|
|
183
207
|
*/
|
|
184
208
|
declare function unwrap(node: TSESTreeTypeOrChainExpression): Exclude<TSESTree.Expression, TSESTreeTypeOrChainExpression>;
|
|
185
209
|
declare function unwrap<T extends TSESTree.Node>(node: T): Exclude<T, TSESTreeTypeOrChainExpression>;
|
|
@@ -199,7 +223,7 @@ declare function getFullyQualifiedName(node: TSESTree.Node, getText: (node: TSES
|
|
|
199
223
|
/**
|
|
200
224
|
* Get the name of the callee of a call expression.
|
|
201
225
|
* @param node The call expression to inspect.
|
|
202
|
-
* @returns The callee name (ex: `"useState"`), or `null`
|
|
226
|
+
* @returns The callee name (ex: `"useState"`), or `null` for computed member expressions and other unsupported callee forms.
|
|
203
227
|
*/
|
|
204
228
|
declare function getCalleeName(node: TSESTree.CallExpression): string | null;
|
|
205
229
|
/**
|
|
@@ -218,6 +242,9 @@ declare function getMemberChain(node: TSESTree.Expression | TSESTree.PrivateIden
|
|
|
218
242
|
declare function getPropertyName(property: TSESTree.Property, effort?: "min" | "max"): string | null;
|
|
219
243
|
/**
|
|
220
244
|
* Find a property by name in a list of object literal properties, recursing into spread object expressions.
|
|
245
|
+
*
|
|
246
|
+
* Names are resolved with maximum effort: in addition to non-computed identifiers, string-literal keys
|
|
247
|
+
* (computed or not) and template literals without expressions are matched.
|
|
221
248
|
* @param properties The object literal properties to search.
|
|
222
249
|
* @param name The property name to look for.
|
|
223
250
|
* @returns The matching `Property` node, or `null` when not found.
|
|
@@ -228,7 +255,7 @@ declare function findProperty(properties: TSESTree.ObjectLiteralElement[], name:
|
|
|
228
255
|
* destructuring patterns such as `[local, globalValue] = source` or
|
|
229
256
|
* `({ a: obj.x } = source)`. Defaults (`{ a = source }`) are not targets;
|
|
230
257
|
* their left side is.
|
|
231
|
-
* @param node The assignment left side or for-in/of loop target to inspect.
|
|
258
|
+
* @param node The assignment left side or for-in/of loop target to inspect. Declaration-form loop targets are not expanded and return an empty array.
|
|
232
259
|
* @returns The identifier and member expression targets being written.
|
|
233
260
|
*/
|
|
234
261
|
declare function getAssignmentTargets(node: TSESTree.Node): (TSESTree.Identifier | TSESTree.MemberExpression)[];
|
|
@@ -238,19 +265,25 @@ declare namespace analyze_d_exports {
|
|
|
238
265
|
/**
|
|
239
266
|
* Check if a statement terminates the control flow of its block.
|
|
240
267
|
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
* not
|
|
268
|
+
* - Treats `return`, `throw`, `break`, and `continue` as terminating.
|
|
269
|
+
* - Treats a block as terminating when any direct statement terminates (uses `some`), so unreachable trailing code does not matter.
|
|
270
|
+
* - Treats an `if` as terminating only when it has an `else` and both branches terminate.
|
|
271
|
+
* - Treats a `try` as terminating when its finalizer terminates, or when its try block terminates and, if present, its catch body terminates.
|
|
272
|
+
* - Does not analyze `switch` statements, loops, or labeled statements, so an all-terminating `switch` or an infinite loop is not recognized.
|
|
273
|
+
* - Is purely syntactic: conditions are not evaluated and no reachability analysis is performed.
|
|
246
274
|
* @param node The node to check.
|
|
247
275
|
* @returns `true` if the statement terminates the control flow.
|
|
248
276
|
*/
|
|
249
277
|
declare function isTerminatingStatement(node: TSESTree.Node): boolean;
|
|
250
278
|
/**
|
|
251
|
-
* Check if every path through a statement exits the current function with a
|
|
252
|
-
*
|
|
253
|
-
* `continue` are not
|
|
279
|
+
* Check if every path through a statement exits the current function with a `return` or `throw`.
|
|
280
|
+
*
|
|
281
|
+
* - Treats only `return` and `throw` as terminating; unlike {@link isTerminatingStatement}, `break` and `continue` are not.
|
|
282
|
+
* - Inspects only the last statement of a block: `{ foo(); return; }` terminates, while `{ return; foo(); }` does not; earlier `break`/`continue` paths are not modeled.
|
|
283
|
+
* - Treats an `if` as terminating only when it has an `else` and both branches terminate.
|
|
284
|
+
* - Treats a `try` as terminating when its finalizer terminates, or when its try block terminates and, if present, its catch body terminates.
|
|
285
|
+
* - Does not analyze `switch` statements, loops, or labeled statements, so `while (true) {}` is not recognized.
|
|
286
|
+
* - Is purely syntactic: conditions are not evaluated and no reachability analysis is performed.
|
|
254
287
|
* @param node The node to check.
|
|
255
288
|
* @returns `true` if the statement unconditionally terminates.
|
|
256
289
|
*/
|
|
@@ -266,7 +299,7 @@ type SimpleTraverseOptions = {
|
|
|
266
299
|
enter?: (node: TSESTree.Node, parent: TSESTree.Node | undefined) => void;
|
|
267
300
|
/** Called for each visited node after its children are visited. */
|
|
268
301
|
leave?: (node: TSESTree.Node, parent: TSESTree.Node | undefined) => void;
|
|
269
|
-
/** Attach parent pointers to every visited node. */
|
|
302
|
+
/** Attach parent pointers to every visited node; the root's parent is removed. */
|
|
270
303
|
setParentPointers?: boolean;
|
|
271
304
|
/** Visitor keys to use instead of the default ESTree keys (ex: to prune subtrees). */
|
|
272
305
|
visitorKeys?: VisitorKeys;
|
package/dist/index.js
CHANGED
|
@@ -39,7 +39,7 @@ function isIdentifier(node, name) {
|
|
|
39
39
|
return node.type === AST_NODE_TYPES.Identifier && (name == null || node.name === name);
|
|
40
40
|
}
|
|
41
41
|
function isDirective(node, name) {
|
|
42
|
-
return node.type === AST_NODE_TYPES.ExpressionStatement && (name == null || node.directive === name);
|
|
42
|
+
return node.type === AST_NODE_TYPES.ExpressionStatement && node.directive != null && (name == null || node.directive === name);
|
|
43
43
|
}
|
|
44
44
|
const isCallExpression = is(AST_NODE_TYPES.CallExpression);
|
|
45
45
|
const isExpression = isOneOf([
|
|
@@ -333,7 +333,7 @@ function getPropertyName(property, effort = "min") {
|
|
|
333
333
|
}
|
|
334
334
|
function findProperty(properties, name) {
|
|
335
335
|
for (const property of properties) {
|
|
336
|
-
if (property.type === AST_NODE_TYPES.Property && getPropertyName(property) === name) return property;
|
|
336
|
+
if (property.type === AST_NODE_TYPES.Property && getPropertyName(property, "max") === name) return property;
|
|
337
337
|
if (property.type === AST_NODE_TYPES.SpreadElement && property.argument.type === AST_NODE_TYPES.ObjectExpression) {
|
|
338
338
|
const found = findProperty(property.argument.properties, name);
|
|
339
339
|
if (found != null) return found;
|
|
@@ -364,7 +364,9 @@ const isEqual = dual(2, (a, b) => {
|
|
|
364
364
|
switch (true) {
|
|
365
365
|
case a === b: return true;
|
|
366
366
|
case a.type !== b.type: return false;
|
|
367
|
-
case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal:
|
|
367
|
+
case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal:
|
|
368
|
+
if ("regex" in a || "regex" in b) return "regex" in a && "regex" in b && a.regex.pattern === b.regex.pattern && a.regex.flags === b.regex.flags;
|
|
369
|
+
return a.value === b.value;
|
|
368
370
|
case a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement: return a.value.raw === b.value.raw;
|
|
369
371
|
case a.type === AST_NODE_TYPES.TemplateLiteral && b.type === AST_NODE_TYPES.TemplateLiteral: {
|
|
370
372
|
if (a.quasis.length !== b.quasis.length || a.expressions.length !== b.expressions.length) return false;
|
|
@@ -381,6 +383,7 @@ const isEqual = dual(2, (a, b) => {
|
|
|
381
383
|
}
|
|
382
384
|
case isIdentifier(a) && isIdentifier(b): return a.name === b.name;
|
|
383
385
|
case a.type === AST_NODE_TYPES.PrivateIdentifier && b.type === AST_NODE_TYPES.PrivateIdentifier: return a.name === b.name;
|
|
386
|
+
case a.type === AST_NODE_TYPES.ChainExpression && b.type === AST_NODE_TYPES.ChainExpression: return isEqual(a.expression, b.expression);
|
|
384
387
|
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return a.computed === b.computed && a.optional === b.optional && isEqual(a.property, b.property) && isEqual(a.object, b.object);
|
|
385
388
|
case a.type === AST_NODE_TYPES.CallExpression && b.type === AST_NODE_TYPES.CallExpression: {
|
|
386
389
|
if (a.optional !== b.optional) return false;
|
|
@@ -485,7 +488,11 @@ const EXPRESSION_VISITOR_KEYS = {
|
|
|
485
488
|
[AST_NODE_TYPES.ExpressionStatement]: ["expression"],
|
|
486
489
|
[AST_NODE_TYPES.ForInStatement]: ["left", "right"],
|
|
487
490
|
[AST_NODE_TYPES.ForOfStatement]: ["left", "right"],
|
|
488
|
-
[AST_NODE_TYPES.ForStatement]: [
|
|
491
|
+
[AST_NODE_TYPES.ForStatement]: [
|
|
492
|
+
"init",
|
|
493
|
+
"test",
|
|
494
|
+
"update"
|
|
495
|
+
],
|
|
489
496
|
[AST_NODE_TYPES.IfStatement]: ["test"],
|
|
490
497
|
[AST_NODE_TYPES.ImportExpression]: ["source", "options"],
|
|
491
498
|
[AST_NODE_TYPES.JSXAttribute]: ["name", "value"],
|
|
@@ -530,6 +537,8 @@ const EXPRESSION_VISITOR_KEYS = {
|
|
|
530
537
|
[AST_NODE_TYPES.TSTypeAssertion]: ["expression"],
|
|
531
538
|
[AST_NODE_TYPES.UnaryExpression]: ["argument"],
|
|
532
539
|
[AST_NODE_TYPES.UpdateExpression]: ["argument"],
|
|
540
|
+
[AST_NODE_TYPES.VariableDeclaration]: ["declarations"],
|
|
541
|
+
[AST_NODE_TYPES.VariableDeclarator]: ["init"],
|
|
533
542
|
[AST_NODE_TYPES.WhileStatement]: ["test"],
|
|
534
543
|
[AST_NODE_TYPES.YieldExpression]: ["argument"]
|
|
535
544
|
};
|
|
@@ -609,6 +618,10 @@ function isUnconditionallyTerminating(node) {
|
|
|
609
618
|
return last != null && isUnconditionallyTerminating(last);
|
|
610
619
|
}
|
|
611
620
|
case AST_NODE_TYPES.IfStatement: return node.alternate != null && isUnconditionallyTerminating(node.consequent) && isUnconditionallyTerminating(node.alternate);
|
|
621
|
+
case AST_NODE_TYPES.TryStatement:
|
|
622
|
+
if (node.finalizer != null && isUnconditionallyTerminating(node.finalizer)) return true;
|
|
623
|
+
if (node.handler == null) return isUnconditionallyTerminating(node.block);
|
|
624
|
+
return isUnconditionallyTerminating(node.block) && isUnconditionallyTerminating(node.handler.body);
|
|
612
625
|
default: return false;
|
|
613
626
|
}
|
|
614
627
|
}
|