UNPKG

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