UNPKG

5.74 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 * Array containing the plugins that you want to enable.
93 */
94 plugins?: ParserPlugin[];
95
96 /**
97 * Should the parser work in strict mode.
98 * Defaults to true if sourceType === 'module'. Otherwise, false.
99 */
100 strictMode?: boolean;
101
102 /**
103 * Adds a ranges property to each node: [node.start, node.end]
104 */
105 ranges?: boolean;
106
107 /**
108 * Adds all parsed tokens to a tokens property on the File node.
109 */
110 tokens?: boolean;
111
112 /**
113 * By default, the parser adds information about parentheses by setting
114 * `extra.parenthesized` to `true` as needed.
115 * When this option is `true` the parser creates `ParenthesizedExpression`
116 * AST nodes instead of using the `extra` property.
117 */
118 createParenthesizedExpressions?: boolean;
119}
120
121export type ParserPlugin =
122 | "asyncDoExpressions"
123 | "asyncGenerators"
124 | "bigInt"
125 | "classPrivateMethods"
126 | "classPrivateProperties"
127 | "classProperties"
128 | "classStaticBlock" // Enabled by default
129 | "decimal"
130 | "decorators"
131 | "decorators-legacy"
132 | "doExpressions"
133 | "dynamicImport"
134 | "estree"
135 | "exportDefaultFrom"
136 | "exportNamespaceFrom" // deprecated
137 | "flow"
138 | "flowComments"
139 | "functionBind"
140 | "functionSent"
141 | "importMeta"
142 | "jsx"
143 | "logicalAssignment"
144 | "importAssertions"
145 | "moduleBlocks"
146 | "moduleStringNames"
147 | "nullishCoalescingOperator"
148 | "numericSeparator"
149 | "objectRestSpread"
150 | "optionalCatchBinding"
151 | "optionalChaining"
152 | "partialApplication"
153 | "pipelineOperator"
154 | "placeholders"
155 | "privateIn" // Enabled by default
156 | "throwExpressions"
157 | "topLevelAwait"
158 | "typescript"
159 | "v8intrinsic"
160 | ParserPluginWithOptions;
161
162export type ParserPluginWithOptions =
163 | ["decorators", DecoratorsPluginOptions]
164 | ["pipelineOperator", PipelineOperatorPluginOptions]
165 | ["recordAndTuple", RecordAndTuplePluginOptions]
166 | ["flow", FlowPluginOptions]
167 | ["typescript", TypeScriptPluginOptions];
168
169export interface DecoratorsPluginOptions {
170 decoratorsBeforeExport?: boolean;
171}
172
173export interface PipelineOperatorPluginOptions {
174 proposal: "minimal" | "fsharp" | "hack" | "smart";
175 topicToken?: "%" | "#";
176}
177
178export interface RecordAndTuplePluginOptions {
179 syntaxType: "bar" | "hash";
180}
181
182export interface FlowPluginOptions {
183 all?: boolean;
184}
185
186export interface TypeScriptPluginOptions {
187 dts?: boolean;
188 disallowAmbiguousJSXLike?: boolean;
189}
190
191export const tokTypes: {
192 // todo(flow->ts) real token type
193 [name: string]: any;
194};
195
196export interface ParseError {
197 code: string;
198 reasonCode: string;
199}
200
201type ParseResult<Result> = Result & {
202 errors: ParseError[];
203};