UNPKG

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