UNPKG

13 kBTypeScriptView Raw
1// Babel 7 doesn't have typings yet. These are minimal and temporary
2
3declare module '@babel/core';
4
5declare module '@babel/plugin-syntax-import-meta';
6
7declare module '@babel/plugin-syntax-dynamic-import';
8
9declare module '@babel/helper-plugin-utils';
10
11declare module '@babel/template';
12
13declare module '@babel/traverse' {
14 import * as t from 'babel-types';
15 export type Node = t.Node;
16
17 export class Scope {
18 constructor(path: NodePath, parentScope?: Scope);
19 path: NodePath;
20 block: Node;
21 parentBlock: Node;
22 parent: Scope;
23 // hub: Hub;
24 bindings: {[name: string]: Binding;};
25
26 /** Generate a unique identifier and add it to the current scope. */
27 generateDeclaredUidIdentifier(name?: string): t.Identifier;
28
29 /** Generate a unique identifier. */
30 generateUidIdentifier(name?: string): t.Identifier;
31
32 /** Generate a unique `_id1` binding. */
33 generateUid(name?: string): string;
34
35 /** Walks the scope tree and gathers **all** bindings. */
36 getAllBindings(...kinds: string[]): object;
37 }
38
39 export class Binding {
40 constructor(opts: {
41 existing: Binding; identifier: t.Identifier; scope: Scope; path: NodePath;
42 kind: 'var' | 'let' | 'const';
43 });
44 identifier: t.Identifier;
45 scope: Scope;
46 path: NodePath;
47 kind: 'var'|'let'|'const'|'module';
48 referenced: boolean;
49 references: number;
50 referencePaths: NodePath[];
51 constant: boolean;
52 constantViolations: NodePath[];
53 }
54
55 export class NodePath<T = Node> {
56 node: T;
57 scope: Scope;
58
59 traverse(visitor: Visitor, state?: any): void;
60
61 // ------------------------- replacement -------------------------
62 /**
63 * Replace a node with an array of multiple. This method performs the
64 * following steps:
65 *
66 * - Inherit the comments of first provided node with that of the current
67 * node.
68 * - Insert the provided nodes after the current node.
69 * - Remove the current node.
70 */
71 replaceWithMultiple(nodes: Node[]): void;
72
73 /**
74 * Parse a string as an expression and replace the current node with the
75 * result.
76 *
77 * NOTE: This is typically not a good idea to use. Building source strings
78 * when transforming ASTs is an antipattern and SHOULD NOT be encouraged.
79 * Even if it's easier to use, your transforms will be extremely brittle.
80 */
81 replaceWithSourceString(replacement: any): void;
82
83 /** Replace the current node with another. */
84 replaceWith(replacement: Node|NodePath): void;
85
86 /**
87 * This method takes an array of statements nodes and then explodes it
88 * into expressions. This method retains completion records which is
89 * extremely important to retain original semantics.
90 */
91 replaceExpressionWithStatements(nodes: Node[]): Node;
92
93 replaceInline(nodes: Node|Node[]): void;
94 }
95
96
97 // The Visitor has to be generic because babel binds `this` for each property.
98 // `this` is usually used in babel plugins to pass plugin state from
99 // `pre` -> `visitor` -> `post`. An example of this can be seen in the
100 // official babel handbook:
101 // https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#-pre-and-post-in-plugins
102 export interface Visitor<S = Node> extends VisitNodeObject<Node> {
103 ArrayExpression?: VisitNode<S, t.ArrayExpression>;
104 AssignmentExpression?: VisitNode<S, t.AssignmentExpression>;
105 LVal?: VisitNode<S, t.LVal>;
106 Expression?: VisitNode<S, t.Expression>;
107 BinaryExpression?: VisitNode<S, t.BinaryExpression>;
108 Directive?: VisitNode<S, t.Directive>;
109 DirectiveLiteral?: VisitNode<S, t.DirectiveLiteral>;
110 BlockStatement?: VisitNode<S, t.BlockStatement>;
111 BreakStatement?: VisitNode<S, t.BreakStatement>;
112 Identifier?: VisitNode<S, t.Identifier>;
113 CallExpression?: VisitNode<S, t.CallExpression>;
114 CatchClause?: VisitNode<S, t.CatchClause>;
115 ConditionalExpression?: VisitNode<S, t.ConditionalExpression>;
116 ContinueStatement?: VisitNode<S, t.ContinueStatement>;
117 DebuggerStatement?: VisitNode<S, t.DebuggerStatement>;
118 DoWhileStatement?: VisitNode<S, t.DoWhileStatement>;
119 Statement?: VisitNode<S, t.Statement>;
120 EmptyStatement?: VisitNode<S, t.EmptyStatement>;
121 ExpressionStatement?: VisitNode<S, t.ExpressionStatement>;
122 File?: VisitNode<S, t.File>;
123 Program?: VisitNode<S, t.Program>;
124 ForInStatement?: VisitNode<S, t.ForInStatement>;
125 VariableDeclaration?: VisitNode<S, t.VariableDeclaration>;
126 ForStatement?: VisitNode<S, t.ForStatement>;
127 FunctionDeclaration?: VisitNode<S, t.FunctionDeclaration>;
128 FunctionExpression?: VisitNode<S, t.FunctionExpression>;
129 IfStatement?: VisitNode<S, t.IfStatement>;
130 LabeledStatement?: VisitNode<S, t.LabeledStatement>;
131 StringLiteral?: VisitNode<S, t.StringLiteral>;
132 NumericLiteral?: VisitNode<S, t.NumericLiteral>;
133 NullLiteral?: VisitNode<S, t.NullLiteral>;
134 BooleanLiteral?: VisitNode<S, t.BooleanLiteral>;
135 RegExpLiteral?: VisitNode<S, t.RegExpLiteral>;
136 LogicalExpression?: VisitNode<S, t.LogicalExpression>;
137 MemberExpression?: VisitNode<S, t.MemberExpression>;
138 NewExpression?: VisitNode<S, t.NewExpression>;
139 ObjectExpression?: VisitNode<S, t.ObjectExpression>;
140 ObjectMethod?: VisitNode<S, t.ObjectMethod>;
141 ObjectProperty?: VisitNode<S, t.ObjectProperty>;
142 RestElement?: VisitNode<S, t.RestElement>;
143 ReturnStatement?: VisitNode<S, t.ReturnStatement>;
144 SequenceExpression?: VisitNode<S, t.SequenceExpression>;
145 SwitchCase?: VisitNode<S, t.SwitchCase>;
146 SwitchStatement?: VisitNode<S, t.SwitchStatement>;
147 ThisExpression?: VisitNode<S, t.ThisExpression>;
148 ThrowStatement?: VisitNode<S, t.ThrowStatement>;
149 TryStatement?: VisitNode<S, t.TryStatement>;
150 UnaryExpression?: VisitNode<S, t.UnaryExpression>;
151 UpdateExpression?: VisitNode<S, t.UpdateExpression>;
152 VariableDeclarator?: VisitNode<S, t.VariableDeclarator>;
153 WhileStatement?: VisitNode<S, t.WhileStatement>;
154 WithStatement?: VisitNode<S, t.WithStatement>;
155 AssignmentPattern?: VisitNode<S, t.AssignmentPattern>;
156 ArrayPattern?: VisitNode<S, t.ArrayPattern>;
157 ArrowFunctionExpression?: VisitNode<S, t.ArrowFunctionExpression>;
158 ClassBody?: VisitNode<S, t.ClassBody>;
159 ClassDeclaration?: VisitNode<S, t.ClassDeclaration>;
160 ClassExpression?: VisitNode<S, t.ClassExpression>;
161 ExportAllDeclaration?: VisitNode<S, t.ExportAllDeclaration>;
162 ExportDefaultDeclaration?: VisitNode<S, t.ExportDefaultDeclaration>;
163 ExportNamedDeclaration?: VisitNode<S, t.ExportNamedDeclaration>;
164 Declaration?: VisitNode<S, t.Declaration>;
165 ExportSpecifier?: VisitNode<S, t.ExportSpecifier>;
166 ForOfStatement?: VisitNode<S, t.ForOfStatement>;
167 ImportDeclaration?: VisitNode<S, t.ImportDeclaration>;
168 ImportDefaultSpecifier?: VisitNode<S, t.ImportDefaultSpecifier>;
169 ImportNamespaceSpecifier?: VisitNode<S, t.ImportNamespaceSpecifier>;
170 ImportSpecifier?: VisitNode<S, t.ImportSpecifier>;
171 MetaProperty?: VisitNode<S, t.MetaProperty>;
172 ClassMethod?: VisitNode<S, t.ClassMethod>;
173 ObjectPattern?: VisitNode<S, t.ObjectPattern>;
174 SpreadElement?: VisitNode<S, t.SpreadElement>;
175 Super?: VisitNode<S, t.Super>;
176 TaggedTemplateExpression?: VisitNode<S, t.TaggedTemplateExpression>;
177 TemplateLiteral?: VisitNode<S, t.TemplateLiteral>;
178 TemplateElement?: VisitNode<S, t.TemplateElement>;
179 YieldExpression?: VisitNode<S, t.YieldExpression>;
180 AnyTypeAnnotation?: VisitNode<S, t.AnyTypeAnnotation>;
181 ArrayTypeAnnotation?: VisitNode<S, t.ArrayTypeAnnotation>;
182 BooleanTypeAnnotation?: VisitNode<S, t.BooleanTypeAnnotation>;
183 BooleanLiteralTypeAnnotation?: VisitNode<S, t.BooleanLiteralTypeAnnotation>;
184 NullLiteralTypeAnnotation?: VisitNode<S, t.NullLiteralTypeAnnotation>;
185 ClassImplements?: VisitNode<S, t.ClassImplements>;
186 ClassProperty?: VisitNode<S, t.ClassProperty>;
187 DeclareClass?: VisitNode<S, t.DeclareClass>;
188 DeclareFunction?: VisitNode<S, t.DeclareFunction>;
189 DeclareInterface?: VisitNode<S, t.DeclareInterface>;
190 DeclareModule?: VisitNode<S, t.DeclareModule>;
191 DeclareTypeAlias?: VisitNode<S, t.DeclareTypeAlias>;
192 DeclareVariable?: VisitNode<S, t.DeclareVariable>;
193 ExistentialTypeParam?: VisitNode<S, t.ExistentialTypeParam>;
194 FunctionTypeAnnotation?: VisitNode<S, t.FunctionTypeAnnotation>;
195 FunctionTypeParam?: VisitNode<S, t.FunctionTypeParam>;
196 GenericTypeAnnotation?: VisitNode<S, t.GenericTypeAnnotation>;
197 InterfaceExtends?: VisitNode<S, t.InterfaceExtends>;
198 InterfaceDeclaration?: VisitNode<S, t.InterfaceDeclaration>;
199 IntersectionTypeAnnotation?: VisitNode<S, t.IntersectionTypeAnnotation>;
200 MixedTypeAnnotation?: VisitNode<S, t.MixedTypeAnnotation>;
201 NullableTypeAnnotation?: VisitNode<S, t.NullableTypeAnnotation>;
202 NumericLiteralTypeAnnotation?: VisitNode<S, t.NumericLiteralTypeAnnotation>;
203 NumberTypeAnnotation?: VisitNode<S, t.NumberTypeAnnotation>;
204 StringLiteralTypeAnnotation?: VisitNode<S, t.StringLiteralTypeAnnotation>;
205 StringTypeAnnotation?: VisitNode<S, t.StringTypeAnnotation>;
206 ThisTypeAnnotation?: VisitNode<S, t.ThisTypeAnnotation>;
207 TupleTypeAnnotation?: VisitNode<S, t.TupleTypeAnnotation>;
208 TypeofTypeAnnotation?: VisitNode<S, t.TypeofTypeAnnotation>;
209 TypeAlias?: VisitNode<S, t.TypeAlias>;
210 TypeAnnotation?: VisitNode<S, t.TypeAnnotation>;
211 TypeCastExpression?: VisitNode<S, t.TypeCastExpression>;
212 TypeParameterDeclaration?: VisitNode<S, t.TypeParameterDeclaration>;
213 TypeParameterInstantiation?: VisitNode<S, t.TypeParameterInstantiation>;
214 ObjectTypeAnnotation?: VisitNode<S, t.ObjectTypeAnnotation>;
215 ObjectTypeCallProperty?: VisitNode<S, t.ObjectTypeCallProperty>;
216 ObjectTypeIndexer?: VisitNode<S, t.ObjectTypeIndexer>;
217 ObjectTypeProperty?: VisitNode<S, t.ObjectTypeProperty>;
218 QualifiedTypeIdentifier?: VisitNode<S, t.QualifiedTypeIdentifier>;
219 UnionTypeAnnotation?: VisitNode<S, t.UnionTypeAnnotation>;
220 VoidTypeAnnotation?: VisitNode<S, t.VoidTypeAnnotation>;
221 JSXAttribute?: VisitNode<S, t.JSXAttribute>;
222 JSXIdentifier?: VisitNode<S, t.JSXIdentifier>;
223 JSXNamespacedName?: VisitNode<S, t.JSXNamespacedName>;
224 JSXElement?: VisitNode<S, t.JSXElement>;
225 JSXExpressionContainer?: VisitNode<S, t.JSXExpressionContainer>;
226 JSXClosingElement?: VisitNode<S, t.JSXClosingElement>;
227 JSXMemberExpression?: VisitNode<S, t.JSXMemberExpression>;
228 JSXOpeningElement?: VisitNode<S, t.JSXOpeningElement>;
229 JSXEmptyExpression?: VisitNode<S, t.JSXEmptyExpression>;
230 JSXSpreadAttribute?: VisitNode<S, t.JSXSpreadAttribute>;
231 JSXText?: VisitNode<S, t.JSXText>;
232 Noop?: VisitNode<S, t.Noop>;
233 ParenthesizedExpression?: VisitNode<S, t.ParenthesizedExpression>;
234 AwaitExpression?: VisitNode<S, t.AwaitExpression>;
235 BindExpression?: VisitNode<S, t.BindExpression>;
236 Decorator?: VisitNode<S, t.Decorator>;
237 DoExpression?: VisitNode<S, t.DoExpression>;
238 ExportDefaultSpecifier?: VisitNode<S, t.ExportDefaultSpecifier>;
239 ExportNamespaceSpecifier?: VisitNode<S, t.ExportNamespaceSpecifier>;
240 RestProperty?: VisitNode<S, t.RestProperty>;
241 SpreadProperty?: VisitNode<S, t.SpreadProperty>;
242 Binary?: VisitNode<S, t.Binary>;
243 Scopable?: VisitNode<S, t.Scopable>;
244 BlockParent?: VisitNode<S, t.BlockParent>;
245 Block?: VisitNode<S, t.Block>;
246 Terminatorless?: VisitNode<S, t.Terminatorless>;
247 CompletionStatement?: VisitNode<S, t.CompletionStatement>;
248 Conditional?: VisitNode<S, t.Conditional>;
249 Loop?: VisitNode<S, t.Loop>;
250 While?: VisitNode<S, t.While>;
251 ExpressionWrapper?: VisitNode<S, t.ExpressionWrapper>;
252 For?: VisitNode<S, t.For>;
253 ForXStatement?: VisitNode<S, t.ForXStatement>;
254 Function?: VisitNode<S, t.Function>;
255 FunctionParent?: VisitNode<S, t.FunctionParent>;
256 Pureish?: VisitNode<S, t.Pureish>;
257 Literal?: VisitNode<S, t.Literal>;
258 Immutable?: VisitNode<S, t.Immutable>;
259 UserWhitespacable?: VisitNode<S, t.UserWhitespacable>;
260 Method?: VisitNode<S, t.Method>;
261 ObjectMember?: VisitNode<S, t.ObjectMember>;
262 Property?: VisitNode<S, t.Property>;
263 UnaryLike?: VisitNode<S, t.UnaryLike>;
264 Pattern?: VisitNode<S, t.Pattern>;
265 Class?: VisitNode<S, t.Class>;
266 ModuleDeclaration?: VisitNode<S, t.ModuleDeclaration>;
267 ExportDeclaration?: VisitNode<S, t.ExportDeclaration>;
268 ModuleSpecifier?: VisitNode<S, t.ModuleSpecifier>;
269 Flow?: VisitNode<S, t.Flow>;
270 FlowBaseAnnotation?: VisitNode<S, t.FlowBaseAnnotation>;
271 FlowDeclaration?: VisitNode<S, t.FlowDeclaration>;
272 JSX?: VisitNode<S, t.JSX>;
273 Scope?: VisitNode<S, t.Scopable>;
274 }
275
276 export type VisitNode<T, P> = VisitNodeFunction<T, P>|VisitNodeObject<T>;
277
278 export type VisitNodeFunction<T, P> =
279 (this: T, path: NodePath<P>, state: any) => void;
280
281 export interface VisitNodeObject<T> {
282 enter?(path: NodePath<T>, state: any): void;
283 exit?(path: NodePath<T>, state: any): void;
284 }
285}