UNPKG

4.63 kBTypeScriptView Raw
1import * as ESTree from "estree";
2
3declare namespace ESTraverse {
4 const Syntax: Syntax;
5
6 const VisitorKeys: VisitorKeys;
7
8 enum VisitorOption {
9 Break,
10 Skip,
11 Remove,
12 }
13
14 class Controller {
15 /**
16 * Traverse the AST.
17 */
18 traverse(root: ESTree.Node, visitor: Visitor): void;
19
20 /**
21 * Traverse and replace the AST.
22 */
23 replace(root: ESTree.Node, visitor: Visitor): ESTree.Node;
24
25 /**
26 * The current node.
27 */
28 current(): ESTree.Node;
29
30 /**
31 * The type of current node.
32 */
33 type(): string;
34
35 /**
36 * Obtain the property paths array from root to the current node.
37 */
38 path(): Array<string | number> | null;
39
40 /**
41 * An array of parent elements.
42 */
43 parents(): ESTree.Node[];
44
45 /**
46 * Notify the controller to break the traversals, skip the child nodes of current node or remove the
47 * current node.
48 */
49 notify(flag: VisitorOption): void;
50
51 /**
52 * Break the traversals.
53 */
54 break(): void;
55
56 /**
57 * Skip the child nodes of current node.
58 */
59 skip(): void;
60
61 /**
62 * Remove the current node.
63 */
64 remove(): void;
65 }
66
67 function traverse(root: ESTree.Node, visitor: Visitor): void;
68
69 function replace(root: ESTree.Node, visitor: Visitor): ESTree.Node;
70
71 function attachComments(tree: ESTree.Node, providedComments: ESTree.Comment[], tokens: ESTree.Node[]): ESTree.Node;
72
73 function cloneEnvironment(): typeof ESTraverse;
74
75 type NodeType =
76 | "AssignmentExpression"
77 | "AssignmentPattern"
78 | "ArrayExpression"
79 | "ArrayPattern"
80 | "ArrowFunctionExpression"
81 | "AwaitExpression"
82 | "BlockStatement"
83 | "BinaryExpression"
84 | "BreakStatement"
85 | "CallExpression"
86 | "CatchClause"
87 | "ClassBody"
88 | "ClassDeclaration"
89 | "ClassExpression"
90 | "ComprehensionBlock"
91 | "ComprehensionExpression"
92 | "ConditionalExpression"
93 | "ContinueStatement"
94 | "DebuggerStatement"
95 | "DirectiveStatement"
96 | "DoWhileStatement"
97 | "EmptyStatement"
98 | "ExportAllDeclaration"
99 | "ExportDefaultDeclaration"
100 | "ExportNamedDeclaration"
101 | "ExportSpecifier"
102 | "ExpressionStatement"
103 | "ForStatement"
104 | "ForInStatement"
105 | "ForOfStatement"
106 | "FunctionDeclaration"
107 | "FunctionExpression"
108 | "GeneratorExpression"
109 | "Identifier"
110 | "IfStatement"
111 | "ImportExpression"
112 | "ImportDeclaration"
113 | "ImportDefaultSpecifier"
114 | "ImportNamespaceSpecifier"
115 | "ImportSpecifier"
116 | "Literal"
117 | "LabeledStatement"
118 | "LogicalExpression"
119 | "MemberExpression"
120 | "MetaProperty"
121 | "MethodDefinition"
122 | "ModuleSpecifier"
123 | "NewExpression"
124 | "ObjectExpression"
125 | "ObjectPattern"
126 | "Program"
127 | "Property"
128 | "RestElement"
129 | "ReturnStatement"
130 | "SequenceExpression"
131 | "SpreadElement"
132 | "Super"
133 | "SwitchStatement"
134 | "SwitchCase"
135 | "TaggedTemplateExpression"
136 | "TemplateElement"
137 | "TemplateLiteral"
138 | "ThisExpression"
139 | "ThrowStatement"
140 | "TryStatement"
141 | "UnaryExpression"
142 | "UpdateExpression"
143 | "VariableDeclaration"
144 | "VariableDeclarator"
145 | "WhileStatement"
146 | "WithStatement"
147 | "YieldExpression";
148
149 interface Syntax extends Record<NodeType, NodeType> {}
150
151 interface VisitorKeys extends Record<NodeType, string[]> {}
152
153 interface Visitor {
154 enter?:
155 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
156 | ((this: Controller, node: ESTree.Node, parent: ESTree.Node | null) => VisitorOption | ESTree.Node | void)
157 | undefined;
158
159 leave?:
160 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
161 | ((this: Controller, node: ESTree.Node, parent: ESTree.Node | null) => VisitorOption | ESTree.Node | void)
162 | undefined;
163
164 fallback?: "iteration" | ((this: Controller, node: ESTree.Node) => string[]) | undefined;
165
166 keys?: Record<string, string[]> | undefined;
167 }
168}
169
170// eslint-disable-next-line @definitelytyped/export-just-namespace
171export = ESTraverse;
172
\No newline at end of file