UNPKG

16.8 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 | StaticBlock | 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 StaticBlock extends Omit<BlockStatement, 'type'> {
108 type: "StaticBlock";
109}
110
111export interface ExpressionStatement extends BaseStatement {
112 type: "ExpressionStatement";
113 expression: Expression;
114}
115
116export interface IfStatement extends BaseStatement {
117 type: "IfStatement";
118 test: Expression;
119 consequent: Statement;
120 alternate?: Statement | null | undefined;
121}
122
123export interface LabeledStatement extends BaseStatement {
124 type: "LabeledStatement";
125 label: Identifier;
126 body: Statement;
127}
128
129export interface BreakStatement extends BaseStatement {
130 type: "BreakStatement";
131 label?: Identifier | null | undefined;
132}
133
134export interface ContinueStatement extends BaseStatement {
135 type: "ContinueStatement";
136 label?: Identifier | null | undefined;
137}
138
139export interface WithStatement extends BaseStatement {
140 type: "WithStatement";
141 object: Expression;
142 body: Statement;
143}
144
145export interface SwitchStatement extends BaseStatement {
146 type: "SwitchStatement";
147 discriminant: Expression;
148 cases: Array<SwitchCase>;
149}
150
151export interface ReturnStatement extends BaseStatement {
152 type: "ReturnStatement";
153 argument?: Expression | null | undefined;
154}
155
156export interface ThrowStatement extends BaseStatement {
157 type: "ThrowStatement";
158 argument: Expression;
159}
160
161export interface TryStatement extends BaseStatement {
162 type: "TryStatement";
163 block: BlockStatement;
164 handler?: CatchClause | null | undefined;
165 finalizer?: BlockStatement | null | undefined;
166}
167
168export interface WhileStatement extends BaseStatement {
169 type: "WhileStatement";
170 test: Expression;
171 body: Statement;
172}
173
174export interface DoWhileStatement extends BaseStatement {
175 type: "DoWhileStatement";
176 body: Statement;
177 test: Expression;
178}
179
180export interface ForStatement extends BaseStatement {
181 type: "ForStatement";
182 init?: VariableDeclaration | Expression | null | undefined;
183 test?: Expression | null | undefined;
184 update?: Expression | null | undefined;
185 body: Statement;
186}
187
188interface BaseForXStatement extends BaseStatement {
189 left: VariableDeclaration | Pattern;
190 right: Expression;
191 body: Statement;
192}
193
194export interface ForInStatement extends BaseForXStatement {
195 type: "ForInStatement";
196}
197
198export interface DebuggerStatement extends BaseStatement {
199 type: "DebuggerStatement";
200}
201
202export type Declaration =
203 FunctionDeclaration | VariableDeclaration | ClassDeclaration;
204
205interface BaseDeclaration extends BaseStatement { }
206
207export interface FunctionDeclaration extends BaseFunction, BaseDeclaration {
208 type: "FunctionDeclaration";
209 /** It is null when a function declaration is a part of the `export default function` statement */
210 id: Identifier | null;
211 body: BlockStatement;
212}
213
214export interface VariableDeclaration extends BaseDeclaration {
215 type: "VariableDeclaration";
216 declarations: Array<VariableDeclarator>;
217 kind: "var" | "let" | "const";
218}
219
220export interface VariableDeclarator extends BaseNode {
221 type: "VariableDeclarator";
222 id: Pattern;
223 init?: Expression | null | undefined;
224}
225
226type Expression =
227 ThisExpression | ArrayExpression | ObjectExpression | FunctionExpression |
228 ArrowFunctionExpression | YieldExpression | Literal | UnaryExpression |
229 UpdateExpression | BinaryExpression | AssignmentExpression |
230 LogicalExpression | MemberExpression | ConditionalExpression |
231 CallExpression | NewExpression | SequenceExpression | TemplateLiteral |
232 TaggedTemplateExpression | ClassExpression | MetaProperty | Identifier |
233 AwaitExpression | ImportExpression | ChainExpression;
234
235export interface BaseExpression extends BaseNode { }
236
237type ChainElement = SimpleCallExpression | MemberExpression;
238
239export interface ChainExpression extends BaseExpression {
240 type: "ChainExpression";
241 expression: ChainElement;
242}
243
244export interface ThisExpression extends BaseExpression {
245 type: "ThisExpression";
246}
247
248export interface ArrayExpression extends BaseExpression {
249 type: "ArrayExpression";
250 elements: Array<Expression | SpreadElement | null>;
251}
252
253export interface ObjectExpression extends BaseExpression {
254 type: "ObjectExpression";
255 properties: Array<Property | SpreadElement>;
256}
257
258export interface PrivateIdentifier extends BaseNode {
259 type: "PrivateIdentifier";
260 name: string;
261}
262
263export interface Property extends BaseNode {
264 type: "Property";
265 key: Expression | PrivateIdentifier;
266 value: Expression | Pattern; // Could be an AssignmentProperty
267 kind: "init" | "get" | "set";
268 method: boolean;
269 shorthand: boolean;
270 computed: boolean;
271}
272
273export interface PropertyDefinition extends BaseNode {
274 type: "PropertyDefinition";
275 key: Expression | PrivateIdentifier;
276 value?: Expression | null | undefined;
277 computed: boolean;
278 static: boolean;
279}
280
281export interface FunctionExpression extends BaseFunction, BaseExpression {
282 id?: Identifier | null | undefined;
283 type: "FunctionExpression";
284 body: BlockStatement;
285}
286
287export interface SequenceExpression extends BaseExpression {
288 type: "SequenceExpression";
289 expressions: Array<Expression>;
290}
291
292export interface UnaryExpression extends BaseExpression {
293 type: "UnaryExpression";
294 operator: UnaryOperator;
295 prefix: true;
296 argument: Expression;
297}
298
299export interface BinaryExpression extends BaseExpression {
300 type: "BinaryExpression";
301 operator: BinaryOperator;
302 left: Expression;
303 right: Expression;
304}
305
306export interface AssignmentExpression extends BaseExpression {
307 type: "AssignmentExpression";
308 operator: AssignmentOperator;
309 left: Pattern | MemberExpression;
310 right: Expression;
311}
312
313export interface UpdateExpression extends BaseExpression {
314 type: "UpdateExpression";
315 operator: UpdateOperator;
316 argument: Expression;
317 prefix: boolean;
318}
319
320export interface LogicalExpression extends BaseExpression {
321 type: "LogicalExpression";
322 operator: LogicalOperator;
323 left: Expression;
324 right: Expression;
325}
326
327export interface ConditionalExpression extends BaseExpression {
328 type: "ConditionalExpression";
329 test: Expression;
330 alternate: Expression;
331 consequent: Expression;
332}
333
334interface BaseCallExpression extends BaseExpression {
335 callee: Expression | Super;
336 arguments: Array<Expression | SpreadElement>;
337}
338export type CallExpression = SimpleCallExpression | NewExpression;
339
340export interface SimpleCallExpression extends BaseCallExpression {
341 type: "CallExpression";
342 optional: boolean;
343}
344
345export interface NewExpression extends BaseCallExpression {
346 type: "NewExpression";
347}
348
349export interface MemberExpression extends BaseExpression, BasePattern {
350 type: "MemberExpression";
351 object: Expression | Super;
352 property: Expression | PrivateIdentifier;
353 computed: boolean;
354 optional: boolean;
355}
356
357export type Pattern =
358 Identifier | ObjectPattern | ArrayPattern | RestElement |
359 AssignmentPattern | MemberExpression;
360
361interface BasePattern extends BaseNode { }
362
363export interface SwitchCase extends BaseNode {
364 type: "SwitchCase";
365 test?: Expression | null | undefined;
366 consequent: Array<Statement>;
367}
368
369export interface CatchClause extends BaseNode {
370 type: "CatchClause";
371 param: Pattern | null;
372 body: BlockStatement;
373}
374
375export interface Identifier extends BaseNode, BaseExpression, BasePattern {
376 type: "Identifier";
377 name: string;
378}
379
380export type Literal = SimpleLiteral | RegExpLiteral | BigIntLiteral;
381
382export interface SimpleLiteral extends BaseNode, BaseExpression {
383 type: "Literal";
384 value: string | boolean | number | null;
385 raw?: string | undefined;
386}
387
388export interface RegExpLiteral extends BaseNode, BaseExpression {
389 type: "Literal";
390 value?: RegExp | null | undefined;
391 regex: {
392 pattern: string;
393 flags: string;
394 };
395 raw?: string | undefined;
396}
397
398export interface BigIntLiteral extends BaseNode, BaseExpression {
399 type: "Literal";
400 value?: bigint | null | undefined;
401 bigint: string;
402 raw?: string | undefined;
403}
404
405export type UnaryOperator =
406 "-" | "+" | "!" | "~" | "typeof" | "void" | "delete";
407
408export type BinaryOperator =
409 "==" | "!=" | "===" | "!==" | "<" | "<=" | ">" | ">=" | "<<" |
410 ">>" | ">>>" | "+" | "-" | "*" | "/" | "%" | "**" | "|" | "^" | "&" | "in" |
411 "instanceof";
412
413export type LogicalOperator = "||" | "&&" | "??";
414
415export type AssignmentOperator =
416 "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "**=" | "<<=" | ">>=" | ">>>=" |
417 "|=" | "^=" | "&=";
418
419export type UpdateOperator = "++" | "--";
420
421export interface ForOfStatement extends BaseForXStatement {
422 type: "ForOfStatement";
423 await: boolean;
424}
425
426export interface Super extends BaseNode {
427 type: "Super";
428}
429
430export interface SpreadElement extends BaseNode {
431 type: "SpreadElement";
432 argument: Expression;
433}
434
435export interface ArrowFunctionExpression extends BaseExpression, BaseFunction {
436 type: "ArrowFunctionExpression";
437 expression: boolean;
438 body: BlockStatement | Expression;
439}
440
441export interface YieldExpression extends BaseExpression {
442 type: "YieldExpression";
443 argument?: Expression | null | undefined;
444 delegate: boolean;
445}
446
447export interface TemplateLiteral extends BaseExpression {
448 type: "TemplateLiteral";
449 quasis: Array<TemplateElement>;
450 expressions: Array<Expression>;
451}
452
453export interface TaggedTemplateExpression extends BaseExpression {
454 type: "TaggedTemplateExpression";
455 tag: Expression;
456 quasi: TemplateLiteral;
457}
458
459export interface TemplateElement extends BaseNode {
460 type: "TemplateElement";
461 tail: boolean;
462 value: {
463 /** It is null when the template literal is tagged and the text has an invalid escape (e.g. - tag`\unicode and \u{55}`) */
464 cooked?: string | null | undefined;
465 raw: string;
466 };
467}
468
469export interface AssignmentProperty extends Property {
470 value: Pattern;
471 kind: "init";
472 method: boolean; // false
473}
474
475export interface ObjectPattern extends BasePattern {
476 type: "ObjectPattern";
477 properties: Array<AssignmentProperty | RestElement>;
478}
479
480export interface ArrayPattern extends BasePattern {
481 type: "ArrayPattern";
482 elements: Array<Pattern | null>;
483}
484
485export interface RestElement extends BasePattern {
486 type: "RestElement";
487 argument: Pattern;
488}
489
490export interface AssignmentPattern extends BasePattern {
491 type: "AssignmentPattern";
492 left: Pattern;
493 right: Expression;
494}
495
496export type Class = ClassDeclaration | ClassExpression;
497interface BaseClass extends BaseNode {
498 superClass?: Expression | null | undefined;
499 body: ClassBody;
500}
501
502export interface ClassBody extends BaseNode {
503 type: "ClassBody";
504 body: Array<MethodDefinition | PropertyDefinition | StaticBlock>;
505}
506
507export interface MethodDefinition extends BaseNode {
508 type: "MethodDefinition";
509 key: Expression | PrivateIdentifier;
510 value: FunctionExpression;
511 kind: "constructor" | "method" | "get" | "set";
512 computed: boolean;
513 static: boolean;
514}
515
516export interface ClassDeclaration extends BaseClass, BaseDeclaration {
517 type: "ClassDeclaration";
518 /** It is null when a class declaration is a part of the `export default class` statement */
519 id: Identifier | null;
520}
521
522export interface ClassExpression extends BaseClass, BaseExpression {
523 type: "ClassExpression";
524 id?: Identifier | null | undefined;
525}
526
527export interface MetaProperty extends BaseExpression {
528 type: "MetaProperty";
529 meta: Identifier;
530 property: Identifier;
531}
532
533export type ModuleDeclaration =
534 ImportDeclaration | ExportNamedDeclaration | ExportDefaultDeclaration |
535 ExportAllDeclaration;
536interface BaseModuleDeclaration extends BaseNode { }
537
538export type ModuleSpecifier =
539 ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier |
540 ExportSpecifier;
541interface BaseModuleSpecifier extends BaseNode {
542 local: Identifier;
543}
544
545export interface ImportDeclaration extends BaseModuleDeclaration {
546 type: "ImportDeclaration";
547 specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>;
548 source: Literal;
549}
550
551export interface ImportSpecifier extends BaseModuleSpecifier {
552 type: "ImportSpecifier";
553 imported: Identifier;
554}
555
556export interface ImportExpression extends BaseExpression {
557 type: "ImportExpression";
558 source: Expression;
559}
560
561export interface ImportDefaultSpecifier extends BaseModuleSpecifier {
562 type: "ImportDefaultSpecifier";
563}
564
565export interface ImportNamespaceSpecifier extends BaseModuleSpecifier {
566 type: "ImportNamespaceSpecifier";
567}
568
569export interface ExportNamedDeclaration extends BaseModuleDeclaration {
570 type: "ExportNamedDeclaration";
571 declaration?: Declaration | null | undefined;
572 specifiers: Array<ExportSpecifier>;
573 source?: Literal | null | undefined;
574}
575
576export interface ExportSpecifier extends BaseModuleSpecifier {
577 type: "ExportSpecifier";
578 exported: Identifier;
579}
580
581export interface ExportDefaultDeclaration extends BaseModuleDeclaration {
582 type: "ExportDefaultDeclaration";
583 declaration: Declaration | Expression;
584}
585
586export interface ExportAllDeclaration extends BaseModuleDeclaration {
587 type: "ExportAllDeclaration";
588 exported: Identifier | null;
589 source: Literal;
590}
591
592export interface AwaitExpression extends BaseExpression {
593 type: "AwaitExpression";
594 argument: Expression;
595}