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
501export type UpdateOperator = '++' | '--';
502
503export interface ForOfStatement extends BaseForXStatement {
504 type: 'ForOfStatement';
505 await: boolean;
506}
507
508export interface Super extends BaseNode {
509 type: 'Super';
510}
511
512export interface SpreadElement extends BaseNode {
513 type: 'SpreadElement';
514 argument: Expression;
515}
516
517export interface ArrowFunctionExpression extends BaseExpression, BaseFunction {
518 type: 'ArrowFunctionExpression';
519 expression: boolean;
520 body: BlockStatement | Expression;
521}
522
523export interface YieldExpression extends BaseExpression {
524 type: 'YieldExpression';
525 argument?: Expression | null | undefined;
526 delegate: boolean;
527}
528
529export interface TemplateLiteral extends BaseExpression {
530 type: 'TemplateLiteral';
531 quasis: TemplateElement[];
532 expressions: Expression[];
533}
534
535export interface TaggedTemplateExpression extends BaseExpression {
536 type: 'TaggedTemplateExpression';
537 tag: Expression;
538 quasi: TemplateLiteral;
539}
540
541export interface TemplateElement extends BaseNode {
542 type: 'TemplateElement';
543 tail: boolean;
544 value: {
545 /** It is null when the template literal is tagged and the text has an invalid escape (e.g. - tag`\unicode and \u{55}`) */
546 cooked?: string | null | undefined;
547 raw: string;
548 };
549}
550
551export interface AssignmentProperty extends Property {
552 value: Pattern;
553 kind: 'init';
554 method: boolean; // false
555}
556
557export interface ObjectPattern extends BasePattern {
558 type: 'ObjectPattern';
559 properties: Array<AssignmentProperty | RestElement>;
560}
561
562export interface ArrayPattern extends BasePattern {
563 type: 'ArrayPattern';
564 elements: Array<Pattern | null>;
565}
566
567export interface RestElement extends BasePattern {
568 type: 'RestElement';
569 argument: Pattern;
570}
571
572export interface AssignmentPattern extends BasePattern {
573 type: 'AssignmentPattern';
574 left: Pattern;
575 right: Expression;
576}
577
578export type Class = ClassDeclaration | ClassExpression;
579export interface BaseClass extends BaseNode {
580 superClass?: Expression | null | undefined;
581 body: ClassBody;
582}
583
584export interface ClassBody extends BaseNode {
585 type: 'ClassBody';
586 body: Array<MethodDefinition | PropertyDefinition | StaticBlock>;
587}
588
589export interface MethodDefinition extends BaseNode {
590 type: 'MethodDefinition';
591 key: Expression | PrivateIdentifier;
592 value: FunctionExpression;
593 kind: 'constructor' | 'method' | 'get' | 'set';
594 computed: boolean;
595 static: boolean;
596}
597
598export interface ClassDeclaration extends BaseClass, BaseDeclaration {
599 type: 'ClassDeclaration';
600 /** It is null when a class declaration is a part of the `export default class` statement */
601 id: Identifier | null;
602}
603
604export interface ClassExpression extends BaseClass, BaseExpression {
605 type: 'ClassExpression';
606 id?: Identifier | null | undefined;
607}
608
609export interface MetaProperty extends BaseExpression {
610 type: 'MetaProperty';
611 meta: Identifier;
612 property: Identifier;
613}
614
615export type ModuleDeclaration =
616 | ImportDeclaration
617 | ExportNamedDeclaration
618 | ExportDefaultDeclaration
619 | ExportAllDeclaration;
620export interface BaseModuleDeclaration extends BaseNode {}
621
622export type ModuleSpecifier = ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier;
623export interface BaseModuleSpecifier extends BaseNode {
624 local: Identifier;
625}
626
627export interface ImportDeclaration extends BaseModuleDeclaration {
628 type: 'ImportDeclaration';
629 specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>;
630 source: Literal;
631}
632
633export interface ImportSpecifier extends BaseModuleSpecifier {
634 type: 'ImportSpecifier';
635 imported: Identifier;
636}
637
638export interface ImportExpression extends BaseExpression {
639 type: 'ImportExpression';
640 source: Expression;
641}
642
643export interface ImportDefaultSpecifier extends BaseModuleSpecifier {
644 type: 'ImportDefaultSpecifier';
645}
646
647export interface ImportNamespaceSpecifier extends BaseModuleSpecifier {
648 type: 'ImportNamespaceSpecifier';
649}
650
651export interface ExportNamedDeclaration extends BaseModuleDeclaration {
652 type: 'ExportNamedDeclaration';
653 declaration?: Declaration | null | undefined;
654 specifiers: ExportSpecifier[];
655 source?: Literal | null | undefined;
656}
657
658export interface ExportSpecifier extends BaseModuleSpecifier {
659 type: 'ExportSpecifier';
660 exported: Identifier;
661}
662
663export interface ExportDefaultDeclaration extends BaseModuleDeclaration {
664 type: 'ExportDefaultDeclaration';
665 declaration: Declaration | Expression;
666}
667
668export interface ExportAllDeclaration extends BaseModuleDeclaration {
669 type: 'ExportAllDeclaration';
670 exported: Identifier | null;
671 source: Literal;
672}
673
674export interface AwaitExpression extends BaseExpression {
675 type: 'AwaitExpression';
676 argument: Expression;
677}