UNPKG

16.7 kBTypeScriptView Raw
1// Type definitions for ESTree AST specification
2// Project: https://github.com/estree/estree
3// Definitions by: RReverser <https://github.com/RReverser>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6// This definition file follows a somewhat unusual format. ESTree allows
7// runtime type checks based on the `type` parameter. In order to explain this
8// to typescript we want to use discriminated union types:
9// https://github.com/Microsoft/TypeScript/pull/9163
10//
11// For ESTree this is a bit tricky because the high level interfaces like
12// Node or Function are pulling double duty. We want to pass common fields down
13// to the interfaces that extend them (like Identifier or
14// ArrowFunctionExpression), but you can't extend a type union or enforce
15// common fields on them. So we've split the high level interfaces into two
16// types, a base type which passes down inherited fields, and a type union of
17// all types which extend the base type. Only the type union is exported, and
18// the union is how other types refer to the collection of inheriting types.
19//
20// This makes the definitions file here somewhat more difficult to maintain,
21// but it has the notable advantage of making ESTree much easier to use as
22// an end user.
23
24interface BaseNodeWithoutComments {
25 // Every leaf interface that extends BaseNode must specify a type property.
26 // The type property should be a string literal. For example, Identifier
27 // has: `type: "Identifier"`
28 type: string;
29 loc?: SourceLocation | null | undefined;
30 range?: [number, number] | undefined;
31}
32
33interface BaseNode extends BaseNodeWithoutComments {
34 leadingComments?: Array<Comment> | undefined;
35 trailingComments?: Array<Comment> | undefined;
36}
37
38export type Node =
39 Identifier | Literal | Program | Function | SwitchCase | CatchClause |
40 VariableDeclarator | Statement | Expression | PrivateIdentifier | Property | PropertyDefinition |
41 AssignmentProperty | Super | TemplateElement | SpreadElement | Pattern |
42 ClassBody | Class | MethodDefinition | ModuleDeclaration | ModuleSpecifier;
43
44export interface Comment extends BaseNodeWithoutComments {
45 type: "Line" | "Block";
46 value: string;
47}
48
49interface SourceLocation {
50 source?: string | null | undefined;
51 start: Position;
52 end: Position;
53}
54
55export interface Position {
56 /** >= 1 */
57 line: number;
58 /** >= 0 */
59 column: number;
60}
61
62export interface Program extends BaseNode {
63 type: "Program";
64 sourceType: "script" | "module";
65 body: Array<Directive | Statement | ModuleDeclaration>;
66 comments?: Array<Comment> | undefined;
67}
68
69export interface Directive extends BaseNode {
70 type: "ExpressionStatement";
71 expression: Literal;
72 directive: string;
73}
74
75interface BaseFunction extends BaseNode {
76 params: Array<Pattern>;
77 generator?: boolean | undefined;
78 async?: boolean | undefined;
79 // The body is either BlockStatement or Expression because arrow functions
80 // can have a body that's either. FunctionDeclarations and
81 // FunctionExpressions have only BlockStatement bodies.
82 body: BlockStatement | Expression;
83}
84
85export type Function =
86 FunctionDeclaration | FunctionExpression | ArrowFunctionExpression;
87
88export type Statement =
89 ExpressionStatement | BlockStatement | EmptyStatement |
90 DebuggerStatement | WithStatement | ReturnStatement | LabeledStatement |
91 BreakStatement | ContinueStatement | IfStatement | SwitchStatement |
92 ThrowStatement | TryStatement | WhileStatement | DoWhileStatement |
93 ForStatement | ForInStatement | ForOfStatement | Declaration;
94
95interface BaseStatement extends BaseNode { }
96
97export interface EmptyStatement extends BaseStatement {
98 type: "EmptyStatement";
99}
100
101export interface BlockStatement extends BaseStatement {
102 type: "BlockStatement";
103 body: Array<Statement>;
104 innerComments?: Array<Comment> | undefined;
105}
106
107export interface ExpressionStatement extends BaseStatement {
108 type: "ExpressionStatement";
109 expression: Expression;
110}
111
112export interface IfStatement extends BaseStatement {
113 type: "IfStatement";
114 test: Expression;
115 consequent: Statement;
116 alternate?: Statement | null | undefined;
117}
118
119export interface LabeledStatement extends BaseStatement {
120 type: "LabeledStatement";
121 label: Identifier;
122 body: Statement;
123}
124
125export interface BreakStatement extends BaseStatement {
126 type: "BreakStatement";
127 label?: Identifier | null | undefined;
128}
129
130export interface ContinueStatement extends BaseStatement {
131 type: "ContinueStatement";
132 label?: Identifier | null | undefined;
133}
134
135export interface WithStatement extends BaseStatement {
136 type: "WithStatement";
137 object: Expression;
138 body: Statement;
139}
140
141export interface SwitchStatement extends BaseStatement {
142 type: "SwitchStatement";
143 discriminant: Expression;
144 cases: Array<SwitchCase>;
145}
146
147export interface ReturnStatement extends BaseStatement {
148 type: "ReturnStatement";
149 argument?: Expression | null | undefined;
150}
151
152export interface ThrowStatement extends BaseStatement {
153 type: "ThrowStatement";
154 argument: Expression;
155}
156
157export interface TryStatement extends BaseStatement {
158 type: "TryStatement";
159 block: BlockStatement;
160 handler?: CatchClause | null | undefined;
161 finalizer?: BlockStatement | null | undefined;
162}
163
164export interface WhileStatement extends BaseStatement {
165 type: "WhileStatement";
166 test: Expression;
167 body: Statement;
168}
169
170export interface DoWhileStatement extends BaseStatement {
171 type: "DoWhileStatement";
172 body: Statement;
173 test: Expression;
174}
175
176export interface ForStatement extends BaseStatement {
177 type: "ForStatement";
178 init?: VariableDeclaration | Expression | null | undefined;
179 test?: Expression | null | undefined;
180 update?: Expression | null | undefined;
181 body: Statement;
182}
183
184interface BaseForXStatement extends BaseStatement {
185 left: VariableDeclaration | Pattern;
186 right: Expression;
187 body: Statement;
188}
189
190export interface ForInStatement extends BaseForXStatement {
191 type: "ForInStatement";
192}
193
194export interface DebuggerStatement extends BaseStatement {
195 type: "DebuggerStatement";
196}
197
198export type Declaration =
199 FunctionDeclaration | VariableDeclaration | ClassDeclaration;
200
201interface BaseDeclaration extends BaseStatement { }
202
203export interface FunctionDeclaration extends BaseFunction, BaseDeclaration {
204 type: "FunctionDeclaration";
205 /** It is null when a function declaration is a part of the `export default function` statement */
206 id: Identifier | null;
207 body: BlockStatement;
208}
209
210export interface VariableDeclaration extends BaseDeclaration {
211 type: "VariableDeclaration";
212 declarations: Array<VariableDeclarator>;
213 kind: "var" | "let" | "const";
214}
215
216export interface VariableDeclarator extends BaseNode {
217 type: "VariableDeclarator";
218 id: Pattern;
219 init?: Expression | null | undefined;
220}
221
222type Expression =
223 ThisExpression | ArrayExpression | ObjectExpression | FunctionExpression |
224 ArrowFunctionExpression | YieldExpression | Literal | UnaryExpression |
225 UpdateExpression | BinaryExpression | AssignmentExpression |
226 LogicalExpression | MemberExpression | ConditionalExpression |
227 CallExpression | NewExpression | SequenceExpression | TemplateLiteral |
228 TaggedTemplateExpression | ClassExpression | MetaProperty | Identifier |
229 AwaitExpression | ImportExpression | ChainExpression;
230
231export interface BaseExpression extends BaseNode { }
232
233type ChainElement = SimpleCallExpression | MemberExpression;
234
235export interface ChainExpression extends BaseExpression {
236 type: "ChainExpression";
237 expression: ChainElement;
238}
239
240export interface ThisExpression extends BaseExpression {
241 type: "ThisExpression";
242}
243
244export interface ArrayExpression extends BaseExpression {
245 type: "ArrayExpression";
246 elements: Array<Expression | SpreadElement | null>;
247}
248
249export interface ObjectExpression extends BaseExpression {
250 type: "ObjectExpression";
251 properties: Array<Property | SpreadElement>;
252}
253
254export interface PrivateIdentifier extends BaseNode {
255 type: "PrivateIdentifier";
256 name: string;
257}
258
259export interface Property extends BaseNode {
260 type: "Property";
261 key: Expression | PrivateIdentifier;
262 value: Expression | Pattern; // Could be an AssignmentProperty
263 kind: "init" | "get" | "set";
264 method: boolean;
265 shorthand: boolean;
266 computed: boolean;
267}
268
269export interface PropertyDefinition extends BaseNode {
270 type: "PropertyDefinition";
271 key: Expression | PrivateIdentifier;
272 value?: Expression | null | undefined;
273 computed: boolean;
274 static: boolean;
275}
276
277export interface FunctionExpression extends BaseFunction, BaseExpression {
278 id?: Identifier | null | undefined;
279 type: "FunctionExpression";
280 body: BlockStatement;
281}
282
283export interface SequenceExpression extends BaseExpression {
284 type: "SequenceExpression";
285 expressions: Array<Expression>;
286}
287
288export interface UnaryExpression extends BaseExpression {
289 type: "UnaryExpression";
290 operator: UnaryOperator;
291 prefix: true;
292 argument: Expression;
293}
294
295export interface BinaryExpression extends BaseExpression {
296 type: "BinaryExpression";
297 operator: BinaryOperator;
298 left: Expression;
299 right: Expression;
300}
301
302export interface AssignmentExpression extends BaseExpression {
303 type: "AssignmentExpression";
304 operator: AssignmentOperator;
305 left: Pattern | MemberExpression;
306 right: Expression;
307}
308
309export interface UpdateExpression extends BaseExpression {
310 type: "UpdateExpression";
311 operator: UpdateOperator;
312 argument: Expression;
313 prefix: boolean;
314}
315
316export interface LogicalExpression extends BaseExpression {
317 type: "LogicalExpression";
318 operator: LogicalOperator;
319 left: Expression;
320 right: Expression;
321}
322
323export interface ConditionalExpression extends BaseExpression {
324 type: "ConditionalExpression";
325 test: Expression;
326 alternate: Expression;
327 consequent: Expression;
328}
329
330interface BaseCallExpression extends BaseExpression {
331 callee: Expression | Super;
332 arguments: Array<Expression | SpreadElement>;
333}
334export type CallExpression = SimpleCallExpression | NewExpression;
335
336export interface SimpleCallExpression extends BaseCallExpression {
337 type: "CallExpression";
338 optional: boolean;
339}
340
341export interface NewExpression extends BaseCallExpression {
342 type: "NewExpression";
343}
344
345export interface MemberExpression extends BaseExpression, BasePattern {
346 type: "MemberExpression";
347 object: Expression | Super;
348 property: Expression | PrivateIdentifier;
349 computed: boolean;
350 optional: boolean;
351}
352
353export type Pattern =
354 Identifier | ObjectPattern | ArrayPattern | RestElement |
355 AssignmentPattern | MemberExpression;
356
357interface BasePattern extends BaseNode { }
358
359export interface SwitchCase extends BaseNode {
360 type: "SwitchCase";
361 test?: Expression | null | undefined;
362 consequent: Array<Statement>;
363}
364
365export interface CatchClause extends BaseNode {
366 type: "CatchClause";
367 param: Pattern | null;
368 body: BlockStatement;
369}
370
371export interface Identifier extends BaseNode, BaseExpression, BasePattern {
372 type: "Identifier";
373 name: string;
374}
375
376export type Literal = SimpleLiteral | RegExpLiteral | BigIntLiteral;
377
378export interface SimpleLiteral extends BaseNode, BaseExpression {
379 type: "Literal";
380 value: string | boolean | number | null;
381 raw?: string | undefined;
382}
383
384export interface RegExpLiteral extends BaseNode, BaseExpression {
385 type: "Literal";
386 value?: RegExp | null | undefined;
387 regex: {
388 pattern: string;
389 flags: string;
390 };
391 raw?: string | undefined;
392}
393
394export interface BigIntLiteral extends BaseNode, BaseExpression {
395 type: "Literal";
396 value?: bigint | null | undefined;
397 bigint: string;
398 raw?: string | undefined;
399}
400
401export type UnaryOperator =
402 "-" | "+" | "!" | "~" | "typeof" | "void" | "delete";
403
404export type BinaryOperator =
405 "==" | "!=" | "===" | "!==" | "<" | "<=" | ">" | ">=" | "<<" |
406 ">>" | ">>>" | "+" | "-" | "*" | "/" | "%" | "**" | "|" | "^" | "&" | "in" |
407 "instanceof";
408
409export type LogicalOperator = "||" | "&&" | "??";
410
411export type AssignmentOperator =
412 "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "**=" | "<<=" | ">>=" | ">>>=" |
413 "|=" | "^=" | "&=";
414
415export type UpdateOperator = "++" | "--";
416
417export interface ForOfStatement extends BaseForXStatement {
418 type: "ForOfStatement";
419 await: boolean;
420}
421
422export interface Super extends BaseNode {
423 type: "Super";
424}
425
426export interface SpreadElement extends BaseNode {
427 type: "SpreadElement";
428 argument: Expression;
429}
430
431export interface ArrowFunctionExpression extends BaseExpression, BaseFunction {
432 type: "ArrowFunctionExpression";
433 expression: boolean;
434 body: BlockStatement | Expression;
435}
436
437export interface YieldExpression extends BaseExpression {
438 type: "YieldExpression";
439 argument?: Expression | null | undefined;
440 delegate: boolean;
441}
442
443export interface TemplateLiteral extends BaseExpression {
444 type: "TemplateLiteral";
445 quasis: Array<TemplateElement>;
446 expressions: Array<Expression>;
447}
448
449export interface TaggedTemplateExpression extends BaseExpression {
450 type: "TaggedTemplateExpression";
451 tag: Expression;
452 quasi: TemplateLiteral;
453}
454
455export interface TemplateElement extends BaseNode {
456 type: "TemplateElement";
457 tail: boolean;
458 value: {
459 /** It is null when the template literal is tagged and the text has an invalid escape (e.g. - tag`\unicode and \u{55}`) */
460 cooked?: string | null | undefined;
461 raw: string;
462 };
463}
464
465export interface AssignmentProperty extends Property {
466 value: Pattern;
467 kind: "init";
468 method: boolean; // false
469}
470
471export interface ObjectPattern extends BasePattern {
472 type: "ObjectPattern";
473 properties: Array<AssignmentProperty | RestElement>;
474}
475
476export interface ArrayPattern extends BasePattern {
477 type: "ArrayPattern";
478 elements: Array<Pattern | null>;
479}
480
481export interface RestElement extends BasePattern {
482 type: "RestElement";
483 argument: Pattern;
484}
485
486export interface AssignmentPattern extends BasePattern {
487 type: "AssignmentPattern";
488 left: Pattern;
489 right: Expression;
490}
491
492export type Class = ClassDeclaration | ClassExpression;
493interface BaseClass extends BaseNode {
494 superClass?: Expression | null | undefined;
495 body: ClassBody;
496}
497
498export interface ClassBody extends BaseNode {
499 type: "ClassBody";
500 body: Array<MethodDefinition | PropertyDefinition>;
501}
502
503export interface MethodDefinition extends BaseNode {
504 type: "MethodDefinition";
505 key: Expression | PrivateIdentifier;
506 value: FunctionExpression;
507 kind: "constructor" | "method" | "get" | "set";
508 computed: boolean;
509 static: boolean;
510}
511
512export interface ClassDeclaration extends BaseClass, BaseDeclaration {
513 type: "ClassDeclaration";
514 /** It is null when a class declaration is a part of the `export default class` statement */
515 id: Identifier | null;
516}
517
518export interface ClassExpression extends BaseClass, BaseExpression {
519 type: "ClassExpression";
520 id?: Identifier | null | undefined;
521}
522
523export interface MetaProperty extends BaseExpression {
524 type: "MetaProperty";
525 meta: Identifier;
526 property: Identifier;
527}
528
529export type ModuleDeclaration =
530 ImportDeclaration | ExportNamedDeclaration | ExportDefaultDeclaration |
531 ExportAllDeclaration;
532interface BaseModuleDeclaration extends BaseNode { }
533
534export type ModuleSpecifier =
535 ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier |
536 ExportSpecifier;
537interface BaseModuleSpecifier extends BaseNode {
538 local: Identifier;
539}
540
541export interface ImportDeclaration extends BaseModuleDeclaration {
542 type: "ImportDeclaration";
543 specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>;
544 source: Literal;
545}
546
547export interface ImportSpecifier extends BaseModuleSpecifier {
548 type: "ImportSpecifier";
549 imported: Identifier;
550}
551
552export interface ImportExpression extends BaseExpression {
553 type: "ImportExpression";
554 source: Expression;
555}
556
557export interface ImportDefaultSpecifier extends BaseModuleSpecifier {
558 type: "ImportDefaultSpecifier";
559}
560
561export interface ImportNamespaceSpecifier extends BaseModuleSpecifier {
562 type: "ImportNamespaceSpecifier";
563}
564
565export interface ExportNamedDeclaration extends BaseModuleDeclaration {
566 type: "ExportNamedDeclaration";
567 declaration?: Declaration | null | undefined;
568 specifiers: Array<ExportSpecifier>;
569 source?: Literal | null | undefined;
570}
571
572export interface ExportSpecifier extends BaseModuleSpecifier {
573 type: "ExportSpecifier";
574 exported: Identifier;
575}
576
577export interface ExportDefaultDeclaration extends BaseModuleDeclaration {
578 type: "ExportDefaultDeclaration";
579 declaration: Declaration | Expression;
580}
581
582export interface ExportAllDeclaration extends BaseModuleDeclaration {
583 type: "ExportAllDeclaration";
584 exported: Identifier | null;
585 source: Literal;
586}
587
588export interface AwaitExpression extends BaseExpression {
589 type: "AwaitExpression";
590 argument: Expression;
591}