@eslint-react/ast 2.8.2-next.0 → 2.8.2-next.2

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 CHANGED
@@ -18,11 +18,41 @@ type TSESTreeTypeExpression = TSESTree$1.TSAsExpression | TSESTree$1.TSNonNullEx
18
18
  type TSESTreeTypeAssertionExpression = TSESTree$1.TSAsExpression | TSESTree$1.TSNonNullExpression | TSESTree$1.TSSatisfiesExpression | TSESTree$1.TSTypeAssertion;
19
19
  //#endregion
20
20
  //#region src/class-id.d.ts
21
+ /**
22
+ * Get the class identifier of a class node
23
+ * @param node The class node to get the identifier from
24
+ * @returns The class identifier or unit if not found
25
+ */
21
26
  declare function getClassId(node: TSESTreeClass): TSESTree.BindingName | unit;
22
27
  //#endregion
28
+ //#region src/directive-is.d.ts
29
+ /**
30
+ * Check if a node is a directive
31
+ * @param node The node to check
32
+ * @returns True if the node is a directive, false otherwise
33
+ */
34
+ declare function isDirective(node: TSESTree.Node): node is TSESTree.StringLiteral;
35
+ //#endregion
36
+ //#region src/directive-kind.d.ts
37
+ type DirectiveKind = "use client" | "use server" | "use memo" | "use no memo";
38
+ /**
39
+ * Check if a node is a directive kind
40
+ * @param kind The kind to check
41
+ * @returns True if the kind is a directive kind, false otherwise
42
+ */
43
+ declare function isDirectiveKind(kind: string): kind is DirectiveKind;
44
+ //#endregion
45
+ //#region src/directive-name.d.ts
46
+ /**
47
+ * Check if a string is a directive name
48
+ * @param name The string to check
49
+ * @returns True if the string is a directive name, false otherwise
50
+ */
51
+ declare function isDirectiveName(name: string): boolean;
52
+ //#endregion
23
53
  //#region src/equal.d.ts
24
54
  /**
25
- * Determine whether node equals to another node
55
+ * Check if two nodes are equal
26
56
  * @param a node to compare
27
57
  * @param b node to compare
28
58
  * @returns `true` if node equal
@@ -44,11 +74,11 @@ declare function getUnderlyingExpression(node: TSESTree.Node): Exclude<TSESTree.
44
74
  //#endregion
45
75
  //#region src/expression-is.d.ts
46
76
  /**
47
- * Check if the given expression is a 'this' expression.
48
- * Unwraps any type expressions before checking.
77
+ * Check if the given expression is a 'this' expression
78
+ * Unwraps any type expressions before checking
49
79
  *
50
80
  * @param node The expression node to check
51
- * @returns true if the expression is a ThisExpression, false otherwise
81
+ * @returns True if the expression is a 'this' expression, false otherwise
52
82
  */
53
83
  declare function isThisExpressionLoose(node: TSESTree.Expression): boolean;
54
84
  //#endregion
@@ -87,7 +117,15 @@ declare const getNestedNewExpressions: (node: TSESTree.Node) => TSESTree.NewExpr
87
117
  */
88
118
  declare const getNestedCallExpressions: (node: TSESTree.Node) => TSESTree.CallExpression[];
89
119
  //#endregion
90
- //#region src/function-directives.d.ts
120
+ //#region src/file-directive.d.ts
121
+ /**
122
+ * Get all directive string literals from a program node
123
+ * @param node The program AST node
124
+ * @returns The array of directive string literals (e.g., "use strict")
125
+ */
126
+ declare function getFileDirectives(node: TSESTree.Program): TSESTree.StringLiteral[];
127
+ //#endregion
128
+ //#region src/function-directive.d.ts
91
129
  /**
92
130
  * Get all directive string literals from a function node
93
131
  * @param node The function AST node
@@ -108,8 +146,8 @@ type FunctionID = ReturnType<typeof getFunctionId>;
108
146
  //#endregion
109
147
  //#region src/function-init-path.d.ts
110
148
  /**
111
- * Represents various AST paths for React component function declarations.
112
- * Each tuple type represents a specific component definition pattern.
149
+ * Represents various AST paths for React component function declarations
150
+ * Each tuple type represents a specific component definition pattern
113
151
  */
114
152
  type FunctionInitPath = readonly [TSESTree.FunctionDeclaration] | readonly [TSESTree.VariableDeclaration, TSESTree.VariableDeclarator, TSESTreeFunction] | readonly [TSESTree.VariableDeclaration, TSESTree.VariableDeclarator, TSESTree.CallExpression, TSESTreeFunction] | readonly [TSESTree.VariableDeclaration, TSESTree.VariableDeclarator, TSESTree.CallExpression, TSESTree.CallExpression, TSESTreeFunction] | readonly [TSESTree.VariableDeclaration, TSESTree.VariableDeclarator, TSESTree.ObjectExpression, TSESTree.Property, TSESTreeFunction] | readonly [TSESTree.VariableDeclaration, TSESTree.VariableDeclarator, TSESTree.ObjectExpression, TSESTree.Property, TSESTree.CallExpression, TSESTreeFunction] | readonly [TSESTree.VariableDeclaration, TSESTree.VariableDeclarator, TSESTree.ObjectExpression, TSESTree.Property, TSESTree.CallExpression, TSESTree.CallExpression, TSESTreeFunction] | readonly [TSESTree.ClassDeclaration, TSESTree.ClassBody, TSESTree.MethodDefinition, TSESTreeFunction] | readonly [TSESTree.ClassDeclaration, TSESTree.ClassBody, TSESTree.PropertyDefinition, TSESTreeFunction];
115
153
  /**
@@ -131,12 +169,27 @@ declare function getFunctionInitPath(node: TSESTreeFunction): unit | FunctionIni
131
169
  declare function hasCallInFunctionInitPath(callName: string, initPath: FunctionInitPath): boolean;
132
170
  //#endregion
133
171
  //#region src/function-is.d.ts
172
+ /**
173
+ * Check if a function is empty
174
+ * @param node The function node to check
175
+ * @returns True if the function is empty, false otherwise
176
+ */
134
177
  declare function isFunctionEmpty(node: TSESTreeFunction): boolean;
178
+ /**
179
+ * Check if a function is immediately invoked
180
+ * @param node The function node to check
181
+ * @returns True if the function is immediately invoked, false otherwise
182
+ */
135
183
  declare function isFunctionImmediatelyInvoked(node: TSESTreeFunction): boolean;
136
184
  //#endregion
137
185
  //#region src/identifier-is.d.ts
138
- declare function isNaN(node: TSESTree.Node | null | unit): node is TSESTree.Identifier;
139
- declare function isUndefined(node: TSESTree.Node | null | unit): node is TSESTree.Identifier;
186
+ /**
187
+ * Check if the given node is an identifier
188
+ * @param node The node to check
189
+ * @param name The name to check
190
+ * @returns True if the node is an identifier, false otherwise
191
+ */
192
+ declare function isIdentifier(node: TSESTree.Node | null | unit, name?: string): node is TSESTree.Identifier;
140
193
  //#endregion
141
194
  //#region src/identifier-name.d.ts
142
195
  declare function isIdentifierName(name: string): boolean;
@@ -215,14 +268,6 @@ declare function isProcessEnvNodeEnv(node: TSESTree.Node | null | unit): node is
215
268
  */
216
269
  declare function isProcessEnvNodeEnvCompare(node: TSESTree.Node | null | unit, operator: "===" | "!==", value: "development" | "production"): node is TSESTree.BinaryExpression;
217
270
  //#endregion
218
- //#region src/program-directives.d.ts
219
- /**
220
- * Get all directive string literals from a program node
221
- * @param node The program AST node
222
- * @returns The array of directive string literals (e.g., "use strict")
223
- */
224
- declare function getProgramDirectives(node: TSESTree.Program): TSESTree.StringLiteral[];
225
- //#endregion
226
271
  //#region src/property-name.d.ts
227
272
  declare function getPropertyName(node: TSESTree.Node): string | unit;
228
273
  //#endregion
@@ -280,4 +325,4 @@ declare function isViMock(node: TSESTree.Node | null | unit): node is TSESTree.M
280
325
  */
281
326
  declare function isViMockCallback(node: TSESTree.Node | null | unit): boolean;
282
327
  //#endregion
283
- export { DisplayNameAssignmentExpression, FunctionID, FunctionInitPath, ImplicitReturnArrowFunctionExpression, ObjectDestructuringVariableDeclarator, SEL_DISPLAY_NAME_ASSIGNMENT_EXPRESSION, SEL_IMPLICIT_RETURN_ARROW_FUNCTION_EXPRESSION, SEL_OBJECT_DESTRUCTURING_VARIABLE_DECLARATOR, TSESTreeArrayTupleType, TSESTreeClass, TSESTreeDestructuringPattern, TSESTreeFunction, TSESTreeFunctionType, TSESTreeJSX, TSESTreeJSXAttributeLike, TSESTreeLoop, TSESTreeMethodOrProperty, TSESTreeProperty, TSESTreeTypeAssertionExpression, TSESTreeTypeDeclaration, TSESTreeTypeExpression, findParentNode, getClassId, getFunctionDirectives, getFunctionId, getFunctionInitPath, getNestedCallExpressions, getNestedExpressionsOfType, getNestedIdentifiers, getNestedNewExpressions, getNestedReturnStatements, getProgramDirectives, getPropertyName, getUnderlyingExpression, hasCallInFunctionInitPath, is, isClass, isConditional, isControlFlow, isFunction, isFunctionEmpty, isFunctionImmediatelyInvoked, isFunctionType, isIdentifierName, isJSX, isJSXElement, isJSXFragment, isJSXTagNameExpression, isLineBreak, isLiteral, isLoop, isMethodOrProperty, isMultiLine, isNaN, isNodeEqual, isOneOf, isProcessEnvNodeEnv, isProcessEnvNodeEnvCompare, isProperty, isThisExpressionLoose, isTypeAssertionExpression, isTypeExpression, isUndefined, isViMock, isViMockCallback, toDelimiterFormat, toStringFormat };
328
+ 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, TSESTreeFunction, TSESTreeFunctionType, TSESTreeJSX, TSESTreeJSXAttributeLike, TSESTreeLoop, TSESTreeMethodOrProperty, TSESTreeProperty, TSESTreeTypeAssertionExpression, TSESTreeTypeDeclaration, TSESTreeTypeExpression, findParentNode, getClassId, getFileDirectives, getFunctionDirectives, getFunctionId, getFunctionInitPath, getNestedCallExpressions, getNestedExpressionsOfType, getNestedIdentifiers, getNestedNewExpressions, getNestedReturnStatements, getPropertyName, getUnderlyingExpression, hasCallInFunctionInitPath, is, isClass, isConditional, isControlFlow, isDirective, isDirectiveKind, 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, toDelimiterFormat, toStringFormat };
package/dist/index.js CHANGED
@@ -5,12 +5,64 @@ import { simpleTraverse } from "@typescript-eslint/typescript-estree";
5
5
  import { delimiterCase, replace, toLowerCase } from "string-ts";
6
6
 
7
7
  //#region src/class-id.ts
8
+ /**
9
+ * Get the class identifier of a class node
10
+ * @param node The class node to get the identifier from
11
+ * @returns The class identifier or unit if not found
12
+ */
8
13
  function getClassId(node) {
9
14
  if (node.id != null) return node.id;
10
15
  if (node.parent.type === AST_NODE_TYPES.VariableDeclarator) return node.parent.id;
11
16
  return unit;
12
17
  }
13
18
 
19
+ //#endregion
20
+ //#region src/directive-name.ts
21
+ /**
22
+ * Check if a string is a directive name
23
+ * @param name The string to check
24
+ * @returns True if the string is a directive name, false otherwise
25
+ */
26
+ function isDirectiveName(name) {
27
+ return name.startsWith("use ") && name.length > 4;
28
+ }
29
+
30
+ //#endregion
31
+ //#region src/literal.ts
32
+ function isLiteral(node, type) {
33
+ if (node.type !== AST_NODE_TYPES.Literal) return false;
34
+ if (type == null) return true;
35
+ switch (type) {
36
+ case "boolean": return typeof node.value === "boolean";
37
+ case "null": return node.value === null;
38
+ case "number": return typeof node.value === "number";
39
+ case "regexp": return "regex" in node;
40
+ case "string": return typeof node.value === "string";
41
+ }
42
+ }
43
+
44
+ //#endregion
45
+ //#region src/directive-is.ts
46
+ /**
47
+ * Check if a node is a directive
48
+ * @param node The node to check
49
+ * @returns True if the node is a directive, false otherwise
50
+ */
51
+ function isDirective(node) {
52
+ return isLiteral(node, "string") && isDirectiveName(node.value);
53
+ }
54
+
55
+ //#endregion
56
+ //#region src/directive-kind.ts
57
+ /**
58
+ * Check if a node is a directive kind
59
+ * @param kind The kind to check
60
+ * @returns True if the kind is a directive kind, false otherwise
61
+ */
62
+ function isDirectiveKind(kind) {
63
+ return kind === "use client" || kind === "use server" || kind === "use memo" || kind === "use no memo";
64
+ }
65
+
14
66
  //#endregion
15
67
  //#region src/is.ts
16
68
  const is = ASTUtils.isNodeOfType;
@@ -102,7 +154,7 @@ function getUnderlyingExpression(node) {
102
154
  //#endregion
103
155
  //#region src/equal.ts
104
156
  /**
105
- * Determine whether node equals to another node
157
+ * Check if two nodes are equal
106
158
  * @param a node to compare
107
159
  * @param b node to compare
108
160
  * @returns `true` if node equal
@@ -143,11 +195,11 @@ const isNodeEqual = dual(2, (a, b) => {
143
195
  //#endregion
144
196
  //#region src/expression-is.ts
145
197
  /**
146
- * Check if the given expression is a 'this' expression.
147
- * Unwraps any type expressions before checking.
198
+ * Check if the given expression is a 'this' expression
199
+ * Unwraps any type expressions before checking
148
200
  *
149
201
  * @param node The expression node to check
150
- * @returns true if the expression is a ThisExpression, false otherwise
202
+ * @returns True if the expression is a 'this' expression, false otherwise
151
203
  */
152
204
  function isThisExpressionLoose(node) {
153
205
  return getUnderlyingExpression(node).type === AST_NODE_TYPES.ThisExpression;
@@ -349,21 +401,25 @@ const getNestedNewExpressions = getNestedExpressionsOfType(AST_NODE_TYPES.NewExp
349
401
  const getNestedCallExpressions = getNestedExpressionsOfType(AST_NODE_TYPES.CallExpression);
350
402
 
351
403
  //#endregion
352
- //#region src/literal.ts
353
- function isLiteral(node, type) {
354
- if (node.type !== AST_NODE_TYPES.Literal) return false;
355
- if (type == null) return true;
356
- switch (type) {
357
- case "boolean": return typeof node.value === "boolean";
358
- case "null": return node.value === null;
359
- case "number": return typeof node.value === "number";
360
- case "regexp": return "regex" in node;
361
- case "string": return typeof node.value === "string";
404
+ //#region src/file-directive.ts
405
+ /**
406
+ * Get all directive string literals from a program node
407
+ * @param node The program AST node
408
+ * @returns The array of directive string literals (e.g., "use strict")
409
+ */
410
+ function getFileDirectives(node) {
411
+ const directives = [];
412
+ for (const stmt of node.body) {
413
+ if (stmt.type !== AST_NODE_TYPES.ExpressionStatement) continue;
414
+ const expr = getUnderlyingExpression(stmt.expression);
415
+ if (!isDirective(expr)) continue;
416
+ directives.push(expr);
362
417
  }
418
+ return directives;
363
419
  }
364
420
 
365
421
  //#endregion
366
- //#region src/function-directives.ts
422
+ //#region src/function-directive.ts
367
423
  /**
368
424
  * Get all directive string literals from a function node
369
425
  * @param node The function AST node
@@ -375,7 +431,7 @@ function getFunctionDirectives(node) {
375
431
  for (const stmt of node.body.body) {
376
432
  if (stmt.type !== AST_NODE_TYPES.ExpressionStatement) continue;
377
433
  const expr = getUnderlyingExpression(stmt.expression);
378
- if (!isLiteral(expr, "string")) continue;
434
+ if (!isDirective(expr)) continue;
379
435
  directives.push(expr);
380
436
  }
381
437
  return directives;
@@ -476,20 +532,33 @@ function hasCallInFunctionInitPath(callName, initPath) {
476
532
 
477
533
  //#endregion
478
534
  //#region src/function-is.ts
535
+ /**
536
+ * Check if a function is empty
537
+ * @param node The function node to check
538
+ * @returns True if the function is empty, false otherwise
539
+ */
479
540
  function isFunctionEmpty(node) {
480
541
  return node.body.type === AST_NODE_TYPES.BlockStatement && node.body.body.length === 0;
481
542
  }
543
+ /**
544
+ * Check if a function is immediately invoked
545
+ * @param node The function node to check
546
+ * @returns True if the function is immediately invoked, false otherwise
547
+ */
482
548
  function isFunctionImmediatelyInvoked(node) {
483
549
  return node.type !== AST_NODE_TYPES.FunctionDeclaration && node.parent.type === AST_NODE_TYPES.CallExpression && node.parent.callee === node;
484
550
  }
485
551
 
486
552
  //#endregion
487
553
  //#region src/identifier-is.ts
488
- function isNaN(node) {
489
- return node != null && node.type === AST_NODE_TYPES.Identifier && node.name === "NaN";
490
- }
491
- function isUndefined(node) {
492
- return node != null && node.type === AST_NODE_TYPES.Identifier && node.name === "undefined";
554
+ /**
555
+ * Check if the given node is an identifier
556
+ * @param node The node to check
557
+ * @param name The name to check
558
+ * @returns True if the node is an identifier, false otherwise
559
+ */
560
+ function isIdentifier(node, name) {
561
+ return node != null && node.type === AST_NODE_TYPES.Identifier && (name == null || node.name === name);
493
562
  }
494
563
 
495
564
  //#endregion
@@ -585,24 +654,6 @@ function isProcessEnvNodeEnvCompare(node, operator, value) {
585
654
  return false;
586
655
  }
587
656
 
588
- //#endregion
589
- //#region src/program-directives.ts
590
- /**
591
- * Get all directive string literals from a program node
592
- * @param node The program AST node
593
- * @returns The array of directive string literals (e.g., "use strict")
594
- */
595
- function getProgramDirectives(node) {
596
- const directives = [];
597
- for (const stmt of node.body) {
598
- if (stmt.type !== AST_NODE_TYPES.ExpressionStatement) continue;
599
- const expr = getUnderlyingExpression(stmt.expression);
600
- if (!isLiteral(expr, "string")) continue;
601
- directives.push(expr);
602
- }
603
- return directives;
604
- }
605
-
606
657
  //#endregion
607
658
  //#region src/property-name.ts
608
659
  function getPropertyName(node) {
@@ -650,4 +701,4 @@ function isViMockCallback(node) {
650
701
  }
651
702
 
652
703
  //#endregion
653
- export { SEL_DISPLAY_NAME_ASSIGNMENT_EXPRESSION, SEL_IMPLICIT_RETURN_ARROW_FUNCTION_EXPRESSION, SEL_OBJECT_DESTRUCTURING_VARIABLE_DECLARATOR, findParentNode, getClassId, getFunctionDirectives, getFunctionId, getFunctionInitPath, getNestedCallExpressions, getNestedExpressionsOfType, getNestedIdentifiers, getNestedNewExpressions, getNestedReturnStatements, getProgramDirectives, getPropertyName, getUnderlyingExpression, hasCallInFunctionInitPath, is, isClass, isConditional, isControlFlow, isFunction, isFunctionEmpty, isFunctionImmediatelyInvoked, isFunctionType, isIdentifierName, isJSX, isJSXElement, isJSXFragment, isJSXTagNameExpression, isLineBreak, isLiteral, isLoop, isMethodOrProperty, isMultiLine, isNaN, isNodeEqual, isOneOf, isProcessEnvNodeEnv, isProcessEnvNodeEnvCompare, isProperty, isThisExpressionLoose, isTypeAssertionExpression, isTypeExpression, isUndefined, isViMock, isViMockCallback, toDelimiterFormat, toStringFormat };
704
+ export { SEL_DISPLAY_NAME_ASSIGNMENT_EXPRESSION, SEL_IMPLICIT_RETURN_ARROW_FUNCTION_EXPRESSION, SEL_OBJECT_DESTRUCTURING_VARIABLE_DECLARATOR, findParentNode, getClassId, getFileDirectives, getFunctionDirectives, getFunctionId, getFunctionInitPath, getNestedCallExpressions, getNestedExpressionsOfType, getNestedIdentifiers, getNestedNewExpressions, getNestedReturnStatements, getPropertyName, getUnderlyingExpression, hasCallInFunctionInitPath, is, isClass, isConditional, isControlFlow, isDirective, isDirectiveKind, 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, toDelimiterFormat, toStringFormat };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/ast",
3
- "version": "2.8.2-next.0",
3
+ "version": "2.8.2-next.2",
4
4
  "description": "ESLint React's TSESTree AST utility module.",
5
5
  "homepage": "https://github.com/Rel1cx/eslint-react",
6
6
  "bugs": {
@@ -34,7 +34,7 @@
34
34
  "@typescript-eslint/typescript-estree": "^8.54.0",
35
35
  "@typescript-eslint/utils": "^8.54.0",
36
36
  "string-ts": "^2.3.1",
37
- "@eslint-react/eff": "2.8.2-next.0"
37
+ "@eslint-react/eff": "2.8.2-next.2"
38
38
  },
39
39
  "devDependencies": {
40
40
  "tsdown": "^0.20.1",