UNPKG

21.3 kBTypeScriptView Raw
1export interface Node {
2 start: number
3 end: number
4 type: string
5 range?: [number, number]
6 loc?: SourceLocation | null
7}
8
9export interface SourceLocation {
10 source?: string | null
11 start: Position
12 end: Position
13}
14
15export interface Position {
16 /** 1-based */
17 line: number
18 /** 0-based */
19 column: number
20}
21
22export interface Identifier extends Node {
23 type: "Identifier"
24 name: string
25}
26
27export interface Literal extends Node {
28 type: "Literal"
29 value?: string | boolean | null | number | RegExp | bigint
30 raw?: string
31 regex?: {
32 pattern: string
33 flags: string
34 }
35 bigint?: string
36}
37
38export interface Program extends Node {
39 type: "Program"
40 body: Array<Statement | ModuleDeclaration>
41 sourceType: "script" | "module"
42}
43
44export interface Function extends Node {
45 id?: Identifier | null
46 params: Array<Pattern>
47 body: BlockStatement | Expression
48 generator: boolean
49 expression: boolean
50 async: boolean
51}
52
53export interface ExpressionStatement extends Node {
54 type: "ExpressionStatement"
55 expression: Expression | Literal
56 directive?: string
57}
58
59export interface BlockStatement extends Node {
60 type: "BlockStatement"
61 body: Array<Statement>
62}
63
64export interface EmptyStatement extends Node {
65 type: "EmptyStatement"
66}
67
68export interface DebuggerStatement extends Node {
69 type: "DebuggerStatement"
70}
71
72export interface WithStatement extends Node {
73 type: "WithStatement"
74 object: Expression
75 body: Statement
76}
77
78export interface ReturnStatement extends Node {
79 type: "ReturnStatement"
80 argument?: Expression | null
81}
82
83export interface LabeledStatement extends Node {
84 type: "LabeledStatement"
85 label: Identifier
86 body: Statement
87}
88
89export interface BreakStatement extends Node {
90 type: "BreakStatement"
91 label?: Identifier | null
92}
93
94export interface ContinueStatement extends Node {
95 type: "ContinueStatement"
96 label?: Identifier | null
97}
98
99export interface IfStatement extends Node {
100 type: "IfStatement"
101 test: Expression
102 consequent: Statement
103 alternate?: Statement | null
104}
105
106export interface SwitchStatement extends Node {
107 type: "SwitchStatement"
108 discriminant: Expression
109 cases: Array<SwitchCase>
110}
111
112export interface SwitchCase extends Node {
113 type: "SwitchCase"
114 test?: Expression | null
115 consequent: Array<Statement>
116}
117
118export interface ThrowStatement extends Node {
119 type: "ThrowStatement"
120 argument: Expression
121}
122
123export interface TryStatement extends Node {
124 type: "TryStatement"
125 block: BlockStatement
126 handler?: CatchClause | null
127 finalizer?: BlockStatement | null
128}
129
130export interface CatchClause extends Node {
131 type: "CatchClause"
132 param?: Pattern | null
133 body: BlockStatement
134}
135
136export interface WhileStatement extends Node {
137 type: "WhileStatement"
138 test: Expression
139 body: Statement
140}
141
142export interface DoWhileStatement extends Node {
143 type: "DoWhileStatement"
144 body: Statement
145 test: Expression
146}
147
148export interface ForStatement extends Node {
149 type: "ForStatement"
150 init?: VariableDeclaration | Expression | null
151 test?: Expression | null
152 update?: Expression | null
153 body: Statement
154}
155
156export interface ForInStatement extends Node {
157 type: "ForInStatement"
158 left: VariableDeclaration | Pattern
159 right: Expression
160 body: Statement
161}
162
163export interface FunctionDeclaration extends Function {
164 type: "FunctionDeclaration"
165 id: Identifier
166 body: BlockStatement
167}
168
169export interface VariableDeclaration extends Node {
170 type: "VariableDeclaration"
171 declarations: Array<VariableDeclarator>
172 kind: "var" | "let" | "const"
173}
174
175export interface VariableDeclarator extends Node {
176 type: "VariableDeclarator"
177 id: Pattern
178 init?: Expression | null
179}
180
181export interface ThisExpression extends Node {
182 type: "ThisExpression"
183}
184
185export interface ArrayExpression extends Node {
186 type: "ArrayExpression"
187 elements: Array<Expression | SpreadElement | null>
188}
189
190export interface ObjectExpression extends Node {
191 type: "ObjectExpression"
192 properties: Array<Property | SpreadElement>
193}
194
195export interface Property extends Node {
196 type: "Property"
197 key: Expression
198 value: Expression
199 kind: "init" | "get" | "set"
200 method: boolean
201 shorthand: boolean
202 computed: boolean
203}
204
205export interface FunctionExpression extends Function {
206 type: "FunctionExpression"
207 body: BlockStatement
208}
209
210export interface UnaryExpression extends Node {
211 type: "UnaryExpression"
212 operator: UnaryOperator
213 prefix: boolean
214 argument: Expression
215}
216
217export type UnaryOperator = "-" | "+" | "!" | "~" | "typeof" | "void" | "delete"
218
219export interface UpdateExpression extends Node {
220 type: "UpdateExpression"
221 operator: UpdateOperator
222 argument: Expression
223 prefix: boolean
224}
225
226export type UpdateOperator = "++" | "--"
227
228export interface BinaryExpression extends Node {
229 type: "BinaryExpression"
230 operator: BinaryOperator
231 left: Expression | PrivateIdentifier
232 right: Expression
233}
234
235export type BinaryOperator = "==" | "!=" | "===" | "!==" | "<" | "<=" | ">" | ">=" | "<<" | ">>" | ">>>" | "+" | "-" | "*" | "/" | "%" | "|" | "^" | "&" | "in" | "instanceof" | "**"
236
237export interface AssignmentExpression extends Node {
238 type: "AssignmentExpression"
239 operator: AssignmentOperator
240 left: Pattern
241 right: Expression
242}
243
244export type AssignmentOperator = "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "<<=" | ">>=" | ">>>=" | "|=" | "^=" | "&=" | "**=" | "||=" | "&&=" | "??="
245
246export interface LogicalExpression extends Node {
247 type: "LogicalExpression"
248 operator: LogicalOperator
249 left: Expression
250 right: Expression
251}
252
253export type LogicalOperator = "||" | "&&" | "??"
254
255export interface MemberExpression extends Node {
256 type: "MemberExpression"
257 object: Expression | Super
258 property: Expression | PrivateIdentifier
259 computed: boolean
260 optional: boolean
261}
262
263export interface ConditionalExpression extends Node {
264 type: "ConditionalExpression"
265 test: Expression
266 alternate: Expression
267 consequent: Expression
268}
269
270export interface CallExpression extends Node {
271 type: "CallExpression"
272 callee: Expression | Super
273 arguments: Array<Expression | SpreadElement>
274 optional: boolean
275}
276
277export interface NewExpression extends Node {
278 type: "NewExpression"
279 callee: Expression
280 arguments: Array<Expression | SpreadElement>
281}
282
283export interface SequenceExpression extends Node {
284 type: "SequenceExpression"
285 expressions: Array<Expression>
286}
287
288export interface ForOfStatement extends Node {
289 type: "ForOfStatement"
290 left: VariableDeclaration | Pattern
291 right: Expression
292 body: Statement
293 await: boolean
294}
295
296export interface Super extends Node {
297 type: "Super"
298}
299
300export interface SpreadElement extends Node {
301 type: "SpreadElement"
302 argument: Expression
303}
304
305export interface ArrowFunctionExpression extends Function {
306 type: "ArrowFunctionExpression"
307}
308
309export interface YieldExpression extends Node {
310 type: "YieldExpression"
311 argument?: Expression | null
312 delegate: boolean
313}
314
315export interface TemplateLiteral extends Node {
316 type: "TemplateLiteral"
317 quasis: Array<TemplateElement>
318 expressions: Array<Expression>
319}
320
321export interface TaggedTemplateExpression extends Node {
322 type: "TaggedTemplateExpression"
323 tag: Expression
324 quasi: TemplateLiteral
325}
326
327export interface TemplateElement extends Node {
328 type: "TemplateElement"
329 tail: boolean
330 value: {
331 cooked?: string | null
332 raw: string
333 }
334}
335
336export interface AssignmentProperty extends Node {
337 type: "Property"
338 key: Expression
339 value: Pattern
340 kind: "init"
341 method: false
342 shorthand: boolean
343 computed: boolean
344}
345
346export interface ObjectPattern extends Node {
347 type: "ObjectPattern"
348 properties: Array<AssignmentProperty | RestElement>
349}
350
351export interface ArrayPattern extends Node {
352 type: "ArrayPattern"
353 elements: Array<Pattern | null>
354}
355
356export interface RestElement extends Node {
357 type: "RestElement"
358 argument: Pattern
359}
360
361export interface AssignmentPattern extends Node {
362 type: "AssignmentPattern"
363 left: Pattern
364 right: Expression
365}
366
367export interface Class extends Node {
368 id?: Identifier | null
369 superClass?: Expression | null
370 body: ClassBody
371}
372
373export interface ClassBody extends Node {
374 type: "ClassBody"
375 body: Array<MethodDefinition | PropertyDefinition | StaticBlock>
376}
377
378export interface MethodDefinition extends Node {
379 type: "MethodDefinition"
380 key: Expression | PrivateIdentifier
381 value: FunctionExpression
382 kind: "constructor" | "method" | "get" | "set"
383 computed: boolean
384 static: boolean
385}
386
387export interface ClassDeclaration extends Class {
388 type: "ClassDeclaration"
389 id: Identifier
390}
391
392export interface ClassExpression extends Class {
393 type: "ClassExpression"
394}
395
396export interface MetaProperty extends Node {
397 type: "MetaProperty"
398 meta: Identifier
399 property: Identifier
400}
401
402export interface ImportDeclaration extends Node {
403 type: "ImportDeclaration"
404 specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>
405 source: Literal
406}
407
408export interface ImportSpecifier extends Node {
409 type: "ImportSpecifier"
410 imported: Identifier | Literal
411 local: Identifier
412}
413
414export interface ImportDefaultSpecifier extends Node {
415 type: "ImportDefaultSpecifier"
416 local: Identifier
417}
418
419export interface ImportNamespaceSpecifier extends Node {
420 type: "ImportNamespaceSpecifier"
421 local: Identifier
422}
423
424export interface ExportNamedDeclaration extends Node {
425 type: "ExportNamedDeclaration"
426 declaration?: Declaration | null
427 specifiers: Array<ExportSpecifier>
428 source?: Literal | null
429}
430
431export interface ExportSpecifier extends Node {
432 type: "ExportSpecifier"
433 exported: Identifier | Literal
434 local: Identifier | Literal
435}
436
437export interface AnonymousFunctionDeclaration extends Function {
438 type: "FunctionDeclaration"
439 id: null
440 body: BlockStatement
441}
442
443export interface AnonymousClassDeclaration extends Class {
444 type: "ClassDeclaration"
445 id: null
446}
447
448export interface ExportDefaultDeclaration extends Node {
449 type: "ExportDefaultDeclaration"
450 declaration: AnonymousFunctionDeclaration | FunctionDeclaration | AnonymousClassDeclaration | ClassDeclaration | Expression
451}
452
453export interface ExportAllDeclaration extends Node {
454 type: "ExportAllDeclaration"
455 source: Literal
456 exported?: Identifier | Literal | null
457}
458
459export interface AwaitExpression extends Node {
460 type: "AwaitExpression"
461 argument: Expression
462}
463
464export interface ChainExpression extends Node {
465 type: "ChainExpression"
466 expression: MemberExpression | CallExpression
467}
468
469export interface ImportExpression extends Node {
470 type: "ImportExpression"
471 source: Expression
472}
473
474export interface ParenthesizedExpression extends Node {
475 type: "ParenthesizedExpression"
476 expression: Expression
477}
478
479export interface PropertyDefinition extends Node {
480 type: "PropertyDefinition"
481 key: Expression | PrivateIdentifier
482 value?: Expression | null
483 computed: boolean
484 static: boolean
485}
486
487export interface PrivateIdentifier extends Node {
488 type: "PrivateIdentifier"
489 name: string
490}
491
492export interface StaticBlock extends Node {
493 type: "StaticBlock"
494 body: Array<Statement>
495}
496
497export type Statement =
498| ExpressionStatement
499| BlockStatement
500| EmptyStatement
501| DebuggerStatement
502| WithStatement
503| ReturnStatement
504| LabeledStatement
505| BreakStatement
506| ContinueStatement
507| IfStatement
508| SwitchStatement
509| ThrowStatement
510| TryStatement
511| WhileStatement
512| DoWhileStatement
513| ForStatement
514| ForInStatement
515| ForOfStatement
516| Declaration
517
518export type Declaration =
519| FunctionDeclaration
520| VariableDeclaration
521| ClassDeclaration
522
523export type Expression =
524| Identifier
525| Literal
526| ThisExpression
527| ArrayExpression
528| ObjectExpression
529| FunctionExpression
530| UnaryExpression
531| UpdateExpression
532| BinaryExpression
533| AssignmentExpression
534| LogicalExpression
535| MemberExpression
536| ConditionalExpression
537| CallExpression
538| NewExpression
539| SequenceExpression
540| ArrowFunctionExpression
541| YieldExpression
542| TemplateLiteral
543| TaggedTemplateExpression
544| ClassExpression
545| MetaProperty
546| AwaitExpression
547| ChainExpression
548| ImportExpression
549| ParenthesizedExpression
550
551export type Pattern =
552| Identifier
553| MemberExpression
554| ObjectPattern
555| ArrayPattern
556| RestElement
557| AssignmentPattern
558
559export type ModuleDeclaration =
560| ImportDeclaration
561| ExportNamedDeclaration
562| ExportDefaultDeclaration
563| ExportAllDeclaration
564
565export type AnyNode = Statement | Expression | Declaration | ModuleDeclaration | Literal | Program | SwitchCase | CatchClause | Property | Super | SpreadElement | TemplateElement | AssignmentProperty | ObjectPattern | ArrayPattern | RestElement | AssignmentPattern | ClassBody | MethodDefinition | MetaProperty | ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier | AnonymousFunctionDeclaration | AnonymousClassDeclaration | PropertyDefinition | PrivateIdentifier | StaticBlock | VariableDeclarator
566
567export function parse(input: string, options: Options): Program
568
569export function parseExpressionAt(input: string, pos: number, options: Options): Expression
570
571export function tokenizer(input: string, options: Options): {
572 getToken(): Token
573 [Symbol.iterator](): Iterator<Token>
574}
575
576export type ecmaVersion = 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 2024 | 2025 | "latest"
577
578export interface Options {
579 /**
580 * `ecmaVersion` indicates the ECMAScript version to parse. Can be a
581 * number, either in year (`2022`) or plain version number (`6`) form,
582 * or `"latest"` (the latest the library supports). This influences
583 * support for strict mode, the set of reserved words, and support for
584 * new syntax features.
585 */
586 ecmaVersion: ecmaVersion
587
588 /**
589 * `sourceType` indicates the mode the code should be parsed in.
590 * Can be either `"script"` or `"module"`. This influences global
591 * strict mode and parsing of `import` and `export` declarations.
592 */
593 sourceType?: "script" | "module"
594
595 /**
596 * a callback that will be called when a semicolon is automatically inserted.
597 * @param lastTokEnd the position of the comma as an offset
598 * @param lastTokEndLoc location if {@link locations} is enabled
599 */
600 onInsertedSemicolon?: (lastTokEnd: number, lastTokEndLoc?: Position) => void
601
602 /**
603 * similar to `onInsertedSemicolon`, but for trailing commas
604 * @param lastTokEnd the position of the comma as an offset
605 * @param lastTokEndLoc location if `locations` is enabled
606 */
607 onTrailingComma?: (lastTokEnd: number, lastTokEndLoc?: Position) => void
608
609 /**
610 * By default, reserved words are only enforced if ecmaVersion >= 5.
611 * Set `allowReserved` to a boolean value to explicitly turn this on
612 * an off. When this option has the value "never", reserved words
613 * and keywords can also not be used as property names.
614 */
615 allowReserved?: boolean | "never"
616
617 /**
618 * When enabled, a return at the top level is not considered an error.
619 */
620 allowReturnOutsideFunction?: boolean
621
622 /**
623 * When enabled, import/export statements are not constrained to
624 * appearing at the top of the program, and an import.meta expression
625 * in a script isn't considered an error.
626 */
627 allowImportExportEverywhere?: boolean
628
629 /**
630 * By default, `await` identifiers are allowed to appear at the top-level scope only if {@link ecmaVersion} >= 2022.
631 * When enabled, await identifiers are allowed to appear at the top-level scope,
632 * but they are still not allowed in non-async functions.
633 */
634 allowAwaitOutsideFunction?: boolean
635
636 /**
637 * When enabled, super identifiers are not constrained to
638 * appearing in methods and do not raise an error when they appear elsewhere.
639 */
640 allowSuperOutsideMethod?: boolean
641
642 /**
643 * When enabled, hashbang directive in the beginning of file is
644 * allowed and treated as a line comment. Enabled by default when
645 * {@link ecmaVersion} >= 2023.
646 */
647 allowHashBang?: boolean
648
649 /**
650 * By default, the parser will verify that private properties are
651 * only used in places where they are valid and have been declared.
652 * Set this to false to turn such checks off.
653 */
654 checkPrivateFields?: boolean
655
656 /**
657 * When `locations` is on, `loc` properties holding objects with
658 * `start` and `end` properties as {@link Position} objects will be attached to the
659 * nodes.
660 */
661 locations?: boolean
662
663 /**
664 * a callback that will cause Acorn to call that export function with object in the same
665 * format as tokens returned from `tokenizer().getToken()`. Note
666 * that you are not allowed to call the parser from the
667 * callback—that will corrupt its internal state.
668 */
669 onToken?: ((token: Token) => void) | Token[]
670
671
672 /**
673 * This takes a export function or an array.
674 *
675 * When a export function is passed, Acorn will call that export function with `(block, text, start,
676 * end)` parameters whenever a comment is skipped. `block` is a
677 * boolean indicating whether this is a block (`/* *\/`) comment,
678 * `text` is the content of the comment, and `start` and `end` are
679 * character offsets that denote the start and end of the comment.
680 * When the {@link locations} option is on, two more parameters are
681 * passed, the full locations of {@link Position} export type of the start and
682 * end of the comments.
683 *
684 * When a array is passed, each found comment of {@link Comment} export type is pushed to the array.
685 *
686 * Note that you are not allowed to call the
687 * parser from the callback—that will corrupt its internal state.
688 */
689 onComment?: ((
690 isBlock: boolean, text: string, start: number, end: number, startLoc?: Position,
691 endLoc?: Position
692 ) => void) | Comment[]
693
694 /**
695 * Nodes have their start and end characters offsets recorded in
696 * `start` and `end` properties (directly on the node, rather than
697 * the `loc` object, which holds line/column data. To also add a
698 * [semi-standardized][range] `range` property holding a `[start,
699 * end]` array with the same numbers, set the `ranges` option to
700 * `true`.
701 */
702 ranges?: boolean
703
704 /**
705 * It is possible to parse multiple files into a single AST by
706 * passing the tree produced by parsing the first file as
707 * `program` option in subsequent parses. This will add the
708 * toplevel forms of the parsed file to the `Program` (top) node
709 * of an existing parse tree.
710 */
711 program?: Node
712
713 /**
714 * When {@link locations} is on, you can pass this to record the source
715 * file in every node's `loc` object.
716 */
717 sourceFile?: string
718
719 /**
720 * This value, if given, is stored in every node, whether {@link locations} is on or off.
721 */
722 directSourceFile?: string
723
724 /**
725 * When enabled, parenthesized expressions are represented by
726 * (non-standard) ParenthesizedExpression nodes
727 */
728 preserveParens?: boolean
729}
730
731export class Parser {
732 options: Options
733 input: string
734
735 protected constructor(options: Options, input: string, startPos?: number)
736 parse(): Program
737
738 static parse(input: string, options: Options): Program
739 static parseExpressionAt(input: string, pos: number, options: Options): Expression
740 static tokenizer(input: string, options: Options): {
741 getToken(): Token
742 [Symbol.iterator](): Iterator<Token>
743 }
744 static extend(...plugins: ((BaseParser: typeof Parser) => typeof Parser)[]): typeof Parser
745}
746
747export const defaultOptions: Options
748
749export function getLineInfo(input: string, offset: number): Position
750
751export class TokenType {
752 label: string
753 keyword: string | undefined
754}
755
756export const tokTypes: {
757 num: TokenType
758 regexp: TokenType
759 string: TokenType
760 name: TokenType
761 privateId: TokenType
762 eof: TokenType
763
764 bracketL: TokenType
765 bracketR: TokenType
766 braceL: TokenType
767 braceR: TokenType
768 parenL: TokenType
769 parenR: TokenType
770 comma: TokenType
771 semi: TokenType
772 colon: TokenType
773 dot: TokenType
774 question: TokenType
775 questionDot: TokenType
776 arrow: TokenType
777 template: TokenType
778 invalidTemplate: TokenType
779 ellipsis: TokenType
780 backQuote: TokenType
781 dollarBraceL: TokenType
782
783 eq: TokenType
784 assign: TokenType
785 incDec: TokenType
786 prefix: TokenType
787 logicalOR: TokenType
788 logicalAND: TokenType
789 bitwiseOR: TokenType
790 bitwiseXOR: TokenType
791 bitwiseAND: TokenType
792 equality: TokenType
793 relational: TokenType
794 bitShift: TokenType
795 plusMin: TokenType
796 modulo: TokenType
797 star: TokenType
798 slash: TokenType
799 starstar: TokenType
800 coalesce: TokenType
801
802 _break: TokenType
803 _case: TokenType
804 _catch: TokenType
805 _continue: TokenType
806 _debugger: TokenType
807 _default: TokenType
808 _do: TokenType
809 _else: TokenType
810 _finally: TokenType
811 _for: TokenType
812 _function: TokenType
813 _if: TokenType
814 _return: TokenType
815 _switch: TokenType
816 _throw: TokenType
817 _try: TokenType
818 _var: TokenType
819 _const: TokenType
820 _while: TokenType
821 _with: TokenType
822 _new: TokenType
823 _this: TokenType
824 _super: TokenType
825 _class: TokenType
826 _extends: TokenType
827 _export: TokenType
828 _import: TokenType
829 _null: TokenType
830 _true: TokenType
831 _false: TokenType
832 _in: TokenType
833 _instanceof: TokenType
834 _typeof: TokenType
835 _void: TokenType
836 _delete: TokenType
837}
838
839export interface Comment {
840 type: "Line" | "Block"
841 value: string
842 start: number
843 end: number
844 loc?: SourceLocation
845 range?: [number, number]
846}
847
848export class Token {
849 type: TokenType
850 start: number
851 end: number
852 loc?: SourceLocation
853 range?: [number, number]
854}
855
856export const version: string
857
\No newline at end of file