UNPKG

4.81 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): 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): 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 always throws an error when it finds some invalid code.
53 * When this option is set to true, it will store the parsing error and
54 * try to continue parsing the invalid input file.
55 */
56 errorRecovery?: boolean;
57
58 /**
59 * Indicate the mode the code should be parsed in.
60 * Can be one of "script", "module", or "unambiguous". Defaults to "script".
61 * "unambiguous" will make @babel/parser attempt to guess, based on the presence
62 * of ES6 import or export statements.
63 * Files with ES6 imports and exports are considered "module" and are otherwise "script".
64 */
65 sourceType?: "script" | "module" | "unambiguous";
66
67 /**
68 * Correlate output AST nodes with their source filename.
69 * Useful when generating code and source maps from the ASTs of multiple input files.
70 */
71 sourceFilename?: string;
72
73 /**
74 * By default, the first line of code parsed is treated as line 1.
75 * You can provide a line number to alternatively start with.
76 * Useful for integration with other source tools.
77 */
78 startLine?: number;
79
80 /**
81 * Array containing the plugins that you want to enable.
82 */
83 plugins?: ParserPlugin[];
84
85 /**
86 * Should the parser work in strict mode.
87 * Defaults to true if sourceType === 'module'. Otherwise, false.
88 */
89 strictMode?: boolean;
90
91 /**
92 * Adds a ranges property to each node: [node.start, node.end]
93 */
94 ranges?: boolean;
95
96 /**
97 * Adds all parsed tokens to a tokens property on the File node.
98 */
99 tokens?: boolean;
100
101 /**
102 * By default, the parser adds information about parentheses by setting
103 * `extra.parenthesized` to `true` as needed.
104 * When this option is `true` the parser creates `ParenthesizedExpression`
105 * AST nodes instead of using the `extra` property.
106 */
107 createParenthesizedExpressions?: boolean;
108}
109
110export type ParserPlugin =
111 | "asyncDoExpressions"
112 | "asyncGenerators"
113 | "bigInt"
114 | "classPrivateMethods"
115 | "classPrivateProperties"
116 | "classProperties"
117 | "classStaticBlock"
118 | "decimal"
119 | "decorators"
120 | "decorators-legacy"
121 | "doExpressions"
122 | "dynamicImport"
123 | "estree"
124 | "exportDefaultFrom"
125 | "exportNamespaceFrom" // deprecated
126 | "flow"
127 | "flowComments"
128 | "functionBind"
129 | "functionSent"
130 | "importMeta"
131 | "jsx"
132 | "logicalAssignment"
133 | "importAssertions"
134 | "moduleStringNames"
135 | "nullishCoalescingOperator"
136 | "numericSeparator"
137 | "objectRestSpread"
138 | "optionalCatchBinding"
139 | "optionalChaining"
140 | "partialApplication"
141 | "pipelineOperator"
142 | "placeholders"
143 | "privateIn"
144 | "throwExpressions"
145 | "topLevelAwait"
146 | "typescript"
147 | "v8intrinsic"
148 | ParserPluginWithOptions;
149
150export type ParserPluginWithOptions =
151 | ["decorators", DecoratorsPluginOptions]
152 | ["pipelineOperator", PipelineOperatorPluginOptions]
153 | ["recordAndTuple", RecordAndTuplePluginOptions]
154 | ["flow", FlowPluginOptions];
155
156export interface DecoratorsPluginOptions {
157 decoratorsBeforeExport?: boolean;
158}
159
160export interface PipelineOperatorPluginOptions {
161 proposal: "fsharp" | "minimal" | "smart";
162}
163
164export interface RecordAndTuplePluginOptions {
165 syntaxType: "bar" | "hash";
166}
167
168export interface FlowPluginOptions {
169 all?: boolean;
170}
171
172export const tokTypes: {
173 // todo(flow->ts) real token type
174 [name: string]: any;
175};