Skip to content

Commit 5ae5235

Browse files
authored
🤖 Merge PR DefinitelyTyped#72705 feat(esrecurse): add types by @hkleungai
1 parent 4e93693 commit 5ae5235

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed

types/esrecurse/.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts

types/esrecurse/esrecurse-tests.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { visit, Visitor } from "esrecurse";
2+
3+
// Mock AST node for testing
4+
const mockAst = {
5+
type: "Program",
6+
body: [
7+
{ type: "Literal", value: 42 },
8+
{
9+
type: "TestExpression",
10+
argument: { type: "Literal", value: 20 },
11+
extended: true,
12+
},
13+
],
14+
};
15+
16+
{
17+
// $ExpectType Visitor
18+
let visitor = new Visitor();
19+
// $ExpectType void
20+
visitor.visit(mockAst);
21+
22+
// $ExpectType Visitor
23+
visitor = new Visitor(null, {
24+
fallback: "iteration",
25+
childVisitorKeys: { TestExpression: ["argument"] },
26+
});
27+
// $ExpectType void
28+
visitor.visit(mockAst);
29+
30+
// $ExpectType Visitor
31+
visitor = new Visitor();
32+
visitor["Literal"] = (node: any) => {
33+
node.value;
34+
};
35+
// $ExpectType void
36+
visitor.visit(mockAst);
37+
}
38+
39+
{
40+
class DerivedVisitor extends Visitor {
41+
constructor() {
42+
super(null);
43+
}
44+
45+
Literal(node: any) {
46+
node.value;
47+
}
48+
}
49+
// $ExpectType DerivedVisitor
50+
const derivedVisitor = new DerivedVisitor();
51+
// $ExpectType void
52+
derivedVisitor.visit(mockAst);
53+
}
54+
55+
{
56+
const objectVisitor = {
57+
Literal: (node: any) => {
58+
// $ExpectType any
59+
node.value;
60+
},
61+
};
62+
63+
// $ExpectType void
64+
visit(mockAst, objectVisitor);
65+
66+
// $ExpectType void
67+
visit(mockAst, objectVisitor, {
68+
fallback: (node: any) => Object.keys(node).filter((key) => key !== "parent"),
69+
childVisitorKeys: { TestExpression: ["argument"] },
70+
});
71+
72+
// $ExpectType void
73+
visit(mockAst, new Visitor(), { fallback: "iteration" });
74+
}

types/esrecurse/index.d.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Options for the Visitor constructor and visit function.
3+
*/
4+
interface VisitorOptions {
5+
/**
6+
* Fallback strategy for unknown node types.
7+
* @default 'iteration'
8+
*/
9+
fallback?: "iteration" | ((node: any) => string[]);
10+
11+
/**
12+
* Custom keys for child nodes by node type.
13+
* @default {}
14+
*/
15+
childVisitorKeys?: Record<string, string[]>;
16+
}
17+
18+
/**
19+
* A visitor class for recursively traversing ECMAScript AST.
20+
*/
21+
declare class Visitor {
22+
/**
23+
* Creates a new Visitor instance.
24+
* @param options Configuration options for the visitor.
25+
*/
26+
constructor(visitor?: Visitor | null, options?: VisitorOptions | null);
27+
28+
/**
29+
* Visits a node, invoking the appropriate handler.
30+
* @param node The AST node to visit.
31+
*/
32+
visit(node: any): void;
33+
34+
/**
35+
* Visits the children of a node based on childVisitorKeys.
36+
* @param node The AST node whose children to visit.
37+
*/
38+
visitChildren(node: any): void;
39+
40+
/**
41+
* Dynamically defined methods for specific node types (e.g., Literal, Program).
42+
* @param node The AST node to process.
43+
*/
44+
[nodeType: string]: ((node: any) => void) | undefined;
45+
}
46+
47+
/**
48+
* Visits an AST node with the specified visitor.
49+
* @param ast The AST node to traverse.
50+
* @param visitor A visitor instance or visitor object.
51+
* @param options Configuration options for the traversal.
52+
*/
53+
declare function visit(
54+
ast: any,
55+
visitor?: Visitor | Record<string, (node: any) => void> | null,
56+
options?: VisitorOptions,
57+
): void;
58+
59+
export { visit, Visitor, type VisitorOptions };

types/esrecurse/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/esrecurse",
4+
"version": "4.3.9999",
5+
"projects": [
6+
"https://github.com/estools/esrecurse"
7+
],
8+
"devDependencies": {
9+
"@types/esrecurse": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "Jimmy Leung",
14+
"githubUsername": "hkleungai"
15+
}
16+
]
17+
}

types/esrecurse/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"esrecurse-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)