@eslint-react/ast 5.30.0-beta.1 → 5.30.0-beta.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
@@ -1,4 +1,5 @@
1
1
  import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/types";
2
+ import { VisitorKeys } from "@typescript-eslint/visitor-keys";
2
3
  /** Union of function-like node types. */
3
4
  type TSESTreeFunction = TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression;
4
5
  /** Union of class-like node types. */
@@ -106,6 +107,57 @@ declare const isEqual: {
106
107
  (a: TSESTree.Node): (b: TSESTree.Node) => boolean;
107
108
  (a: TSESTree.Node, b: TSESTree.Node): boolean;
108
109
  };
110
+ declare namespace collect_d_exports {
111
+ export { getCallExpressions, getIdentifiers, getNewExpressions, getReturnExpressions, getReturnStatements };
112
+ }
113
+ /**
114
+ * Get all identifiers referenced within an expression-like node, in source order.
115
+ *
116
+ * The property name of a non-computed member access is not a reference, so `obj.prop` collects
117
+ * only `obj`. Non-computed object property keys are not visited either. Nested function and class
118
+ * bodies are ignored, and statement roots contribute only their expression-bearing parts.
119
+ * @param node The expression-like node to collect identifiers from.
120
+ * @returns All identifiers referenced by the node.
121
+ */
122
+ declare function getIdentifiers(node: TSESTree.Node): readonly TSESTree.Identifier[];
123
+ /**
124
+ * Get all nested `new` expressions within an expression-like node, in source order.
125
+ *
126
+ * Function and class bodies are not traversed; statement roots contribute only their
127
+ * expression-bearing parts.
128
+ * @param node The expression-like node to collect `new` expressions from.
129
+ * @returns All nested `new` expressions.
130
+ */
131
+ declare const getNewExpressions: (node: TSESTree.Node) => TSESTree.NewExpression[];
132
+ /**
133
+ * Get all nested call expressions within an expression-like node, in source order.
134
+ *
135
+ * Function and class bodies are not traversed; statement roots contribute only their
136
+ * expression-bearing parts.
137
+ * @param node The expression-like node to collect call expressions from.
138
+ * @returns All nested call expressions.
139
+ */
140
+ declare const getCallExpressions: (node: TSESTree.Node) => TSESTree.CallExpression[];
141
+ /**
142
+ * Get all return statements in the subtree of `node` that belong to its function.
143
+ *
144
+ * Return statements nested inside another function are excluded. When `node` is a function, its
145
+ * own returns are collected; otherwise returns in its subtree are collected as long as they are
146
+ * not nested inside a function. The search never expands beyond the subtree of `node`.
147
+ * @param node The node whose subtree is searched.
148
+ * @returns The return statements in the subtree, in source order.
149
+ */
150
+ declare function getReturnStatements(node: TSESTree.Node): readonly TSESTree.ReturnStatement[];
151
+ /**
152
+ * Get all expressions returned by a function, in source order.
153
+ *
154
+ * The implicit return value of an expression-bodied arrow function (ex: `() => value`) is
155
+ * included, while bare `return;` statements without an argument are skipped. Return statements
156
+ * inside nested functions are excluded.
157
+ * @param node The function to collect return expressions from.
158
+ * @returns All expressions returned by the function.
159
+ */
160
+ declare function getReturnExpressions(node: TSESTreeFunction): readonly TSESTree.Expression[];
109
161
  declare namespace extract_d_exports {
110
162
  export { findProperty, getAssignmentTargets, getCalleeName, getFullyQualifiedName, getMemberChain, getPropertyName, unwrap };
111
163
  }
@@ -160,12 +212,39 @@ declare function getPropertyName(property: TSESTree.Property, effort?: "min" | "
160
212
  */
161
213
  declare function getFullyQualifiedName(node: TSESTree.Node, getText: (node: TSESTree.Node) => string): string;
162
214
  declare namespace traverse_d_exports {
163
- export { findEnclosingTryBlock, findParent, hasEnclosingTryBlock, hasParent };
215
+ export { SimpleTraverseOptions, findEnclosingTryBlock, findParent, hasEnclosingTryBlock, hasParent, simpleTraverse };
164
216
  }
165
- type Predicate<T extends TSESTree.Node> = (node: TSESTree.Node) => node is T;
166
217
  type NodePredicate = (node: TSESTree.Node) => boolean;
218
+ type Predicate<T extends TSESTree.Node> = (node: TSESTree.Node) => node is T;
219
+ /** Options for `simpleTraverse`. */
220
+ type SimpleTraverseOptions = {
221
+ /** Called for each visited node before its children are visited. */
222
+ enter?: (node: TSESTree.Node, parent: TSESTree.Node | undefined) => void;
223
+ /** Called for each visited node after its children are visited. */
224
+ leave?: (node: TSESTree.Node, parent: TSESTree.Node | undefined) => void;
225
+ /** Attach parent pointers to every visited node. */
226
+ setParentPointers?: boolean;
227
+ /** Visitor keys to use instead of the default ESTree keys (ex: to prune subtrees). */
228
+ visitorKeys?: VisitorKeys;
229
+ };
230
+ /**
231
+ * Walk the AST from `root`, calling `enter` before and `leave` after a node's children.
232
+ *
233
+ * This is a lightweight local equivalent of `simpleTraverse` from
234
+ * `@typescript-eslint/typescript-estree`: it only depends on the visitor keys, so importing it does
235
+ * not load the TypeScript compiler. Parent pointers are not attached unless `setParentPointers` is
236
+ * enabled; the parent of each node is passed to `enter` and `leave` instead. Node types missing
237
+ * from the visitor keys are not descended into, which allows pruning subtrees (ex: function or
238
+ * class bodies) with a custom map.
239
+ * @param root The root node to traverse.
240
+ * @param options Traversal options.
241
+ */
242
+ declare function simpleTraverse(root: TSESTree.Node, options?: SimpleTraverseOptions): void;
167
243
  /**
168
244
  * Walk up the AST from `node` to find the nearest ancestor matching a predicate.
245
+ *
246
+ * This function relies on the `parent` pointers attached by the parser or by
247
+ * `simpleTraverse` with `setParentPointers` enabled.
169
248
  * @param node The starting node for the upward search.
170
249
  * @param test The predicate a candidate ancestor must satisfy.
171
250
  * @param stop An optional predicate that aborts the search when it returns `true`.
@@ -184,6 +263,9 @@ declare function hasParent<T extends TSESTree.Node>(node: TSESTree.Node | null,
184
263
  declare function hasParent(node: TSESTree.Node | null, test: NodePredicate, stop?: NodePredicate): boolean;
185
264
  /**
186
265
  * Find the nearest `TryStatement` whose `try` block (not `catch`/`finally`) encloses the given node.
266
+ *
267
+ * This function relies on the `parent` pointers attached by the parser or by
268
+ * `simpleTraverse` with `setParentPointers` enabled.
187
269
  * @param node The node to check.
188
270
  * @returns The enclosing `TryStatement`, or `null` when none is found.
189
271
  */
@@ -194,4 +276,4 @@ declare function findEnclosingTryBlock(node: TSESTree.Node): TSESTree.TryStateme
194
276
  * @returns `true` when an enclosing `try` block exists.
195
277
  */
196
278
  declare function hasEnclosingTryBlock(node: TSESTree.Node): boolean;
197
- export { check_d_exports as Check, compare_d_exports as Compare, extract_d_exports as Extract, type TSESTreeClass, type TSESTreeDirective, type TSESTreeFunction, type TSESTreeFunctionOrClass, type TSESTreeJSX, type TSESTreeJSXAttributeLike, type TSESTreeJSXElementLike, type TSESTreeMethodOrPropertyDefinition, type TSESTreeTypeExpression, type TSESTreeTypeOrChainExpression, traverse_d_exports as Traverse };
279
+ export { check_d_exports as Check, collect_d_exports as Collect, compare_d_exports as Compare, extract_d_exports as Extract, type TSESTreeClass, type TSESTreeDirective, type TSESTreeFunction, type TSESTreeFunctionOrClass, type TSESTreeJSX, type TSESTreeJSXAttributeLike, type TSESTreeJSXElementLike, type TSESTreeMethodOrPropertyDefinition, type TSESTreeTypeExpression, type TSESTreeTypeOrChainExpression, traverse_d_exports as Traverse };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { t as __exportAll } from "./rolldown-runtime-D8jQpDG7.js";
2
2
  import { AST_NODE_TYPES } from "@typescript-eslint/types";
3
3
  import * as ASTUtils from "@typescript-eslint/utils/ast-utils";
4
+ import { visitorKeys } from "@typescript-eslint/visitor-keys";
4
5
  // src/check.ts
5
6
  var check_exports = /* @__PURE__ */ __exportAll({
6
7
  is: () => is,
@@ -394,8 +395,30 @@ var traverse_exports = /* @__PURE__ */ __exportAll({
394
395
  findEnclosingTryBlock: () => findEnclosingTryBlock,
395
396
  findParent: () => findParent,
396
397
  hasEnclosingTryBlock: () => hasEnclosingTryBlock,
397
- hasParent: () => hasParent
398
+ hasParent: () => hasParent,
399
+ simpleTraverse: () => simpleTraverse
398
400
  });
401
+ function isNode(value) {
402
+ return typeof value === "object" && value != null && "type" in value && typeof value.type === "string";
403
+ }
404
+ function simpleTraverse(root, options = {}) {
405
+ const { enter, leave, setParentPointers = false, visitorKeys: visitorKeys$1 = visitorKeys } = options;
406
+ const visit = (node, parent) => {
407
+ if (!isNode(node)) return;
408
+ if (setParentPointers) {
409
+ if (parent == null) delete node.parent;
410
+ else node.parent = parent;
411
+ }
412
+ enter?.(node, parent);
413
+ for (const key of visitorKeys$1[node.type] ?? []) {
414
+ const child = node[key];
415
+ if (Array.isArray(child)) for (const item of child) visit(item, node);
416
+ else visit(child, node);
417
+ }
418
+ leave?.(node, parent);
419
+ };
420
+ visit(root, void 0);
421
+ }
399
422
  function findParent(node, test, stop) {
400
423
  if (node == null) return null;
401
424
  let current = node.parent;
@@ -419,4 +442,126 @@ function findEnclosingTryBlock(node) {
419
442
  function hasEnclosingTryBlock(node) {
420
443
  return findEnclosingTryBlock(node) != null;
421
444
  }
422
- export { check_exports as Check, compare_exports as Compare, extract_exports as Extract, traverse_exports as Traverse };
445
+ // src/collect.ts
446
+ var collect_exports = /* @__PURE__ */ __exportAll({
447
+ getCallExpressions: () => getCallExpressions,
448
+ getIdentifiers: () => getIdentifiers,
449
+ getNewExpressions: () => getNewExpressions,
450
+ getReturnExpressions: () => getReturnExpressions,
451
+ getReturnStatements: () => getReturnStatements
452
+ });
453
+ const EXPRESSION_VISITOR_KEYS = {
454
+ [AST_NODE_TYPES.ArrayExpression]: ["elements"],
455
+ [AST_NODE_TYPES.ArrayPattern]: ["elements"],
456
+ [AST_NODE_TYPES.AssignmentExpression]: ["left", "right"],
457
+ [AST_NODE_TYPES.AssignmentPattern]: ["left", "right"],
458
+ [AST_NODE_TYPES.AwaitExpression]: ["argument"],
459
+ [AST_NODE_TYPES.BinaryExpression]: ["left", "right"],
460
+ [AST_NODE_TYPES.CallExpression]: ["callee", "arguments"],
461
+ [AST_NODE_TYPES.ChainExpression]: ["expression"],
462
+ [AST_NODE_TYPES.ConditionalExpression]: [
463
+ "test",
464
+ "consequent",
465
+ "alternate"
466
+ ],
467
+ [AST_NODE_TYPES.Decorator]: ["expression"],
468
+ [AST_NODE_TYPES.DoWhileStatement]: ["test"],
469
+ [AST_NODE_TYPES.ExpressionStatement]: ["expression"],
470
+ [AST_NODE_TYPES.ForInStatement]: ["left", "right"],
471
+ [AST_NODE_TYPES.ForOfStatement]: ["left", "right"],
472
+ [AST_NODE_TYPES.ForStatement]: ["test"],
473
+ [AST_NODE_TYPES.IfStatement]: ["test"],
474
+ [AST_NODE_TYPES.ImportExpression]: ["source", "options"],
475
+ [AST_NODE_TYPES.JSXAttribute]: ["name", "value"],
476
+ [AST_NODE_TYPES.JSXElement]: [
477
+ "openingElement",
478
+ "children",
479
+ "closingElement"
480
+ ],
481
+ [AST_NODE_TYPES.JSXExpressionContainer]: ["expression"],
482
+ [AST_NODE_TYPES.JSXFragment]: [
483
+ "openingFragment",
484
+ "children",
485
+ "closingFragment"
486
+ ],
487
+ [AST_NODE_TYPES.JSXOpeningElement]: [
488
+ "name",
489
+ "typeArguments",
490
+ "attributes"
491
+ ],
492
+ [AST_NODE_TYPES.JSXSpreadAttribute]: ["argument"],
493
+ [AST_NODE_TYPES.JSXSpreadChild]: ["expression"],
494
+ [AST_NODE_TYPES.LogicalExpression]: ["left", "right"],
495
+ [AST_NODE_TYPES.MemberExpression]: ["object", "property"],
496
+ [AST_NODE_TYPES.NewExpression]: ["callee", "arguments"],
497
+ [AST_NODE_TYPES.ObjectExpression]: ["properties"],
498
+ [AST_NODE_TYPES.ObjectPattern]: ["properties"],
499
+ [AST_NODE_TYPES.Property]: ["key", "value"],
500
+ [AST_NODE_TYPES.RestElement]: ["argument"],
501
+ [AST_NODE_TYPES.SequenceExpression]: ["expressions"],
502
+ [AST_NODE_TYPES.SpreadElement]: ["argument"],
503
+ [AST_NODE_TYPES.SwitchCase]: ["test"],
504
+ [AST_NODE_TYPES.TaggedTemplateExpression]: ["tag", "quasi"],
505
+ [AST_NODE_TYPES.TemplateLiteral]: ["expressions"],
506
+ [AST_NODE_TYPES.TSAsExpression]: ["expression"],
507
+ [AST_NODE_TYPES.TSClassImplements]: ["expression"],
508
+ [AST_NODE_TYPES.TSExportAssignment]: ["expression"],
509
+ [AST_NODE_TYPES.TSExternalModuleReference]: ["expression"],
510
+ [AST_NODE_TYPES.TSInstantiationExpression]: ["expression"],
511
+ [AST_NODE_TYPES.TSInterfaceHeritage]: ["expression"],
512
+ [AST_NODE_TYPES.TSNonNullExpression]: ["expression"],
513
+ [AST_NODE_TYPES.TSSatisfiesExpression]: ["expression"],
514
+ [AST_NODE_TYPES.TSTypeAssertion]: ["expression"],
515
+ [AST_NODE_TYPES.UnaryExpression]: ["argument"],
516
+ [AST_NODE_TYPES.UpdateExpression]: ["argument"],
517
+ [AST_NODE_TYPES.WhileStatement]: ["test"],
518
+ [AST_NODE_TYPES.YieldExpression]: ["argument"]
519
+ };
520
+ const RETURN_VISITOR_KEYS = {
521
+ ...visitorKeys,
522
+ [AST_NODE_TYPES.ClassDeclaration]: [],
523
+ [AST_NODE_TYPES.ClassExpression]: []
524
+ };
525
+ function collectNodes(node, isNode) {
526
+ const nodes = [];
527
+ simpleTraverse(node, {
528
+ enter(node, parent) {
529
+ const parentNode = parent ?? node.parent;
530
+ if (parentNode?.type === AST_NODE_TYPES.MemberExpression && !parentNode.computed && parentNode.property === node) return;
531
+ if (parentNode?.type === AST_NODE_TYPES.Property && !parentNode.computed && parentNode.key === node) return;
532
+ if (isNode(node)) nodes.push(node);
533
+ },
534
+ visitorKeys: EXPRESSION_VISITOR_KEYS
535
+ });
536
+ return nodes;
537
+ }
538
+ function getIdentifiers(node) {
539
+ return collectNodes(node, isIdentifier);
540
+ }
541
+ function getExpressionsOfType(type) {
542
+ const isNodeOfType = is(type);
543
+ return (node) => collectNodes(node, isNodeOfType);
544
+ }
545
+ const getNewExpressions = getExpressionsOfType(AST_NODE_TYPES.NewExpression);
546
+ const getCallExpressions = getExpressionsOfType(AST_NODE_TYPES.CallExpression);
547
+ function getReturnStatements(node) {
548
+ const stmts = [];
549
+ const boundaryDepth = isFunction$1(node) ? 1 : 0;
550
+ let depth = 0;
551
+ simpleTraverse(node, {
552
+ enter(child) {
553
+ if (isFunction$1(child)) depth++;
554
+ if (child.type === AST_NODE_TYPES.ReturnStatement && depth === boundaryDepth) stmts.push(child);
555
+ },
556
+ leave(child) {
557
+ if (isFunction$1(child)) depth--;
558
+ },
559
+ visitorKeys: RETURN_VISITOR_KEYS
560
+ });
561
+ return stmts;
562
+ }
563
+ function getReturnExpressions(node) {
564
+ if (node.body.type !== AST_NODE_TYPES.BlockStatement) return [node.body];
565
+ return getReturnStatements(node).flatMap((stmt) => stmt.argument == null ? [] : [stmt.argument]);
566
+ }
567
+ export { check_exports as Check, collect_exports as Collect, compare_exports as Compare, extract_exports as Extract, traverse_exports as Traverse };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/ast",
3
- "version": "5.30.0-beta.1",
3
+ "version": "5.30.0-beta.2",
4
4
  "description": "ESLint React's TSESTree AST utility module.",
5
5
  "homepage": "https://github.com/Rel1cx/eslint-react",
6
6
  "bugs": {
@@ -30,8 +30,8 @@
30
30
  ],
31
31
  "dependencies": {
32
32
  "@typescript-eslint/types": "^8.72.0",
33
- "@typescript-eslint/typescript-estree": "^8.72.0",
34
- "@typescript-eslint/utils": "^8.72.0"
33
+ "@typescript-eslint/utils": "^8.72.0",
34
+ "@typescript-eslint/visitor-keys": "^8.72.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@local/configs": "0.0.0",