@eslint-react/ast 3.0.0-next.7 → 3.0.0-next.70
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 +85 -66
- package/dist/index.js +117 -78
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { unit } from "@eslint-react/eff";
|
|
2
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/types";
|
|
3
2
|
import { TSESTree as TSESTree$1 } from "@typescript-eslint/utils";
|
|
4
3
|
|
|
@@ -34,7 +33,7 @@ type TSESTreeProperty = TSESTree$1.PropertyDefinition | TSESTree$1.TSIndexSignat
|
|
|
34
33
|
/**
|
|
35
34
|
* Represents all JSX-related nodes in TSESTree
|
|
36
35
|
*/
|
|
37
|
-
type TSESTreeJSX = TSESTree$1.JSXAttribute | TSESTree$1.JSXChild | TSESTree$1.JSXClosingElement | TSESTree$1.JSXClosingFragment | TSESTree$1.
|
|
36
|
+
type TSESTreeJSX = TSESTree$1.JSXAttribute | TSESTree$1.JSXChild | TSESTree$1.JSXClosingElement | TSESTree$1.JSXClosingFragment | TSESTree$1.JSXEmptyExpression | TSESTree$1.JSXIdentifierToken | TSESTree$1.JSXOpeningElement | TSESTree$1.JSXOpeningFragment | TSESTree$1.JSXSpreadAttribute | TSESTree$1.JSXTagNameExpression | TSESTree$1.JSXTextToken;
|
|
38
37
|
/**
|
|
39
38
|
* Represents JSX attribute-like nodes (attributes and spread attributes)
|
|
40
39
|
*/
|
|
@@ -73,37 +72,9 @@ type TSESTreeDirectiveLike = TSESTree$1.ExpressionStatement & {
|
|
|
73
72
|
/**
|
|
74
73
|
* Get the class identifier of a class node
|
|
75
74
|
* @param node The class node to get the identifier from
|
|
76
|
-
* @returns The class identifier or
|
|
75
|
+
* @returns The class identifier or null if not found
|
|
77
76
|
*/
|
|
78
|
-
declare function getClassId(node: TSESTreeClass): TSESTree.BindingName |
|
|
79
|
-
//#endregion
|
|
80
|
-
//#region src/directive-helper.d.ts
|
|
81
|
-
/**
|
|
82
|
-
* Get all directive expression statements from the top of a program AST node
|
|
83
|
-
* @param node The program AST node
|
|
84
|
-
* @returns The array of directive string literals (e.g., "use strict")
|
|
85
|
-
*/
|
|
86
|
-
declare function getFileDirectives(node: TSESTree.Program): TSESTreeDirective[];
|
|
87
|
-
/**
|
|
88
|
-
* Get all directive expression statements from the top of a function AST node
|
|
89
|
-
* @param node The function AST node
|
|
90
|
-
* @returns The array of directive string literals (e.g., "use memo", "use no memo")
|
|
91
|
-
*/
|
|
92
|
-
declare function getFunctionDirectives(node: TSESTreeFunction): TSESTreeDirective[];
|
|
93
|
-
/**
|
|
94
|
-
* Check if a directive with the given name exists in the file or function directives
|
|
95
|
-
* @param node The program or function AST node
|
|
96
|
-
* @param name The directive name to check (e.g., "use strict", "use memo", "use no memo")
|
|
97
|
-
* @returns True if the directive exists, false otherwise
|
|
98
|
-
*/
|
|
99
|
-
declare function isDirectiveInFile(node: TSESTree.Program, name: string): boolean;
|
|
100
|
-
/**
|
|
101
|
-
* Check if a directive with the given name exists in the function directives
|
|
102
|
-
* @param node The function AST node
|
|
103
|
-
* @param name The directive name to check (e.g., "use memo", "use no memo")
|
|
104
|
-
* @returns True if the directive exists, false otherwise
|
|
105
|
-
*/
|
|
106
|
-
declare function isDirectiveInFunction(node: TSESTreeFunction, name: string): boolean;
|
|
77
|
+
declare function getClassId(node: TSESTreeClass): TSESTree.BindingName | null;
|
|
107
78
|
//#endregion
|
|
108
79
|
//#region src/directive-is.d.ts
|
|
109
80
|
/**
|
|
@@ -136,15 +107,6 @@ declare function isDirectiveKind(kind: string): kind is DirectiveKind;
|
|
|
136
107
|
*/
|
|
137
108
|
declare function isDirectiveName(name: string): boolean;
|
|
138
109
|
//#endregion
|
|
139
|
-
//#region src/expression-base.d.ts
|
|
140
|
-
/**
|
|
141
|
-
* Unwraps any type expressions to get the underlying JavaScript expression node.
|
|
142
|
-
* Recursively processes nodes until a non-type expression is found.
|
|
143
|
-
* @param node The AST node to unwrap
|
|
144
|
-
* @returns The underlying JavaScript expression node
|
|
145
|
-
*/
|
|
146
|
-
declare function getUnderlyingExpression(node: TSESTree.Node): Exclude<TSESTree.Node, TSESTreeTypeExpression>;
|
|
147
|
-
//#endregion
|
|
148
110
|
//#region src/expression-is.d.ts
|
|
149
111
|
/**
|
|
150
112
|
* Check if the given expression is a 'this' expression
|
|
@@ -190,6 +152,36 @@ declare const getNestedNewExpressions: (node: TSESTree.Node) => TSESTree.NewExpr
|
|
|
190
152
|
*/
|
|
191
153
|
declare const getNestedCallExpressions: (node: TSESTree.Node) => TSESTree.CallExpression[];
|
|
192
154
|
//#endregion
|
|
155
|
+
//#region src/file-directive.d.ts
|
|
156
|
+
/**
|
|
157
|
+
* Get all directive expression statements from the top of a program AST node
|
|
158
|
+
* @param node The program AST node
|
|
159
|
+
* @returns The array of directive string literals (e.g., "use strict")
|
|
160
|
+
*/
|
|
161
|
+
declare function getFileDirectives(node: TSESTree.Program): TSESTreeDirective[];
|
|
162
|
+
/**
|
|
163
|
+
* Check if a directive with the given name exists in the file or function directives
|
|
164
|
+
* @param node The program or function AST node
|
|
165
|
+
* @param name The directive name to check (e.g., "use strict", "use memo", "use no memo")
|
|
166
|
+
* @returns True if the directive exists, false otherwise
|
|
167
|
+
*/
|
|
168
|
+
declare function isDirectiveInFile(node: TSESTree.Program, name: string): boolean;
|
|
169
|
+
//#endregion
|
|
170
|
+
//#region src/function-directive.d.ts
|
|
171
|
+
/**
|
|
172
|
+
* Get all directive expression statements from the top of a function AST node
|
|
173
|
+
* @param node The function AST node
|
|
174
|
+
* @returns The array of directive string literals (e.g., "use memo", "use no memo")
|
|
175
|
+
*/
|
|
176
|
+
declare function getFunctionDirectives(node: TSESTreeFunction): TSESTreeDirective[];
|
|
177
|
+
/**
|
|
178
|
+
* Check if a directive with the given name exists in the function directives
|
|
179
|
+
* @param node The function AST node
|
|
180
|
+
* @param name The directive name to check (e.g., "use memo", "use no memo")
|
|
181
|
+
* @returns True if the directive exists, false otherwise
|
|
182
|
+
*/
|
|
183
|
+
declare function isDirectiveInFunction(node: TSESTreeFunction, name: string): boolean;
|
|
184
|
+
//#endregion
|
|
193
185
|
//#region src/function-id.d.ts
|
|
194
186
|
/**
|
|
195
187
|
* Gets the static name of a function AST node. For function declarations it is
|
|
@@ -198,7 +190,7 @@ declare const getNestedCallExpressions: (node: TSESTree.Node) => TSESTree.CallEx
|
|
|
198
190
|
* where JS gives anonymous function expressions names. We roughly detect the
|
|
199
191
|
* same AST nodes with some exceptions to better fit our use case.
|
|
200
192
|
*/
|
|
201
|
-
declare function getFunctionId(node: TSESTree.Expression | TSESTreeFunction): TSESTree.ArrayExpression | TSESTree.ArrayPattern | TSESTree.ArrowFunctionExpression | TSESTree.AssignmentExpression | TSESTree.AwaitExpression | TSESTree.PrivateInExpression | TSESTree.SymmetricBinaryExpression | TSESTree.CallExpression | TSESTree.ChainExpression | TSESTree.ClassExpression | TSESTree.ConditionalExpression | TSESTree.FunctionExpression | TSESTree.Identifier | TSESTree.ImportExpression | TSESTree.JSXElement | TSESTree.JSXFragment | TSESTree.BigIntLiteral | TSESTree.BooleanLiteral | TSESTree.NullLiteral | TSESTree.NumberLiteral | TSESTree.RegExpLiteral | TSESTree.StringLiteral | TSESTree.LogicalExpression | TSESTree.MemberExpressionComputedName | TSESTree.MemberExpressionNonComputedName | TSESTree.MetaProperty | TSESTree.NewExpression | TSESTree.ObjectExpression | TSESTree.ObjectPattern | TSESTree.PrivateIdentifier | TSESTree.SequenceExpression | TSESTree.Super | TSESTree.TaggedTemplateExpression | TSESTree.TemplateLiteral | TSESTree.ThisExpression | TSESTree.TSAsExpression | TSESTree.TSInstantiationExpression | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion | TSESTree.UnaryExpressionBitwiseNot | TSESTree.UnaryExpressionDelete | TSESTree.UnaryExpressionMinus | TSESTree.UnaryExpressionNot | TSESTree.UnaryExpressionPlus | TSESTree.UnaryExpressionTypeof | TSESTree.UnaryExpressionVoid | TSESTree.UpdateExpression | TSESTree.YieldExpression |
|
|
193
|
+
declare function getFunctionId(node: TSESTree.Expression | TSESTreeFunction): TSESTree.ArrayExpression | TSESTree.ArrayPattern | TSESTree.ArrowFunctionExpression | TSESTree.AssignmentExpression | TSESTree.AwaitExpression | TSESTree.PrivateInExpression | TSESTree.SymmetricBinaryExpression | TSESTree.CallExpression | TSESTree.ChainExpression | TSESTree.ClassExpression | TSESTree.ConditionalExpression | TSESTree.FunctionExpression | TSESTree.Identifier | TSESTree.ImportExpression | TSESTree.JSXElement | TSESTree.JSXFragment | TSESTree.BigIntLiteral | TSESTree.BooleanLiteral | TSESTree.NullLiteral | TSESTree.NumberLiteral | TSESTree.RegExpLiteral | TSESTree.StringLiteral | TSESTree.LogicalExpression | TSESTree.MemberExpressionComputedName | TSESTree.MemberExpressionNonComputedName | TSESTree.MetaProperty | TSESTree.NewExpression | TSESTree.ObjectExpression | TSESTree.ObjectPattern | TSESTree.PrivateIdentifier | TSESTree.SequenceExpression | TSESTree.Super | TSESTree.TaggedTemplateExpression | TSESTree.TemplateLiteral | TSESTree.ThisExpression | TSESTree.TSAsExpression | TSESTree.TSInstantiationExpression | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion | TSESTree.UnaryExpressionBitwiseNot | TSESTree.UnaryExpressionDelete | TSESTree.UnaryExpressionMinus | TSESTree.UnaryExpressionNot | TSESTree.UnaryExpressionPlus | TSESTree.UnaryExpressionTypeof | TSESTree.UnaryExpressionVoid | TSESTree.UpdateExpression | TSESTree.YieldExpression | null;
|
|
202
194
|
/**
|
|
203
195
|
* Type representing the return type of getFunctionId
|
|
204
196
|
*/
|
|
@@ -215,9 +207,9 @@ type FunctionInitPath = readonly [TSESTree.FunctionDeclaration] | readonly [TSES
|
|
|
215
207
|
* Determine what kind of component declaration pattern the function belongs to.
|
|
216
208
|
*
|
|
217
209
|
* @param node The function node to analyze
|
|
218
|
-
* @returns The function initialization path or
|
|
210
|
+
* @returns The function initialization path or null if not identifiable
|
|
219
211
|
*/
|
|
220
|
-
declare function getFunctionInitPath(node: TSESTreeFunction):
|
|
212
|
+
declare function getFunctionInitPath(node: TSESTreeFunction): null | FunctionInitPath;
|
|
221
213
|
/**
|
|
222
214
|
* Check if a specific function call exists in the function initialization path.
|
|
223
215
|
* Useful for detecting HOCs like React.memo, React.forwardRef, etc.
|
|
@@ -249,7 +241,7 @@ declare function isFunctionImmediatelyInvoked(node: TSESTreeFunction): boolean;
|
|
|
249
241
|
* @param name The name to check
|
|
250
242
|
* @returns True if the node is an identifier, false otherwise
|
|
251
243
|
*/
|
|
252
|
-
declare function isIdentifier(node: TSESTree.Node | null
|
|
244
|
+
declare function isIdentifier(node: TSESTree.Node | null, name?: string): node is TSESTree.Identifier;
|
|
253
245
|
//#endregion
|
|
254
246
|
//#region src/identifier-name.d.ts
|
|
255
247
|
/**
|
|
@@ -259,6 +251,15 @@ declare function isIdentifier(node: TSESTree.Node | null | unit, name?: string):
|
|
|
259
251
|
*/
|
|
260
252
|
declare function isIdentifierName(name: string): boolean;
|
|
261
253
|
//#endregion
|
|
254
|
+
//#region src/identifier-traverse.d.ts
|
|
255
|
+
/**
|
|
256
|
+
* Get the root identifier of a (possibly nested) member expression.
|
|
257
|
+
* For `a.b.c`, returns the `a` Identifier node.
|
|
258
|
+
* @param node The expression to analyze
|
|
259
|
+
* @returns The root Identifier node, or null if it cannot be determined (e.g. non-identifier root)
|
|
260
|
+
*/
|
|
261
|
+
declare function getRootIdentifier(node: TSESTree.Expression | TSESTree.PrivateIdentifier): TSESTree.Identifier | null;
|
|
262
|
+
//#endregion
|
|
262
263
|
//#region src/literal-is.d.ts
|
|
263
264
|
/**
|
|
264
265
|
* Check if a node is a literal value
|
|
@@ -272,7 +273,7 @@ declare function isLiteral(node: TSESTree.Node, type: "number"): node is TSESTre
|
|
|
272
273
|
declare function isLiteral(node: TSESTree.Node, type: "regexp"): node is TSESTree.RegExpLiteral;
|
|
273
274
|
declare function isLiteral(node: TSESTree.Node, type: "string"): node is TSESTree.StringLiteral;
|
|
274
275
|
//#endregion
|
|
275
|
-
//#region src/node-
|
|
276
|
+
//#region src/node-equality.d.ts
|
|
276
277
|
/**
|
|
277
278
|
* Check if two nodes are equal
|
|
278
279
|
* @param a node to compare
|
|
@@ -442,24 +443,33 @@ declare const SEL_DISPLAY_NAME_ASSIGNMENT_EXPRESSION: string;
|
|
|
442
443
|
* Find the parent node that satisfies the test function
|
|
443
444
|
* @param node The AST node
|
|
444
445
|
* @param test The test function
|
|
445
|
-
* @returns The parent node that satisfies the test function or `
|
|
446
|
+
* @returns The parent node that satisfies the test function or `null` if not found
|
|
446
447
|
*/
|
|
447
|
-
declare function findParentNode<A extends TSESTree.Node>(node: TSESTree.Node |
|
|
448
|
+
declare function findParentNode<A extends TSESTree.Node>(node: TSESTree.Node | null, test: (n: TSESTree.Node) => n is A): A | null;
|
|
448
449
|
/**
|
|
449
|
-
* Find the parent node that satisfies the test function or `
|
|
450
|
+
* Find the parent node that satisfies the test function or `null` if not found
|
|
450
451
|
* @param node The AST node
|
|
451
452
|
* @param test The test function
|
|
452
453
|
* @returns The parent node that satisfies the test function
|
|
453
454
|
*/
|
|
454
|
-
declare function findParentNode(node: TSESTree.Node |
|
|
455
|
+
declare function findParentNode(node: TSESTree.Node | null, test: (node: TSESTree.Node) => boolean): TSESTree.Node | null;
|
|
455
456
|
//#endregion
|
|
456
|
-
//#region src/
|
|
457
|
+
//#region src/node-unwrap.d.ts
|
|
458
|
+
/**
|
|
459
|
+
* Unwraps any type expressions to get the underlying JavaScript expression node.
|
|
460
|
+
* Recursively processes nodes until a non-type expression is found.
|
|
461
|
+
* @param node The AST node to unwrap
|
|
462
|
+
* @returns The underlying JavaScript expression node
|
|
463
|
+
*/
|
|
464
|
+
declare function getUnderlyingExpression(node: TSESTree.Node): Exclude<TSESTree.Node, TSESTreeTypeExpression>;
|
|
465
|
+
//#endregion
|
|
466
|
+
//#region src/pattern-process-env-node-env.d.ts
|
|
457
467
|
/**
|
|
458
468
|
* Check if the given node is a member expression that accesses `process.env.NODE_ENV`
|
|
459
469
|
* @param node The AST node
|
|
460
470
|
* @returns True if the node is a member expression that accesses `process.env.NODE_ENV`, false otherwise
|
|
461
471
|
*/
|
|
462
|
-
declare function isProcessEnvNodeEnv(node: TSESTree.Node | null
|
|
472
|
+
declare function isProcessEnvNodeEnv(node: TSESTree.Node | null): node is TSESTree.MemberExpression;
|
|
463
473
|
/**
|
|
464
474
|
* Check if the given node is a binary expression that compares `process.env.NODE_ENV` with a string literal.
|
|
465
475
|
* @param node The AST node
|
|
@@ -467,31 +477,40 @@ declare function isProcessEnvNodeEnv(node: TSESTree.Node | null | unit): node is
|
|
|
467
477
|
* @param value The string literal value to compare against
|
|
468
478
|
* @returns True if the node is a binary expression that compares `process.env.NODE_ENV` with the specified value, false otherwise
|
|
469
479
|
*/
|
|
470
|
-
declare function isProcessEnvNodeEnvCompare(node: TSESTree.Node | null
|
|
471
|
-
//#endregion
|
|
472
|
-
//#region src/property-name.d.ts
|
|
473
|
-
/**
|
|
474
|
-
* Get the name of a property from a node
|
|
475
|
-
* Handles identifiers, private identifiers, literals, and template literals
|
|
476
|
-
* @param node The node to get the property name from
|
|
477
|
-
* @returns The property name or unit if not determinable
|
|
478
|
-
*/
|
|
479
|
-
declare function getPropertyName(node: TSESTree.Node): string | unit;
|
|
480
|
+
declare function isProcessEnvNodeEnvCompare(node: TSESTree.Node | null, operator: "===" | "!==", value: "development" | "production"): node is TSESTree.BinaryExpression;
|
|
480
481
|
//#endregion
|
|
481
|
-
//#region src/vitest-mock.d.ts
|
|
482
|
+
//#region src/pattern-vitest-mock.d.ts
|
|
482
483
|
/**
|
|
483
484
|
* Check if the given node is a `vi.mock`.
|
|
484
485
|
* @param node The node to check
|
|
485
486
|
* @returns `true` if the node is a `vi.mock`, otherwise `false`.
|
|
486
487
|
* @internal
|
|
487
488
|
*/
|
|
488
|
-
declare function isViMock(node: TSESTree.Node | null
|
|
489
|
+
declare function isViMock(node: TSESTree.Node | null): node is TSESTree.MemberExpression;
|
|
489
490
|
/**
|
|
490
491
|
* Check if the given node is a `vi.mock` callback.
|
|
491
492
|
* @param node The node to check
|
|
492
493
|
* @returns `true` if the node is a `vi.mock` callback, otherwise `false`.
|
|
493
494
|
* @internal
|
|
494
495
|
*/
|
|
495
|
-
declare function isViMockCallback(node: TSESTree.Node | null
|
|
496
|
+
declare function isViMockCallback(node: TSESTree.Node | null): boolean;
|
|
497
|
+
//#endregion
|
|
498
|
+
//#region src/property-name.d.ts
|
|
499
|
+
/**
|
|
500
|
+
* Get the name of a property from a node
|
|
501
|
+
* Handles identifiers, private identifiers, literals, and template literals
|
|
502
|
+
* @param node The node to get the property name from
|
|
503
|
+
* @returns The property name or null if not determinable
|
|
504
|
+
*/
|
|
505
|
+
declare function getPropertyName(node: TSESTree.Node): string | null;
|
|
506
|
+
//#endregion
|
|
507
|
+
//#region src/property-traverse.d.ts
|
|
508
|
+
/**
|
|
509
|
+
* Recursively traverses an object expression's properties to find a property with the specified name
|
|
510
|
+
* @param properties The properties of the object expression to traverse
|
|
511
|
+
* @param name The name of the property to find
|
|
512
|
+
* @returns The matching property node, or null if not found
|
|
513
|
+
*/
|
|
514
|
+
declare function findProperty(properties: TSESTree.ObjectLiteralElement[], name: string): TSESTree.Property | null;
|
|
496
515
|
//#endregion
|
|
497
|
-
export { DirectiveKind, DisplayNameAssignmentExpression, FunctionID, FunctionInitPath, ImplicitReturnArrowFunctionExpression, ObjectDestructuringVariableDeclarator, SEL_DISPLAY_NAME_ASSIGNMENT_EXPRESSION, SEL_IMPLICIT_RETURN_ARROW_FUNCTION_EXPRESSION, SEL_OBJECT_DESTRUCTURING_VARIABLE_DECLARATOR, TSESTreeArrayTupleType, TSESTreeClass, TSESTreeDestructuringPattern, TSESTreeDirective, TSESTreeDirectiveLike, TSESTreeFunction, TSESTreeFunctionType, TSESTreeJSX, TSESTreeJSXAttributeLike, TSESTreeLoop, TSESTreeMethodOrProperty, TSESTreeProperty, TSESTreeTypeAssertionExpression, TSESTreeTypeDeclaration, TSESTreeTypeExpression, findParentNode, getClassId, getFileDirectives, getFullyQualifiedName, getFunctionDirectives, getFunctionId, getFunctionInitPath, getHumanReadableKind, getNestedCallExpressions, getNestedExpressionsOfType, getNestedIdentifiers, getNestedNewExpressions, getNestedReturnStatements, getPropertyName, getUnderlyingExpression, hasCallInFunctionInitPath, is, isClass, isConditional, isControlFlow, isDirective, isDirectiveInFile, isDirectiveInFunction, isDirectiveKind, isDirectiveLike, isDirectiveName, isFunction, isFunctionEmpty, isFunctionImmediatelyInvoked, isFunctionType, isIdentifier, isIdentifierName, isJSX, isJSXElement, isJSXFragment, isJSXTagNameExpression, isLineBreak, isLiteral, isLoop, isMethodOrProperty, isMultiLine, isNodeEqual, isOneOf, isProcessEnvNodeEnv, isProcessEnvNodeEnvCompare, isProperty, isThisExpressionLoose, isTypeAssertionExpression, isTypeExpression, isViMock, isViMockCallback };
|
|
516
|
+
export { DirectiveKind, DisplayNameAssignmentExpression, FunctionID, FunctionInitPath, ImplicitReturnArrowFunctionExpression, ObjectDestructuringVariableDeclarator, SEL_DISPLAY_NAME_ASSIGNMENT_EXPRESSION, SEL_IMPLICIT_RETURN_ARROW_FUNCTION_EXPRESSION, SEL_OBJECT_DESTRUCTURING_VARIABLE_DECLARATOR, TSESTreeArrayTupleType, TSESTreeClass, TSESTreeDestructuringPattern, TSESTreeDirective, TSESTreeDirectiveLike, TSESTreeFunction, TSESTreeFunctionType, TSESTreeJSX, TSESTreeJSXAttributeLike, TSESTreeLoop, TSESTreeMethodOrProperty, TSESTreeProperty, TSESTreeTypeAssertionExpression, TSESTreeTypeDeclaration, TSESTreeTypeExpression, findParentNode, findProperty, getClassId, getFileDirectives, getFullyQualifiedName, getFunctionDirectives, getFunctionId, getFunctionInitPath, getHumanReadableKind, getNestedCallExpressions, getNestedExpressionsOfType, getNestedIdentifiers, getNestedNewExpressions, getNestedReturnStatements, getPropertyName, getRootIdentifier, getUnderlyingExpression, hasCallInFunctionInitPath, is, isClass, isConditional, isControlFlow, isDirective, isDirectiveInFile, isDirectiveInFunction, isDirectiveKind, isDirectiveLike, isDirectiveName, isFunction, isFunctionEmpty, isFunctionImmediatelyInvoked, isFunctionType, isIdentifier, isIdentifierName, isJSX, isJSXElement, isJSXFragment, isJSXTagNameExpression, isLineBreak, isLiteral, isLoop, isMethodOrProperty, isMultiLine, isNodeEqual, isOneOf, isProcessEnvNodeEnv, isProcessEnvNodeEnvCompare, isProperty, isThisExpressionLoose, isTypeAssertionExpression, isTypeExpression, isViMock, isViMockCallback };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/types";
|
|
2
|
+
import { dual, or } from "@eslint-react/eff";
|
|
3
3
|
import { ASTUtils } from "@typescript-eslint/utils";
|
|
4
4
|
import { simpleTraverse } from "@typescript-eslint/typescript-estree";
|
|
5
5
|
import { delimiterCase, replace, toLowerCase } from "string-ts";
|
|
@@ -8,12 +8,12 @@ import { delimiterCase, replace, toLowerCase } from "string-ts";
|
|
|
8
8
|
/**
|
|
9
9
|
* Get the class identifier of a class node
|
|
10
10
|
* @param node The class node to get the identifier from
|
|
11
|
-
* @returns The class identifier or
|
|
11
|
+
* @returns The class identifier or null if not found
|
|
12
12
|
*/
|
|
13
13
|
function getClassId(node) {
|
|
14
14
|
if (node.id != null) return node.id;
|
|
15
15
|
if (node.parent.type === AST_NODE_TYPES.VariableDeclarator) return node.parent.id;
|
|
16
|
-
return
|
|
16
|
+
return null;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
//#endregion
|
|
@@ -60,54 +60,6 @@ function isDirectiveLike(node) {
|
|
|
60
60
|
return node.type === AST_NODE_TYPES.ExpressionStatement && isLiteral(node.expression, "string") && isDirectiveName(node.expression.value);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
//#endregion
|
|
64
|
-
//#region src/directive-helper.ts
|
|
65
|
-
/**
|
|
66
|
-
* Get all directive expression statements from the top of a program AST node
|
|
67
|
-
* @param node The program AST node
|
|
68
|
-
* @returns The array of directive string literals (e.g., "use strict")
|
|
69
|
-
*/
|
|
70
|
-
function getFileDirectives(node) {
|
|
71
|
-
const directives = [];
|
|
72
|
-
for (const stmt of node.body) {
|
|
73
|
-
if (!isDirective(stmt)) continue;
|
|
74
|
-
directives.push(stmt);
|
|
75
|
-
}
|
|
76
|
-
return directives;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Get all directive expression statements from the top of a function AST node
|
|
80
|
-
* @param node The function AST node
|
|
81
|
-
* @returns The array of directive string literals (e.g., "use memo", "use no memo")
|
|
82
|
-
*/
|
|
83
|
-
function getFunctionDirectives(node) {
|
|
84
|
-
const directives = [];
|
|
85
|
-
if (node.body.type !== AST_NODE_TYPES.BlockStatement) return directives;
|
|
86
|
-
for (const stmt of node.body.body) {
|
|
87
|
-
if (!isDirective(stmt)) continue;
|
|
88
|
-
directives.push(stmt);
|
|
89
|
-
}
|
|
90
|
-
return directives;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Check if a directive with the given name exists in the file or function directives
|
|
94
|
-
* @param node The program or function AST node
|
|
95
|
-
* @param name The directive name to check (e.g., "use strict", "use memo", "use no memo")
|
|
96
|
-
* @returns True if the directive exists, false otherwise
|
|
97
|
-
*/
|
|
98
|
-
function isDirectiveInFile(node, name) {
|
|
99
|
-
return getFileDirectives(node).some((d) => d.directive === name);
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Check if a directive with the given name exists in the function directives
|
|
103
|
-
* @param node The function AST node
|
|
104
|
-
* @param name The directive name to check (e.g., "use memo", "use no memo")
|
|
105
|
-
* @returns True if the directive exists, false otherwise
|
|
106
|
-
*/
|
|
107
|
-
function isDirectiveInFunction(node, name) {
|
|
108
|
-
return getFunctionDirectives(node).some((d) => d.directive === name);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
63
|
//#endregion
|
|
112
64
|
//#region src/directive-kind.ts
|
|
113
65
|
/**
|
|
@@ -275,7 +227,7 @@ const isTypeAssertionExpression = isOneOf([
|
|
|
275
227
|
]);
|
|
276
228
|
|
|
277
229
|
//#endregion
|
|
278
|
-
//#region src/
|
|
230
|
+
//#region src/node-unwrap.ts
|
|
279
231
|
/**
|
|
280
232
|
* Unwraps any type expressions to get the underlying JavaScript expression node.
|
|
281
233
|
* Recursively processes nodes until a non-type expression is found.
|
|
@@ -303,13 +255,13 @@ function isThisExpressionLoose(node) {
|
|
|
303
255
|
//#endregion
|
|
304
256
|
//#region src/node-traverse.ts
|
|
305
257
|
function findParentNode(node, test) {
|
|
306
|
-
if (node == null) return
|
|
258
|
+
if (node == null) return null;
|
|
307
259
|
let parent = node.parent;
|
|
308
260
|
while (parent != null && parent.type !== AST_NODE_TYPES.Program) {
|
|
309
261
|
if (test(parent)) return parent;
|
|
310
262
|
parent = parent.parent;
|
|
311
263
|
}
|
|
312
|
-
return
|
|
264
|
+
return null;
|
|
313
265
|
}
|
|
314
266
|
|
|
315
267
|
//#endregion
|
|
@@ -495,6 +447,57 @@ const getNestedNewExpressions = getNestedExpressionsOfType(AST_NODE_TYPES.NewExp
|
|
|
495
447
|
*/
|
|
496
448
|
const getNestedCallExpressions = getNestedExpressionsOfType(AST_NODE_TYPES.CallExpression);
|
|
497
449
|
|
|
450
|
+
//#endregion
|
|
451
|
+
//#region src/file-directive.ts
|
|
452
|
+
/**
|
|
453
|
+
* Get all directive expression statements from the top of a program AST node
|
|
454
|
+
* @param node The program AST node
|
|
455
|
+
* @returns The array of directive string literals (e.g., "use strict")
|
|
456
|
+
*/
|
|
457
|
+
function getFileDirectives(node) {
|
|
458
|
+
const directives = [];
|
|
459
|
+
for (const stmt of node.body) {
|
|
460
|
+
if (!isDirective(stmt)) continue;
|
|
461
|
+
directives.push(stmt);
|
|
462
|
+
}
|
|
463
|
+
return directives;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Check if a directive with the given name exists in the file or function directives
|
|
467
|
+
* @param node The program or function AST node
|
|
468
|
+
* @param name The directive name to check (e.g., "use strict", "use memo", "use no memo")
|
|
469
|
+
* @returns True if the directive exists, false otherwise
|
|
470
|
+
*/
|
|
471
|
+
function isDirectiveInFile(node, name) {
|
|
472
|
+
return getFileDirectives(node).some((d) => d.directive === name);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
//#endregion
|
|
476
|
+
//#region src/function-directive.ts
|
|
477
|
+
/**
|
|
478
|
+
* Get all directive expression statements from the top of a function AST node
|
|
479
|
+
* @param node The function AST node
|
|
480
|
+
* @returns The array of directive string literals (e.g., "use memo", "use no memo")
|
|
481
|
+
*/
|
|
482
|
+
function getFunctionDirectives(node) {
|
|
483
|
+
const directives = [];
|
|
484
|
+
if (node.body.type !== AST_NODE_TYPES.BlockStatement) return directives;
|
|
485
|
+
for (const stmt of node.body.body) {
|
|
486
|
+
if (!isDirective(stmt)) continue;
|
|
487
|
+
directives.push(stmt);
|
|
488
|
+
}
|
|
489
|
+
return directives;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Check if a directive with the given name exists in the function directives
|
|
493
|
+
* @param node The function AST node
|
|
494
|
+
* @param name The directive name to check (e.g., "use memo", "use no memo")
|
|
495
|
+
* @returns True if the directive exists, false otherwise
|
|
496
|
+
*/
|
|
497
|
+
function isDirectiveInFunction(node, name) {
|
|
498
|
+
return getFunctionDirectives(node).some((d) => d.directive === name);
|
|
499
|
+
}
|
|
500
|
+
|
|
498
501
|
//#endregion
|
|
499
502
|
//#region src/function-id.ts
|
|
500
503
|
/**
|
|
@@ -515,7 +518,7 @@ function getFunctionId(node) {
|
|
|
515
518
|
case node.parent.type === AST_NODE_TYPES.ConditionalExpression: return getFunctionId(node.parent);
|
|
516
519
|
case isTypeAssertionExpression(node.parent): return getFunctionId(node.parent);
|
|
517
520
|
}
|
|
518
|
-
return
|
|
521
|
+
return null;
|
|
519
522
|
}
|
|
520
523
|
|
|
521
524
|
//#endregion
|
|
@@ -525,7 +528,7 @@ function getFunctionId(node) {
|
|
|
525
528
|
* Determine what kind of component declaration pattern the function belongs to.
|
|
526
529
|
*
|
|
527
530
|
* @param node The function node to analyze
|
|
528
|
-
* @returns The function initialization path or
|
|
531
|
+
* @returns The function initialization path or null if not identifiable
|
|
529
532
|
*/
|
|
530
533
|
function getFunctionInitPath(node) {
|
|
531
534
|
if (node.type === AST_NODE_TYPES.FunctionDeclaration) return [node];
|
|
@@ -569,7 +572,7 @@ function getFunctionInitPath(node) {
|
|
|
569
572
|
node
|
|
570
573
|
];
|
|
571
574
|
}
|
|
572
|
-
return
|
|
575
|
+
return null;
|
|
573
576
|
}
|
|
574
577
|
/**
|
|
575
578
|
* Check if a specific function call exists in the function initialization path.
|
|
@@ -632,7 +635,24 @@ function isIdentifierName(name) {
|
|
|
632
635
|
}
|
|
633
636
|
|
|
634
637
|
//#endregion
|
|
635
|
-
//#region src/
|
|
638
|
+
//#region src/identifier-traverse.ts
|
|
639
|
+
/**
|
|
640
|
+
* Get the root identifier of a (possibly nested) member expression.
|
|
641
|
+
* For `a.b.c`, returns the `a` Identifier node.
|
|
642
|
+
* @param node The expression to analyze
|
|
643
|
+
* @returns The root Identifier node, or null if it cannot be determined (e.g. non-identifier root)
|
|
644
|
+
*/
|
|
645
|
+
function getRootIdentifier(node) {
|
|
646
|
+
const expr = getUnderlyingExpression(node);
|
|
647
|
+
switch (expr.type) {
|
|
648
|
+
case AST_NODE_TYPES.Identifier: return expr;
|
|
649
|
+
case AST_NODE_TYPES.MemberExpression: return getRootIdentifier(expr.object);
|
|
650
|
+
default: return null;
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
//#endregion
|
|
655
|
+
//#region src/node-equality.ts
|
|
636
656
|
/**
|
|
637
657
|
* Check if two nodes are equal
|
|
638
658
|
* @param a node to compare
|
|
@@ -734,7 +754,7 @@ const SEL_DISPLAY_NAME_ASSIGNMENT_EXPRESSION = [
|
|
|
734
754
|
].join("");
|
|
735
755
|
|
|
736
756
|
//#endregion
|
|
737
|
-
//#region src/process-env-node-env.ts
|
|
757
|
+
//#region src/pattern-process-env-node-env.ts
|
|
738
758
|
/**
|
|
739
759
|
* Check if the given node is a member expression that accesses `process.env.NODE_ENV`
|
|
740
760
|
* @param node The AST node
|
|
@@ -760,23 +780,7 @@ function isProcessEnvNodeEnvCompare(node, operator, value) {
|
|
|
760
780
|
}
|
|
761
781
|
|
|
762
782
|
//#endregion
|
|
763
|
-
//#region src/
|
|
764
|
-
/**
|
|
765
|
-
* Get the name of a property from a node
|
|
766
|
-
* Handles identifiers, private identifiers, literals, and template literals
|
|
767
|
-
* @param node The node to get the property name from
|
|
768
|
-
* @returns The property name or unit if not determinable
|
|
769
|
-
*/
|
|
770
|
-
function getPropertyName(node) {
|
|
771
|
-
if (isTypeExpression(node)) return getPropertyName(getUnderlyingExpression(node));
|
|
772
|
-
if (node.type === AST_NODE_TYPES.Identifier || node.type === AST_NODE_TYPES.PrivateIdentifier) return node.name;
|
|
773
|
-
if (node.type === AST_NODE_TYPES.Literal) return String(node.value);
|
|
774
|
-
if (node.type === AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0) return node.quasis[0]?.value.raw;
|
|
775
|
-
return unit;
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
//#endregion
|
|
779
|
-
//#region src/vitest-mock.ts
|
|
783
|
+
//#region src/pattern-vitest-mock.ts
|
|
780
784
|
/**
|
|
781
785
|
* Check if the given node is a `vi.mock`.
|
|
782
786
|
* @param node The node to check
|
|
@@ -797,4 +801,39 @@ function isViMockCallback(node) {
|
|
|
797
801
|
}
|
|
798
802
|
|
|
799
803
|
//#endregion
|
|
800
|
-
|
|
804
|
+
//#region src/property-name.ts
|
|
805
|
+
/**
|
|
806
|
+
* Get the name of a property from a node
|
|
807
|
+
* Handles identifiers, private identifiers, literals, and template literals
|
|
808
|
+
* @param node The node to get the property name from
|
|
809
|
+
* @returns The property name or null if not determinable
|
|
810
|
+
*/
|
|
811
|
+
function getPropertyName(node) {
|
|
812
|
+
if (isTypeExpression(node)) return getPropertyName(getUnderlyingExpression(node));
|
|
813
|
+
if (node.type === AST_NODE_TYPES.Identifier || node.type === AST_NODE_TYPES.PrivateIdentifier) return node.name;
|
|
814
|
+
if (node.type === AST_NODE_TYPES.Literal) return String(node.value);
|
|
815
|
+
if (node.type === AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0) return node.quasis[0]?.value.cooked ?? node.quasis[0]?.value.raw ?? null;
|
|
816
|
+
return null;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
//#endregion
|
|
820
|
+
//#region src/property-traverse.ts
|
|
821
|
+
/**
|
|
822
|
+
* Recursively traverses an object expression's properties to find a property with the specified name
|
|
823
|
+
* @param properties The properties of the object expression to traverse
|
|
824
|
+
* @param name The name of the property to find
|
|
825
|
+
* @returns The matching property node, or null if not found
|
|
826
|
+
*/
|
|
827
|
+
function findProperty(properties, name) {
|
|
828
|
+
for (const prop of properties) {
|
|
829
|
+
if (prop.type === AST_NODE_TYPES.Property && getPropertyName(prop.key) === name) return prop;
|
|
830
|
+
if (prop.type === AST_NODE_TYPES.SpreadElement && prop.argument.type === AST_NODE_TYPES.ObjectExpression) {
|
|
831
|
+
const found = findProperty(prop.argument.properties, name);
|
|
832
|
+
if (found != null) return found;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
return null;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
//#endregion
|
|
839
|
+
export { SEL_DISPLAY_NAME_ASSIGNMENT_EXPRESSION, SEL_IMPLICIT_RETURN_ARROW_FUNCTION_EXPRESSION, SEL_OBJECT_DESTRUCTURING_VARIABLE_DECLARATOR, findParentNode, findProperty, getClassId, getFileDirectives, getFullyQualifiedName, getFunctionDirectives, getFunctionId, getFunctionInitPath, getHumanReadableKind, getNestedCallExpressions, getNestedExpressionsOfType, getNestedIdentifiers, getNestedNewExpressions, getNestedReturnStatements, getPropertyName, getRootIdentifier, getUnderlyingExpression, hasCallInFunctionInitPath, is, isClass, isConditional, isControlFlow, isDirective, isDirectiveInFile, isDirectiveInFunction, isDirectiveKind, isDirectiveLike, isDirectiveName, isFunction, isFunctionEmpty, isFunctionImmediatelyInvoked, isFunctionType, isIdentifier, isIdentifierName, isJSX, isJSXElement, isJSXFragment, isJSXTagNameExpression, isLineBreak, isLiteral, isLoop, isMethodOrProperty, isMultiLine, isNodeEqual, isOneOf, isProcessEnvNodeEnv, isProcessEnvNodeEnvCompare, isProperty, isThisExpressionLoose, isTypeAssertionExpression, isTypeExpression, isViMock, isViMockCallback };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/ast",
|
|
3
|
-
"version": "3.0.0-next.
|
|
3
|
+
"version": "3.0.0-next.70",
|
|
4
4
|
"description": "ESLint React's TSESTree AST utility module.",
|
|
5
5
|
"homepage": "https://github.com/Rel1cx/eslint-react",
|
|
6
6
|
"bugs": {
|
|
@@ -34,10 +34,10 @@
|
|
|
34
34
|
"@typescript-eslint/typescript-estree": "canary",
|
|
35
35
|
"@typescript-eslint/utils": "canary",
|
|
36
36
|
"string-ts": "^2.3.1",
|
|
37
|
-
"@eslint-react/eff": "3.0.0-next.
|
|
37
|
+
"@eslint-react/eff": "3.0.0-next.70"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"tsdown": "^0.
|
|
40
|
+
"tsdown": "^0.21.0-beta.2",
|
|
41
41
|
"@local/configs": "0.0.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
@@ -50,6 +50,6 @@
|
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "tsdown --dts-resolve",
|
|
52
52
|
"lint:publish": "publint",
|
|
53
|
-
"lint:ts": "
|
|
53
|
+
"lint:ts": "tsl"
|
|
54
54
|
}
|
|
55
55
|
}
|