UNPKG

6.1 kBTypeScriptView Raw
1// Type definitions for @babel/parser
2// Project: https://github.com/babel/babel/tree/main/packages/babel-parser
3// Definitions by: Troy Gerwien <https://github.com/yortus>
4// Marvin Hagemeister <https://github.com/marvinhagemeister>
5// Avi Vahl <https://github.com/AviVahl>
6// TypeScript Version: 2.9
7
8/**
9 * Parse the provided code as an entire ECMAScript program.
10 */
11export function parse(
12 input: string,
13 options?: ParserOptions
14): ParseResult<import("@babel/types").File>;
15
16/**
17 * Parse the provided code as a single expression.
18 */
19export function parseExpression(
20 input: string,
21 options?: ParserOptions
22): ParseResult<import("@babel/types").Expression>;
23
24export interface ParserOptions {
25 /**
26 * By default, import and export declarations can only appear at a program's top level.
27 * Setting this option to true allows them anywhere where a statement is allowed.
28 */
29 allowImportExportEverywhere?: boolean;
30
31 /**
32 * By default, await use is not allowed outside of an async function.
33 * Set this to true to accept such code.
34 */
35 allowAwaitOutsideFunction?: boolean;
36
37 /**
38 * By default, a return statement at the top level raises an error.
39 * Set this to true to accept such code.
40 */
41 allowReturnOutsideFunction?: boolean;
42
43 allowSuperOutsideMethod?: boolean;
44
45 /**
46 * By default, exported identifiers must refer to a declared variable.
47 * Set this to true to allow export statements to reference undeclared variables.
48 */
49 allowUndeclaredExports?: boolean;
50
51 /**
52 * By default, Babel attaches comments to adjacent AST nodes.
53 * When this option is set to false, comments are not attached.
54 * It can provide up to 30% performance improvement when the input code has many comments.
55 * @babel/eslint-parser will set it for you.
56 * It is not recommended to use attachComment: false with Babel transform,
57 * as doing so removes all the comments in output code, and renders annotations such as
58 * /* istanbul ignore next *\/ nonfunctional.
59 */
60 attachComment?: boolean;
61
62 /**
63 * By default, Babel always throws an error when it finds some invalid code.
64 * When this option is set to true, it will store the parsing error and
65 * try to continue parsing the invalid input file.
66 */
67 errorRecovery?: boolean;
68
69 /**
70 * Indicate the mode the code should be parsed in.
71 * Can be one of "script", "module", or "unambiguous". Defaults to "script".
72 * "unambiguous" will make @babel/parser attempt to guess, based on the presence
73 * of ES6 import or export statements.
74 * Files with ES6 imports and exports are considered "module" and are otherwise "script".
75 */
76 sourceType?: "script" | "module" | "unambiguous";
77
78 /**
79 * Correlate output AST nodes with their source filename.
80 * Useful when generating code and source maps from the ASTs of multiple input files.
81 */
82 sourceFilename?: string;
83
84 /**
85 * By default, the first line of code parsed is treated as line 1.
86 * You can provide a line number to alternatively start with.
87 * Useful for integration with other source tools.
88 */
89 startLine?: number;
90
91 /**
92 * By default, the parsed code is treated as if it starts from line 1, column 0.
93 * You can provide a column number to alternatively start with.
94 * Useful for integration with other source tools.
95 */
96 startColumn?: number;
97
98 /**
99 * Array containing the plugins that you want to enable.
100 */
101 plugins?: ParserPlugin[];
102
103 /**
104 * Should the parser work in strict mode.
105 * Defaults to true if sourceType === 'module'. Otherwise, false.
106 */
107 strictMode?: boolean;
108
109 /**
110 * Adds a ranges property to each node: [node.start, node.end]
111 */
112 ranges?: boolean;
113
114 /**
115 * Adds all parsed tokens to a tokens property on the File node.
116 */
117 tokens?: boolean;
118
119 /**
120 * By default, the parser adds information about parentheses by setting
121 * `extra.parenthesized` to `true` as needed.
122 * When this option is `true` the parser creates `ParenthesizedExpression`
123 * AST nodes instead of using the `extra` property.
124 */
125 createParenthesizedExpressions?: boolean;
126}
127
128export type ParserPlugin =
129 | "asyncDoExpressions"
130 | "asyncGenerators"
131 | "bigInt"
132 | "classPrivateMethods"
133 | "classPrivateProperties"
134 | "classProperties"
135 | "classStaticBlock" // Enabled by default
136 | "decimal"
137 | "decorators"
138 | "decorators-legacy"
139 | "decoratorAutoAccessors"
140 | "destructuringPrivate"
141 | "doExpressions"
142 | "dynamicImport"
143 | "estree"
144 | "exportDefaultFrom"
145 | "exportNamespaceFrom" // deprecated
146 | "flow"
147 | "flowComments"
148 | "functionBind"
149 | "functionSent"
150 | "importMeta"
151 | "jsx"
152 | "logicalAssignment"
153 | "importAssertions"
154 | "moduleBlocks"
155 | "moduleStringNames"
156 | "nullishCoalescingOperator"
157 | "numericSeparator"
158 | "objectRestSpread"
159 | "optionalCatchBinding"
160 | "optionalChaining"
161 | "partialApplication"
162 | "pipelineOperator"
163 | "placeholders"
164 | "privateIn" // Enabled by default
165 | "regexpUnicodeSets"
166 | "throwExpressions"
167 | "topLevelAwait"
168 | "typescript"
169 | "v8intrinsic"
170 | ParserPluginWithOptions;
171
172export type ParserPluginWithOptions =
173 | ["decorators", DecoratorsPluginOptions]
174 | ["pipelineOperator", PipelineOperatorPluginOptions]
175 | ["recordAndTuple", RecordAndTuplePluginOptions]
176 | ["flow", FlowPluginOptions]
177 | ["typescript", TypeScriptPluginOptions];
178
179export interface DecoratorsPluginOptions {
180 decoratorsBeforeExport?: boolean;
181}
182
183export interface PipelineOperatorPluginOptions {
184 proposal: "minimal" | "fsharp" | "hack" | "smart";
185 topicToken?: "%" | "#" | "@@" | "^^" | "^";
186}
187
188export interface RecordAndTuplePluginOptions {
189 syntaxType: "bar" | "hash";
190}
191
192export interface FlowPluginOptions {
193 all?: boolean;
194 enums?: boolean;
195}
196
197export interface TypeScriptPluginOptions {
198 dts?: boolean;
199 disallowAmbiguousJSXLike?: boolean;
200}
201
202export const tokTypes: {
203 // todo(flow->ts) real token type
204 [name: string]: any;
205};
206
207export interface ParseError {
208 code: string;
209 reasonCode: string;
210}
211
212type ParseResult<Result> = Result & {
213 errors: ParseError[];
214};