UNPKG

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