@eslint-react/ast 5.30.0-beta.9 → 5.30.0-rc.0
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 +72 -68
- package/dist/index.js +89 -58
- package/package.json +4 -4
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;
|
|
@@ -24,7 +28,7 @@ type TSESTreeTypeExpression = TSESTree.TSAsExpression | TSESTree.TSInstantiation
|
|
|
24
28
|
/** Union of expression node types that wrap another expression (TypeScript type expressions and chain expressions) — the nodes `Extract.unwrap` strips. */
|
|
25
29
|
type TSESTreeTypeOrChainExpression = TSESTree.ChainExpression | TSESTreeTypeExpression;
|
|
26
30
|
declare namespace check_d_exports {
|
|
27
|
-
export { is, isArrayPatternElementDefault, isCallExpression, isClass,
|
|
31
|
+
export { is, isArrayPatternElementDefault, isCallExpression, isClass, isConditionalOrLoop, isDirective, isExpression, isFunction, isFunctionOrClass, isIdentifier, isJSX, isJSXElement, isJSXElementOrFragment, isJSXExpressionContainer, isJSXFragment, isJSXSpreadAttribute, isJSXTagNameExpression, isLoopStatement, isMemberExpression, isObjectExpression, isObjectPatternElementDefault, isOneOf, isProperty, isPropertyOrMethod, isReturnStatement, isTypeAssertionExpression, isTypeExpression, isTypeOrChainExpression };
|
|
28
32
|
}
|
|
29
33
|
/** Check if a node is of the given type. */
|
|
30
34
|
declare const is: <NodeType extends AST_NODE_TYPES>(nodeType: NodeType) => (node: TSESTree.Node | null | undefined) => node is Extract<TSESTree.Node, {
|
|
@@ -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. */
|
|
@@ -63,15 +67,25 @@ declare const isLoopStatement: (node: TSESTree.Node | null | undefined) => node
|
|
|
63
67
|
declare const isReturnStatement: (node: TSESTree.Node | null | undefined) => node is TSESTree.ReturnStatement;
|
|
64
68
|
/** Check if a node is a class declaration or class expression. */
|
|
65
69
|
declare const isClass: (node: TSESTree.Node | null | undefined) => node is TSESTree.ClassDeclarationWithName | TSESTree.ClassDeclarationWithOptionalName | TSESTree.ClassExpression;
|
|
66
|
-
/** Check if a node is a conditional or logical expression, or a
|
|
67
|
-
declare const
|
|
70
|
+
/** Check if a node is a conditional or logical expression, a conditional statement, or a loop statement. */
|
|
71
|
+
declare const isConditionalOrLoop: (node: TSESTree.Node | null | undefined) => node is TSESTree.ConditionalExpression | TSESTree.DoWhileStatement | TSESTree.ForInStatement | TSESTree.ForOfStatement | TSESTree.ForStatement | TSESTree.IfStatement | TSESTree.LogicalExpression | TSESTree.SwitchStatement | TSESTree.WhileStatement;
|
|
68
72
|
/** Check if a node is a function declaration, function expression, or arrow function expression. */
|
|
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, private 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,35 @@ 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, unless the root itself is a non-computed member property or object
|
|
155
|
+
* key. The property name of a non-computed member access is not a reference, so `obj.prop`
|
|
156
|
+
* collects only `obj`; non-computed object property keys are skipped as well.
|
|
157
|
+
* Function and class nodes are not traversed, and statement roots contribute only their listed
|
|
158
|
+
* expression-bearing keys.
|
|
159
|
+
* @param node The node to collect identifiers from.
|
|
160
|
+
* @returns All identifiers occurring in the node.
|
|
136
161
|
*/
|
|
137
162
|
declare function getIdentifiers(node: TSESTree.Node): readonly TSESTree.Identifier[];
|
|
138
163
|
/**
|
|
139
|
-
* Get all
|
|
164
|
+
* Get all `new` expressions within a node, in source order.
|
|
140
165
|
*
|
|
141
|
-
* Function and class
|
|
142
|
-
* expression-bearing
|
|
143
|
-
* @param node The
|
|
144
|
-
* @returns All
|
|
166
|
+
* The root is included when it is a `new` expression. Function and class nodes are not traversed,
|
|
167
|
+
* and statement roots contribute only their listed expression-bearing keys.
|
|
168
|
+
* @param node The node to collect `new` expressions from.
|
|
169
|
+
* @returns All matching `new` expressions.
|
|
145
170
|
*/
|
|
146
171
|
declare const getNewExpressions: (node: TSESTree.Node) => TSESTree.NewExpression[];
|
|
147
172
|
/**
|
|
148
|
-
* Get all
|
|
173
|
+
* Get all call expressions within a node, in source order.
|
|
149
174
|
*
|
|
150
|
-
* Function and class
|
|
151
|
-
* expression-bearing
|
|
152
|
-
* @param node The
|
|
153
|
-
* @returns All
|
|
175
|
+
* The root is included when it is a call expression. Function and class nodes are not traversed,
|
|
176
|
+
* and statement roots contribute only their listed expression-bearing keys.
|
|
177
|
+
* @param node The node to collect call expressions from.
|
|
178
|
+
* @returns All matching call expressions.
|
|
154
179
|
*/
|
|
155
180
|
declare const getCallExpressions: (node: TSESTree.Node) => TSESTree.CallExpression[];
|
|
156
181
|
/**
|
|
@@ -174,12 +199,12 @@ declare function getReturnStatements(node: TSESTree.Node): readonly TSESTree.Ret
|
|
|
174
199
|
*/
|
|
175
200
|
declare function getReturnExpressions(node: TSESTreeFunction): readonly TSESTree.Expression[];
|
|
176
201
|
declare namespace extract_d_exports {
|
|
177
|
-
export {
|
|
202
|
+
export { getCalleeName, getFullyQualifiedName, getJSXName, unwrap };
|
|
178
203
|
}
|
|
179
204
|
/**
|
|
180
205
|
* Recursively unwrap TypeScript type expressions and chain expressions to get the underlying expression.
|
|
181
206
|
* @param node The node to unwrap.
|
|
182
|
-
* @returns The innermost non-
|
|
207
|
+
* @returns The innermost non-wrapper node (`ChainExpression` and type expressions are stripped) — a reference into the original tree, never a copy.
|
|
183
208
|
*/
|
|
184
209
|
declare function unwrap(node: TSESTreeTypeOrChainExpression): Exclude<TSESTree.Expression, TSESTreeTypeOrChainExpression>;
|
|
185
210
|
declare function unwrap<T extends TSESTree.Node>(node: T): Exclude<T, TSESTreeTypeOrChainExpression>;
|
|
@@ -199,58 +224,37 @@ declare function getFullyQualifiedName(node: TSESTree.Node, getText: (node: TSES
|
|
|
199
224
|
/**
|
|
200
225
|
* Get the name of the callee of a call expression.
|
|
201
226
|
* @param node The call expression to inspect.
|
|
202
|
-
* @returns The callee name (ex: `"useState"`), or `null`
|
|
227
|
+
* @returns The callee name (ex: `"useState"`), or `null` for computed member expressions and other unsupported callee forms.
|
|
203
228
|
*/
|
|
204
229
|
declare function getCalleeName(node: TSESTree.CallExpression): string | null;
|
|
205
|
-
/**
|
|
206
|
-
* Get the member chain of an expression (ex: `[a, b, c]` for `a.b.c`), starting from the base object.
|
|
207
|
-
* Type expressions and chain expressions are unwrapped along the way.
|
|
208
|
-
* @param node The expression to inspect.
|
|
209
|
-
* @returns The base node followed by each member property in access order.
|
|
210
|
-
*/
|
|
211
|
-
declare function getMemberChain(node: TSESTree.Expression | TSESTree.PrivateIdentifier): (TSESTree.AccessorProperty | TSESTree.ArrayExpression | TSESTree.ArrayPattern | TSESTree.ArrowFunctionExpression | TSESTree.AssignmentExpression | TSESTree.AssignmentPattern | TSESTree.AwaitExpression | TSESTree.BinaryExpression | TSESTree.BlockStatement | TSESTree.BreakStatement | TSESTree.CallExpression | TSESTree.CatchClause | TSESTree.ClassBody | TSESTree.ClassDeclaration | TSESTree.ClassExpression | TSESTree.ConditionalExpression | TSESTree.ContinueStatement | TSESTree.DebuggerStatement | TSESTree.Decorator | TSESTree.DoWhileStatement | TSESTree.EmptyStatement | TSESTree.ExportAllDeclaration | TSESTree.ExportDefaultDeclaration | TSESTree.ExportNamedDeclaration | TSESTree.ExportSpecifier | TSESTree.ExpressionStatement | TSESTree.ForInStatement | TSESTree.ForOfStatement | TSESTree.ForStatement | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.Identifier | TSESTree.IfStatement | TSESTree.ImportAttribute | TSESTree.ImportDeclaration | TSESTree.ImportDefaultSpecifier | TSESTree.ImportExpression | TSESTree.ImportNamespaceSpecifier | TSESTree.ImportSpecifier | 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 | TSESTree.LabeledStatement | TSESTree.Literal | TSESTree.LogicalExpression | TSESTree.MemberExpression | TSESTree.MetaProperty | TSESTree.MethodDefinition | TSESTree.NewExpression | TSESTree.ObjectExpression | TSESTree.ObjectPattern | TSESTree.PrivateIdentifier | TSESTree.Program | TSESTree.Property | TSESTree.PropertyDefinition | TSESTree.RestElement | TSESTree.ReturnStatement | TSESTree.SequenceExpression | TSESTree.SpreadElement | TSESTree.StaticBlock | TSESTree.Super | TSESTree.SwitchCase | TSESTree.SwitchStatement | TSESTree.TaggedTemplateExpression | TSESTree.TemplateElement | TSESTree.TemplateLiteral | TSESTree.ThisExpression | TSESTree.ThrowStatement | TSESTree.TryStatement | TSESTree.TSAbstractAccessorProperty | TSESTree.TSAbstractKeyword | TSESTree.TSAbstractMethodDefinition | TSESTree.TSAbstractPropertyDefinition | TSESTree.TSAnyKeyword | TSESTree.TSArrayType | TSESTree.TSAsyncKeyword | TSESTree.TSBigIntKeyword | TSESTree.TSBooleanKeyword | TSESTree.TSCallSignatureDeclaration | TSESTree.TSClassImplements | TSESTree.TSConditionalType | TSESTree.TSConstructorType | TSESTree.TSConstructSignatureDeclaration | TSESTree.TSDeclareFunction | TSESTree.TSDeclareKeyword | TSESTree.TSEmptyBodyFunctionExpression | TSESTree.TSEnumBody | TSESTree.TSEnumDeclaration | TSESTree.TSEnumMember | TSESTree.TSExportAssignment | TSESTree.TSExportKeyword | TSESTree.TSExternalModuleReference | TSESTree.TSFunctionType | TSESTree.TSImportEqualsDeclaration | TSESTree.TSImportType | TSESTree.TSIndexedAccessType | TSESTree.TSIndexSignature | TSESTree.TSInferType | TSESTree.TSInterfaceBody | TSESTree.TSInterfaceDeclaration | TSESTree.TSInterfaceHeritage | TSESTree.TSIntersectionType | TSESTree.TSIntrinsicKeyword | TSESTree.TSLiteralType | TSESTree.TSMappedType | TSESTree.TSMethodSignature | TSESTree.TSModuleBlock | TSESTree.TSModuleDeclaration | TSESTree.TSNamedTupleMember | TSESTree.TSNamespaceExportDeclaration | TSESTree.TSNeverKeyword | TSESTree.TSNullKeyword | TSESTree.TSNumberKeyword | TSESTree.TSObjectKeyword | TSESTree.TSOptionalType | TSESTree.TSParameterProperty | TSESTree.TSPrivateKeyword | TSESTree.TSPropertySignature | TSESTree.TSProtectedKeyword | TSESTree.TSPublicKeyword | TSESTree.TSQualifiedName | TSESTree.TSReadonlyKeyword | TSESTree.TSRestType | TSESTree.TSStaticKeyword | TSESTree.TSStringKeyword | TSESTree.TSSymbolKeyword | TSESTree.TSTemplateLiteralType | TSESTree.TSThisType | TSESTree.TSTupleType | TSESTree.TSTypeAliasDeclaration | TSESTree.TSTypeAnnotation | TSESTree.TSTypeLiteral | TSESTree.TSTypeOperator | TSESTree.TSTypeParameter | TSESTree.TSTypeParameterDeclaration | TSESTree.TSTypeParameterInstantiation | TSESTree.TSTypePredicate | TSESTree.TSTypeQuery | TSESTree.TSTypeReference | TSESTree.TSUndefinedKeyword | TSESTree.TSUnionType | TSESTree.TSUnknownKeyword | TSESTree.TSVoidKeyword | TSESTree.UnaryExpression | TSESTree.UpdateExpression | TSESTree.VariableDeclaration | TSESTree.VariableDeclarator | TSESTree.WhileStatement | TSESTree.WithStatement | TSESTree.YieldExpression)[];
|
|
212
|
-
/**
|
|
213
|
-
* Get the static name of an object property's key.
|
|
214
|
-
* @param property The property to inspect.
|
|
215
|
-
* @param effort `"min"` only matches plain identifiers; `"max"` also resolves string literals and simple template literals.
|
|
216
|
-
* @returns The property name, or `null` when it cannot be statically determined.
|
|
217
|
-
*/
|
|
218
|
-
declare function getPropertyName(property: TSESTree.Property, effort?: "min" | "max"): string | null;
|
|
219
|
-
/**
|
|
220
|
-
* Find a property by name in a list of object literal properties, recursing into spread object expressions.
|
|
221
|
-
* @param properties The object literal properties to search.
|
|
222
|
-
* @param name The property name to look for.
|
|
223
|
-
* @returns The matching `Property` node, or `null` when not found.
|
|
224
|
-
*/
|
|
225
|
-
declare function findProperty(properties: TSESTree.ObjectLiteralElement[], name: string): TSESTree.Property | null;
|
|
226
|
-
/**
|
|
227
|
-
* Collect every write target of an assignment or loop target, expanding
|
|
228
|
-
* destructuring patterns such as `[local, globalValue] = source` or
|
|
229
|
-
* `({ a: obj.x } = source)`. Defaults (`{ a = source }`) are not targets;
|
|
230
|
-
* their left side is.
|
|
231
|
-
* @param node The assignment left side or for-in/of loop target to inspect.
|
|
232
|
-
* @returns The identifier and member expression targets being written.
|
|
233
|
-
*/
|
|
234
|
-
declare function getAssignmentTargets(node: TSESTree.Node): (TSESTree.Identifier | TSESTree.MemberExpression)[];
|
|
235
230
|
declare namespace analyze_d_exports {
|
|
236
231
|
export { isTerminatingStatement, isUnconditionallyTerminating };
|
|
237
232
|
}
|
|
238
233
|
/**
|
|
239
234
|
* Check if a statement terminates the control flow of its block.
|
|
240
235
|
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
* not
|
|
236
|
+
* - Treats `return`, `throw`, `break`, and `continue` as terminating.
|
|
237
|
+
* - Treats a block as terminating when any direct statement terminates (uses `some`), so unreachable trailing code does not matter.
|
|
238
|
+
* - Treats an `if` as terminating only when it has an `else` and both branches terminate.
|
|
239
|
+
* - Treats a `try` as terminating when its finalizer terminates, or when its try block terminates and, if present, its catch body terminates.
|
|
240
|
+
* - Does not analyze `switch` statements, loops, or labeled statements, so an all-terminating `switch` or an infinite loop is not recognized.
|
|
241
|
+
* - Is purely syntactic: conditions are not evaluated and no reachability analysis is performed.
|
|
246
242
|
* @param node The node to check.
|
|
247
243
|
* @returns `true` if the statement terminates the control flow.
|
|
248
244
|
*/
|
|
249
245
|
declare function isTerminatingStatement(node: TSESTree.Node): boolean;
|
|
250
246
|
/**
|
|
251
|
-
* Check if every path through a statement exits the current function with a
|
|
252
|
-
*
|
|
253
|
-
* `continue` are not
|
|
247
|
+
* Check if every path through a statement exits the current function with a `return` or `throw`.
|
|
248
|
+
*
|
|
249
|
+
* - Treats only `return` and `throw` as terminating; unlike {@link isTerminatingStatement}, `break` and `continue` are not.
|
|
250
|
+
* - Inspects only the last statement of a block: `{ foo(); return; }` terminates, while `{ return; foo(); }` does not; earlier `break`/`continue` paths are not modeled.
|
|
251
|
+
* - Treats an `if` as terminating only when it has an `else` and both branches terminate.
|
|
252
|
+
* - Treats a `try` as terminating when its finalizer terminates, or when its try block terminates and, if present, its catch body terminates.
|
|
253
|
+
* - Treats a `while (true)` or `for (;;)` loop as terminating when its body terminates and contains no exiting `break`.
|
|
254
|
+
* - Treats a `do...while` loop as terminating when its body terminates and contains no exiting `break` or `continue`.
|
|
255
|
+
* - Treats a `switch` with a `default` clause as terminating when every clause's statements terminate and no clause contains an exiting `break`/`continue`; fall-through clause groups are not recognized.
|
|
256
|
+
* - Does not analyze labeled statements, and any labeled `break`/`continue` disqualifies the enclosing loop or switch conservatively.
|
|
257
|
+
* - Is purely syntactic: conditions are not evaluated and no reachability analysis is performed.
|
|
254
258
|
* @param node The node to check.
|
|
255
259
|
* @returns `true` if the statement unconditionally terminates.
|
|
256
260
|
*/
|
|
@@ -266,7 +270,7 @@ type SimpleTraverseOptions = {
|
|
|
266
270
|
enter?: (node: TSESTree.Node, parent: TSESTree.Node | undefined) => void;
|
|
267
271
|
/** Called for each visited node after its children are visited. */
|
|
268
272
|
leave?: (node: TSESTree.Node, parent: TSESTree.Node | undefined) => void;
|
|
269
|
-
/** Attach parent pointers to every visited node. */
|
|
273
|
+
/** Attach parent pointers to every visited node; the root's parent is removed. */
|
|
270
274
|
setParentPointers?: boolean;
|
|
271
275
|
/** Visitor keys to use instead of the default ESTree keys (ex: to prune subtrees). */
|
|
272
276
|
visitorKeys?: VisitorKeys;
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@ var check_exports = /* @__PURE__ */ __exportAll({
|
|
|
8
8
|
isArrayPatternElementDefault: () => isArrayPatternElementDefault,
|
|
9
9
|
isCallExpression: () => isCallExpression,
|
|
10
10
|
isClass: () => isClass,
|
|
11
|
-
|
|
11
|
+
isConditionalOrLoop: () => isConditionalOrLoop,
|
|
12
12
|
isDirective: () => isDirective,
|
|
13
13
|
isExpression: () => isExpression,
|
|
14
14
|
isFunction: () => isFunction$1,
|
|
@@ -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([
|
|
@@ -90,7 +90,7 @@ const isLoopStatement = isOneOf([
|
|
|
90
90
|
]);
|
|
91
91
|
const isReturnStatement = is(AST_NODE_TYPES.ReturnStatement);
|
|
92
92
|
const isClass = isOneOf([AST_NODE_TYPES.ClassDeclaration, AST_NODE_TYPES.ClassExpression]);
|
|
93
|
-
const
|
|
93
|
+
const isConditionalOrLoop = isOneOf([
|
|
94
94
|
AST_NODE_TYPES.IfStatement,
|
|
95
95
|
AST_NODE_TYPES.LogicalExpression,
|
|
96
96
|
AST_NODE_TYPES.ConditionalExpression,
|
|
@@ -244,9 +244,6 @@ dual(2, (a, b) => (data) => a(data) === b(data));
|
|
|
244
244
|
dual(2, (antecedent, consequent) => (data) => !antecedent(data) || consequent(data));
|
|
245
245
|
dual(2, (a, b) => (data) => !a(data) && !b(data));
|
|
246
246
|
dual(2, (a, b) => (data) => !a(data) || !b(data));
|
|
247
|
-
function isString(input) {
|
|
248
|
-
return typeof input === "string";
|
|
249
|
-
}
|
|
250
247
|
function isFunction(input) {
|
|
251
248
|
return typeof input === "function";
|
|
252
249
|
}
|
|
@@ -270,13 +267,9 @@ dual(2, (self, predicate) => {
|
|
|
270
267
|
});
|
|
271
268
|
// src/extract.ts
|
|
272
269
|
var extract_exports = /* @__PURE__ */ __exportAll({
|
|
273
|
-
findProperty: () => findProperty,
|
|
274
|
-
getAssignmentTargets: () => getAssignmentTargets,
|
|
275
270
|
getCalleeName: () => getCalleeName,
|
|
276
271
|
getFullyQualifiedName: () => getFullyQualifiedName,
|
|
277
272
|
getJSXName: () => getJSXName,
|
|
278
|
-
getMemberChain: () => getMemberChain,
|
|
279
|
-
getPropertyName: () => getPropertyName,
|
|
280
273
|
unwrap: () => unwrap
|
|
281
274
|
});
|
|
282
275
|
function unwrap(node) {
|
|
@@ -293,8 +286,8 @@ function getJSXName(node) {
|
|
|
293
286
|
function getFullyQualifiedName(node, getText) {
|
|
294
287
|
const expr = unwrap(node);
|
|
295
288
|
switch (expr.type) {
|
|
296
|
-
case AST_NODE_TYPES.Identifier:
|
|
297
|
-
case AST_NODE_TYPES.PrivateIdentifier: return expr.name
|
|
289
|
+
case AST_NODE_TYPES.Identifier: return expr.name;
|
|
290
|
+
case AST_NODE_TYPES.PrivateIdentifier: return `#${expr.name}`;
|
|
298
291
|
case AST_NODE_TYPES.JSXIdentifier:
|
|
299
292
|
case AST_NODE_TYPES.JSXMemberExpression:
|
|
300
293
|
case AST_NODE_TYPES.JSXNamespacedName: return getJSXName(expr);
|
|
@@ -312,50 +305,6 @@ function getCalleeName(node) {
|
|
|
312
305
|
if (callee.type === AST_NODE_TYPES.MemberExpression && !callee.computed && isIdentifier(callee.property)) return callee.property.name;
|
|
313
306
|
return null;
|
|
314
307
|
}
|
|
315
|
-
function getMemberChain(node) {
|
|
316
|
-
const members = [];
|
|
317
|
-
let current = unwrap(node);
|
|
318
|
-
while (current.type === AST_NODE_TYPES.MemberExpression) {
|
|
319
|
-
const property = unwrap(current.property);
|
|
320
|
-
members.unshift(property);
|
|
321
|
-
current = unwrap(current.object);
|
|
322
|
-
}
|
|
323
|
-
members.unshift(current);
|
|
324
|
-
return members;
|
|
325
|
-
}
|
|
326
|
-
function getPropertyName(property, effort = "min") {
|
|
327
|
-
const key = unwrap(property.key);
|
|
328
|
-
if (isIdentifier(key) && !property.computed) return key.name;
|
|
329
|
-
if (effort === "min") return null;
|
|
330
|
-
if (key.type === AST_NODE_TYPES.Literal && isString(key.value)) return key.value;
|
|
331
|
-
if (key.type === AST_NODE_TYPES.TemplateLiteral && key.expressions.length === 0) return key.quasis[0]?.value.cooked ?? key.quasis[0]?.value.raw ?? null;
|
|
332
|
-
return null;
|
|
333
|
-
}
|
|
334
|
-
function findProperty(properties, name) {
|
|
335
|
-
for (const property of properties) {
|
|
336
|
-
if (property.type === AST_NODE_TYPES.Property && getPropertyName(property) === name) return property;
|
|
337
|
-
if (property.type === AST_NODE_TYPES.SpreadElement && property.argument.type === AST_NODE_TYPES.ObjectExpression) {
|
|
338
|
-
const found = findProperty(property.argument.properties, name);
|
|
339
|
-
if (found != null) return found;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
return null;
|
|
343
|
-
}
|
|
344
|
-
function getAssignmentTargets(node) {
|
|
345
|
-
const target = unwrap(node);
|
|
346
|
-
switch (target.type) {
|
|
347
|
-
case AST_NODE_TYPES.Identifier:
|
|
348
|
-
case AST_NODE_TYPES.MemberExpression: return [target];
|
|
349
|
-
case AST_NODE_TYPES.ArrayPattern: return target.elements.flatMap((element) => element == null ? [] : getAssignmentTargets(element));
|
|
350
|
-
case AST_NODE_TYPES.AssignmentPattern: return getAssignmentTargets(target.left);
|
|
351
|
-
case AST_NODE_TYPES.ObjectPattern: return target.properties.flatMap((property) => {
|
|
352
|
-
if (property.type === AST_NODE_TYPES.RestElement) return getAssignmentTargets(property.argument);
|
|
353
|
-
return getAssignmentTargets(property.value);
|
|
354
|
-
});
|
|
355
|
-
case AST_NODE_TYPES.RestElement: return getAssignmentTargets(target.argument);
|
|
356
|
-
default: return [];
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
308
|
// src/compare.ts
|
|
360
309
|
var compare_exports = /* @__PURE__ */ __exportAll({ isEqual: () => isEqual });
|
|
361
310
|
const isEqual = dual(2, (a, b) => {
|
|
@@ -364,7 +313,9 @@ const isEqual = dual(2, (a, b) => {
|
|
|
364
313
|
switch (true) {
|
|
365
314
|
case a === b: return true;
|
|
366
315
|
case a.type !== b.type: return false;
|
|
367
|
-
case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal:
|
|
316
|
+
case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal:
|
|
317
|
+
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;
|
|
318
|
+
return a.value === b.value;
|
|
368
319
|
case a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement: return a.value.raw === b.value.raw;
|
|
369
320
|
case a.type === AST_NODE_TYPES.TemplateLiteral && b.type === AST_NODE_TYPES.TemplateLiteral: {
|
|
370
321
|
if (a.quasis.length !== b.quasis.length || a.expressions.length !== b.expressions.length) return false;
|
|
@@ -381,6 +332,7 @@ const isEqual = dual(2, (a, b) => {
|
|
|
381
332
|
}
|
|
382
333
|
case isIdentifier(a) && isIdentifier(b): return a.name === b.name;
|
|
383
334
|
case a.type === AST_NODE_TYPES.PrivateIdentifier && b.type === AST_NODE_TYPES.PrivateIdentifier: return a.name === b.name;
|
|
335
|
+
case a.type === AST_NODE_TYPES.ChainExpression && b.type === AST_NODE_TYPES.ChainExpression: return isEqual(a.expression, b.expression);
|
|
384
336
|
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
337
|
case a.type === AST_NODE_TYPES.CallExpression && b.type === AST_NODE_TYPES.CallExpression: {
|
|
386
338
|
if (a.optional !== b.optional) return false;
|
|
@@ -485,7 +437,11 @@ const EXPRESSION_VISITOR_KEYS = {
|
|
|
485
437
|
[AST_NODE_TYPES.ExpressionStatement]: ["expression"],
|
|
486
438
|
[AST_NODE_TYPES.ForInStatement]: ["left", "right"],
|
|
487
439
|
[AST_NODE_TYPES.ForOfStatement]: ["left", "right"],
|
|
488
|
-
[AST_NODE_TYPES.ForStatement]: [
|
|
440
|
+
[AST_NODE_TYPES.ForStatement]: [
|
|
441
|
+
"init",
|
|
442
|
+
"test",
|
|
443
|
+
"update"
|
|
444
|
+
],
|
|
489
445
|
[AST_NODE_TYPES.IfStatement]: ["test"],
|
|
490
446
|
[AST_NODE_TYPES.ImportExpression]: ["source", "options"],
|
|
491
447
|
[AST_NODE_TYPES.JSXAttribute]: ["name", "value"],
|
|
@@ -530,6 +486,8 @@ const EXPRESSION_VISITOR_KEYS = {
|
|
|
530
486
|
[AST_NODE_TYPES.TSTypeAssertion]: ["expression"],
|
|
531
487
|
[AST_NODE_TYPES.UnaryExpression]: ["argument"],
|
|
532
488
|
[AST_NODE_TYPES.UpdateExpression]: ["argument"],
|
|
489
|
+
[AST_NODE_TYPES.VariableDeclaration]: ["declarations"],
|
|
490
|
+
[AST_NODE_TYPES.VariableDeclarator]: ["init"],
|
|
533
491
|
[AST_NODE_TYPES.WhileStatement]: ["test"],
|
|
534
492
|
[AST_NODE_TYPES.YieldExpression]: ["argument"]
|
|
535
493
|
};
|
|
@@ -600,6 +558,56 @@ function isTerminatingStatement(node) {
|
|
|
600
558
|
default: return false;
|
|
601
559
|
}
|
|
602
560
|
}
|
|
561
|
+
const jumpBarrierVisitorKeys = {
|
|
562
|
+
...visitorKeys,
|
|
563
|
+
ArrowFunctionExpression: [],
|
|
564
|
+
FunctionDeclaration: [],
|
|
565
|
+
FunctionExpression: []
|
|
566
|
+
};
|
|
567
|
+
function hasExitingJump(node, checkContinue) {
|
|
568
|
+
let found = false;
|
|
569
|
+
let loopDepth = 0;
|
|
570
|
+
let loopOrSwitchDepth = 0;
|
|
571
|
+
simpleTraverse(node, {
|
|
572
|
+
enter(child) {
|
|
573
|
+
switch (child.type) {
|
|
574
|
+
case AST_NODE_TYPES.BreakStatement:
|
|
575
|
+
if (child.label != null || loopOrSwitchDepth === 0) found = true;
|
|
576
|
+
break;
|
|
577
|
+
case AST_NODE_TYPES.ContinueStatement:
|
|
578
|
+
if (child.label != null || checkContinue && loopDepth === 0) found = true;
|
|
579
|
+
break;
|
|
580
|
+
case AST_NODE_TYPES.WhileStatement:
|
|
581
|
+
case AST_NODE_TYPES.DoWhileStatement:
|
|
582
|
+
case AST_NODE_TYPES.ForStatement:
|
|
583
|
+
case AST_NODE_TYPES.ForInStatement:
|
|
584
|
+
case AST_NODE_TYPES.ForOfStatement:
|
|
585
|
+
loopDepth += 1;
|
|
586
|
+
loopOrSwitchDepth += 1;
|
|
587
|
+
break;
|
|
588
|
+
case AST_NODE_TYPES.SwitchStatement: loopOrSwitchDepth += 1;
|
|
589
|
+
}
|
|
590
|
+
},
|
|
591
|
+
leave(child) {
|
|
592
|
+
switch (child.type) {
|
|
593
|
+
case AST_NODE_TYPES.WhileStatement:
|
|
594
|
+
case AST_NODE_TYPES.DoWhileStatement:
|
|
595
|
+
case AST_NODE_TYPES.ForStatement:
|
|
596
|
+
case AST_NODE_TYPES.ForInStatement:
|
|
597
|
+
case AST_NODE_TYPES.ForOfStatement:
|
|
598
|
+
loopDepth -= 1;
|
|
599
|
+
loopOrSwitchDepth -= 1;
|
|
600
|
+
break;
|
|
601
|
+
case AST_NODE_TYPES.SwitchStatement: loopOrSwitchDepth -= 1;
|
|
602
|
+
}
|
|
603
|
+
},
|
|
604
|
+
visitorKeys: jumpBarrierVisitorKeys
|
|
605
|
+
});
|
|
606
|
+
return found;
|
|
607
|
+
}
|
|
608
|
+
function isTrueLiteral(node) {
|
|
609
|
+
return node.type === AST_NODE_TYPES.Literal && node.value === true;
|
|
610
|
+
}
|
|
603
611
|
function isUnconditionallyTerminating(node) {
|
|
604
612
|
switch (node.type) {
|
|
605
613
|
case AST_NODE_TYPES.ReturnStatement:
|
|
@@ -609,6 +617,29 @@ function isUnconditionallyTerminating(node) {
|
|
|
609
617
|
return last != null && isUnconditionallyTerminating(last);
|
|
610
618
|
}
|
|
611
619
|
case AST_NODE_TYPES.IfStatement: return node.alternate != null && isUnconditionallyTerminating(node.consequent) && isUnconditionallyTerminating(node.alternate);
|
|
620
|
+
case AST_NODE_TYPES.TryStatement:
|
|
621
|
+
if (node.finalizer != null && isUnconditionallyTerminating(node.finalizer)) return true;
|
|
622
|
+
if (node.handler == null) return isUnconditionallyTerminating(node.block);
|
|
623
|
+
return isUnconditionallyTerminating(node.block) && isUnconditionallyTerminating(node.handler.body);
|
|
624
|
+
case AST_NODE_TYPES.SwitchStatement:
|
|
625
|
+
if (!node.cases.some((switchCase) => switchCase.test == null)) return false;
|
|
626
|
+
return node.cases.every((switchCase) => {
|
|
627
|
+
const last = switchCase.consequent.at(-1);
|
|
628
|
+
if (last == null) return false;
|
|
629
|
+
if (!isUnconditionallyTerminating(last)) return false;
|
|
630
|
+
return switchCase.consequent.every((statement) => !hasExitingJump(statement, true));
|
|
631
|
+
});
|
|
632
|
+
case AST_NODE_TYPES.WhileStatement:
|
|
633
|
+
if (!isTrueLiteral(node.test)) return false;
|
|
634
|
+
if (!isUnconditionallyTerminating(node.body)) return false;
|
|
635
|
+
return !hasExitingJump(node.body, false);
|
|
636
|
+
case AST_NODE_TYPES.DoWhileStatement:
|
|
637
|
+
if (!isUnconditionallyTerminating(node.body)) return false;
|
|
638
|
+
return !hasExitingJump(node.body, true);
|
|
639
|
+
case AST_NODE_TYPES.ForStatement:
|
|
640
|
+
if (node.test != null && !isTrueLiteral(node.test)) return false;
|
|
641
|
+
if (!isUnconditionallyTerminating(node.body)) return false;
|
|
642
|
+
return !hasExitingJump(node.body, false);
|
|
612
643
|
default: return false;
|
|
613
644
|
}
|
|
614
645
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/ast",
|
|
3
|
-
"version": "5.30.0-
|
|
3
|
+
"version": "5.30.0-rc.0",
|
|
4
4
|
"description": "ESLint React's TSESTree AST utility module.",
|
|
5
5
|
"homepage": "https://github.com/Rel1cx/eslint-react",
|
|
6
6
|
"bugs": {
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
"@local/testkit": "0.0.0",
|
|
40
40
|
"@tsconfig/node20": "^20.1.10",
|
|
41
41
|
"@types/node": "^20.19.43",
|
|
42
|
-
"eslint": "^10.
|
|
43
|
-
"tsdown": "^0.23.
|
|
42
|
+
"eslint": "^10.14.0",
|
|
43
|
+
"tsdown": "^0.23.5",
|
|
44
44
|
"typescript": "6.0.3",
|
|
45
|
-
"vitest": "^5.0
|
|
45
|
+
"vitest": "^5.1.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"eslint": "*",
|