@ohos-ports/hermes-parser 0.37.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/dist/HermesAST.js.flow +57 -0
- package/dist/HermesASTAdapter.js +192 -0
- package/dist/HermesASTAdapter.js.flow +189 -0
- package/dist/HermesParser.js +108 -0
- package/dist/HermesParser.js.flow +163 -0
- package/dist/HermesParserDecodeUTF8String.js +68 -0
- package/dist/HermesParserDecodeUTF8String.js.flow +65 -0
- package/dist/HermesParserDeserializer.js +242 -0
- package/dist/HermesParserDeserializer.js.flow +260 -0
- package/dist/HermesParserNodeDeserializers.js +2594 -0
- package/dist/HermesParserNodeDeserializers.js.flow +16 -0
- package/dist/HermesParserWASM.js +6 -0
- package/dist/HermesParserWASM.js.flow +97 -0
- package/dist/HermesToESTreeAdapter.js +441 -0
- package/dist/HermesToESTreeAdapter.js.flow +423 -0
- package/dist/ParserOptions.js +20 -0
- package/dist/ParserOptions.js.flow +44 -0
- package/dist/babel/TransformESTreeToBabel.js +1197 -0
- package/dist/babel/TransformESTreeToBabel.js.flow +1349 -0
- package/dist/estree/StripFlowTypes.js +175 -0
- package/dist/estree/StripFlowTypes.js.flow +158 -0
- package/dist/estree/StripFlowTypesForBabel.js +215 -0
- package/dist/estree/StripFlowTypesForBabel.js.flow +216 -0
- package/dist/estree/TransformComponentSyntax.js +792 -0
- package/dist/estree/TransformComponentSyntax.js.flow +864 -0
- package/dist/estree/TransformEnumSyntax.js +106 -0
- package/dist/estree/TransformEnumSyntax.js.flow +125 -0
- package/dist/estree/TransformMatchSyntax.js +1083 -0
- package/dist/estree/TransformMatchSyntax.js.flow +991 -0
- package/dist/estree/TransformRecordSyntax.js +296 -0
- package/dist/estree/TransformRecordSyntax.js.flow +310 -0
- package/dist/generated/ESTreeVisitorKeys.js +233 -0
- package/dist/generated/ESTreeVisitorKeys.js.flow +15 -0
- package/dist/generated/ParserVisitorKeys.js +842 -0
- package/dist/generated/ParserVisitorKeys.js.flow +17 -0
- package/dist/getModuleDocblock.js +112 -0
- package/dist/getModuleDocblock.js.flow +118 -0
- package/dist/index.js +170 -0
- package/dist/index.js.flow +131 -0
- package/dist/src/HermesASTAdapter.js +192 -0
- package/dist/src/HermesParser.js +108 -0
- package/dist/src/HermesParserDecodeUTF8String.js +68 -0
- package/dist/src/HermesParserDeserializer.js +242 -0
- package/dist/src/HermesParserNodeDeserializers.js +2594 -0
- package/dist/src/HermesToESTreeAdapter.js +441 -0
- package/dist/src/ParserOptions.js +20 -0
- package/dist/src/babel/TransformESTreeToBabel.js +1197 -0
- package/dist/src/estree/StripFlowTypes.js +175 -0
- package/dist/src/estree/StripFlowTypesForBabel.js +215 -0
- package/dist/src/estree/TransformComponentSyntax.js +792 -0
- package/dist/src/estree/TransformEnumSyntax.js +106 -0
- package/dist/src/estree/TransformMatchSyntax.js +1083 -0
- package/dist/src/estree/TransformRecordSyntax.js +296 -0
- package/dist/src/generated/ESTreeVisitorKeys.js +233 -0
- package/dist/src/generated/ParserVisitorKeys.js +842 -0
- package/dist/src/getModuleDocblock.js +112 -0
- package/dist/src/index.js +170 -0
- package/dist/src/transform/SimpleTransform.js +145 -0
- package/dist/src/transform/astArrayMutationHelpers.js +62 -0
- package/dist/src/transform/astNodeMutationHelpers.js +201 -0
- package/dist/src/traverse/SimpleTraverser.js +139 -0
- package/dist/src/traverse/getVisitorKeys.js +39 -0
- package/dist/src/utils/Builders.js +190 -0
- package/dist/src/utils/GenID.js +46 -0
- package/dist/src/utils/createSyntaxError.js +25 -0
- package/dist/src/utils/isReservedWord.js +62 -0
- package/dist/src/utils/mutateESTreeASTForPrettier.js +127 -0
- package/dist/transform/SimpleTransform.js +145 -0
- package/dist/transform/SimpleTransform.js.flow +168 -0
- package/dist/transform/astArrayMutationHelpers.js +62 -0
- package/dist/transform/astArrayMutationHelpers.js.flow +71 -0
- package/dist/transform/astNodeMutationHelpers.js +201 -0
- package/dist/transform/astNodeMutationHelpers.js.flow +252 -0
- package/dist/traverse/SimpleTraverser.js +139 -0
- package/dist/traverse/SimpleTraverser.js.flow +133 -0
- package/dist/traverse/getVisitorKeys.js +39 -0
- package/dist/traverse/getVisitorKeys.js.flow +38 -0
- package/dist/utils/Builders.js +190 -0
- package/dist/utils/Builders.js.flow +218 -0
- package/dist/utils/GenID.js +46 -0
- package/dist/utils/GenID.js.flow +39 -0
- package/dist/utils/createSyntaxError.js +25 -0
- package/dist/utils/createSyntaxError.js.flow +24 -0
- package/dist/utils/isReservedWord.js +62 -0
- package/dist/utils/isReservedWord.js.flow +57 -0
- package/dist/utils/mutateESTreeASTForPrettier.js +127 -0
- package/dist/utils/mutateESTreeASTForPrettier.js.flow +130 -0
- package/package.json +30 -0
|
@@ -0,0 +1,1349 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow strict
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
import type {
|
|
14
|
+
BaseNode,
|
|
15
|
+
BaseToken,
|
|
16
|
+
BlockStatement,
|
|
17
|
+
CallExpression,
|
|
18
|
+
ChainExpression,
|
|
19
|
+
ClassPropertyNameComputed,
|
|
20
|
+
ClassPropertyNameNonComputed,
|
|
21
|
+
DeclareVariable,
|
|
22
|
+
ESNode,
|
|
23
|
+
ExportAllDeclaration,
|
|
24
|
+
Expression,
|
|
25
|
+
FunctionParameter,
|
|
26
|
+
Identifier,
|
|
27
|
+
ImportExpression,
|
|
28
|
+
JSXElement,
|
|
29
|
+
Literal,
|
|
30
|
+
MethodDefinition,
|
|
31
|
+
PrivateIdentifier,
|
|
32
|
+
Program,
|
|
33
|
+
Property,
|
|
34
|
+
PropertyDefinition,
|
|
35
|
+
RestElement,
|
|
36
|
+
SpreadElement,
|
|
37
|
+
StringLiteral,
|
|
38
|
+
Super,
|
|
39
|
+
TemplateElement,
|
|
40
|
+
TypeAnnotation,
|
|
41
|
+
TypeofTypeAnnotation,
|
|
42
|
+
TypeParameterDeclaration,
|
|
43
|
+
TypeParameterInstantiation,
|
|
44
|
+
Variance,
|
|
45
|
+
} from 'hermes-estree';
|
|
46
|
+
import type {ParserOptions} from '../ParserOptions';
|
|
47
|
+
import type {VisitorKeys} from '../generated/ESTreeVisitorKeys';
|
|
48
|
+
|
|
49
|
+
import {SimpleTransform} from '../transform/SimpleTransform';
|
|
50
|
+
import {SimpleTraverser} from '../traverse/SimpleTraverser';
|
|
51
|
+
import FlowVisitorKeys from '../generated/ESTreeVisitorKeys';
|
|
52
|
+
import {createSyntaxError} from '../utils/createSyntaxError';
|
|
53
|
+
|
|
54
|
+
// Rely on the mapper to fix up parent relationships.
|
|
55
|
+
const EMPTY_PARENT: $FlowFixMe = null;
|
|
56
|
+
|
|
57
|
+
const FlowESTreeAndBabelVisitorKeys: VisitorKeys = {
|
|
58
|
+
...FlowVisitorKeys,
|
|
59
|
+
BigIntLiteral: [],
|
|
60
|
+
BlockStatement: ['directives', ...FlowVisitorKeys.BlockStatement],
|
|
61
|
+
BooleanLiteral: [],
|
|
62
|
+
ClassExpression: ['superTypeParameters', ...FlowVisitorKeys.ClassExpression],
|
|
63
|
+
ClassDeclaration: [
|
|
64
|
+
'superTypeParameters',
|
|
65
|
+
...FlowVisitorKeys.ClassDeclaration,
|
|
66
|
+
],
|
|
67
|
+
ClassMethod: ['key', 'params', 'body', 'returnType', 'typeParameters'],
|
|
68
|
+
ClassPrivateMethod: ['key', 'params', 'body', 'returnType', 'typeParameters'],
|
|
69
|
+
ClassProperty: ['key', 'value', 'typeAnnotation', 'variance'],
|
|
70
|
+
ClassPrivateProperty: ['key', 'value', 'typeAnnotation', 'variance'],
|
|
71
|
+
Directive: ['value'],
|
|
72
|
+
DirectiveLiteral: [],
|
|
73
|
+
ExportNamespaceSpecifier: ['exported'],
|
|
74
|
+
File: ['program', 'comments'],
|
|
75
|
+
Import: [],
|
|
76
|
+
NullLiteral: [],
|
|
77
|
+
NumericLiteral: [],
|
|
78
|
+
ObjectMethod: [
|
|
79
|
+
...FlowVisitorKeys.Property,
|
|
80
|
+
'params',
|
|
81
|
+
'body',
|
|
82
|
+
'returnType',
|
|
83
|
+
'typeParameters',
|
|
84
|
+
],
|
|
85
|
+
ObjectProperty: FlowVisitorKeys.Property,
|
|
86
|
+
OptionalCallExpression: ['callee', 'arguments', 'typeArguments'],
|
|
87
|
+
OptionalMemberExpression: ['object', 'property'],
|
|
88
|
+
PrivateName: ['id'],
|
|
89
|
+
Program: ['directives', ...FlowVisitorKeys.Program],
|
|
90
|
+
RegExpLiteral: [],
|
|
91
|
+
RestElement: [...FlowVisitorKeys.RestElement, 'typeAnnotation'],
|
|
92
|
+
StringLiteral: [],
|
|
93
|
+
TupleTypeAnnotation: ['types'],
|
|
94
|
+
CommentBlock: [],
|
|
95
|
+
CommentLine: [],
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
function nodeWith<T extends ESNode>(node: T, overrideProps: Partial<T>): T {
|
|
99
|
+
return SimpleTransform.nodeWith(
|
|
100
|
+
node,
|
|
101
|
+
overrideProps,
|
|
102
|
+
FlowESTreeAndBabelVisitorKeys,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Babel node types
|
|
108
|
+
*
|
|
109
|
+
* Copied from: https://github.com/babel/babel/blob/main/packages/babel-types/src/ast-types/generated/index.ts
|
|
110
|
+
*/
|
|
111
|
+
export interface BabelFile extends BaseNode {
|
|
112
|
+
readonly type: 'File';
|
|
113
|
+
readonly program: Program;
|
|
114
|
+
readonly comments: ReadonlyArray<BabelComment>;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
interface BabelCommentBlock extends BaseToken {
|
|
118
|
+
readonly type: 'CommentBlock';
|
|
119
|
+
readonly value: string;
|
|
120
|
+
}
|
|
121
|
+
interface BabelCommentLine extends BaseToken {
|
|
122
|
+
readonly type: 'CommentLine';
|
|
123
|
+
readonly value: string;
|
|
124
|
+
}
|
|
125
|
+
type BabelComment = BabelCommentBlock | BabelCommentLine;
|
|
126
|
+
|
|
127
|
+
interface BabelObjectMethod extends BaseNode {
|
|
128
|
+
readonly type: 'ObjectMethod';
|
|
129
|
+
readonly kind: 'method' | 'get' | 'set';
|
|
130
|
+
readonly key: Expression;
|
|
131
|
+
readonly params: ReadonlyArray<FunctionParameter>;
|
|
132
|
+
readonly body: BlockStatement;
|
|
133
|
+
readonly computed: boolean;
|
|
134
|
+
readonly generator: boolean;
|
|
135
|
+
readonly async: boolean;
|
|
136
|
+
readonly returnType?: TypeAnnotation | null;
|
|
137
|
+
readonly typeParameters?: TypeParameterDeclaration | null;
|
|
138
|
+
readonly variance?: boolean | null;
|
|
139
|
+
|
|
140
|
+
// TODO: Should these exist?
|
|
141
|
+
// +id: ...,
|
|
142
|
+
// +value: Expression;
|
|
143
|
+
// +method: boolean;
|
|
144
|
+
// +shorthand: boolean;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
interface BabelObjectProperty extends BaseNode {
|
|
148
|
+
readonly type: 'ObjectProperty';
|
|
149
|
+
readonly computed: boolean;
|
|
150
|
+
readonly key: Expression;
|
|
151
|
+
readonly value: Expression;
|
|
152
|
+
readonly method: boolean;
|
|
153
|
+
readonly shorthand: boolean;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
interface BabelClassMethodBase extends BaseNode {
|
|
157
|
+
readonly kind: 'get' | 'set' | 'method' | 'constructor';
|
|
158
|
+
readonly computed: boolean;
|
|
159
|
+
readonly static: boolean;
|
|
160
|
+
readonly key:
|
|
161
|
+
| Identifier
|
|
162
|
+
| StringLiteral
|
|
163
|
+
| ClassPropertyNameComputed
|
|
164
|
+
| ClassPropertyNameNonComputed;
|
|
165
|
+
readonly id: null;
|
|
166
|
+
readonly params: ReadonlyArray<FunctionParameter>;
|
|
167
|
+
readonly body: BlockStatement;
|
|
168
|
+
readonly async: boolean;
|
|
169
|
+
readonly generator: boolean;
|
|
170
|
+
readonly returnType?: TypeAnnotation | null;
|
|
171
|
+
readonly typeParameters?: TypeParameterDeclaration | null;
|
|
172
|
+
readonly predicate: null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
interface BabelClassPrivateMethod extends BabelClassMethodBase {
|
|
176
|
+
readonly type: 'ClassPrivateMethod';
|
|
177
|
+
}
|
|
178
|
+
interface BabelClassMethod extends BabelClassMethodBase {
|
|
179
|
+
readonly type: 'ClassMethod';
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface BabelClassProperty extends BaseNode {
|
|
183
|
+
readonly type: 'ClassProperty';
|
|
184
|
+
readonly key: Expression;
|
|
185
|
+
readonly value: null | Expression;
|
|
186
|
+
readonly typeAnnotation: null | TypeAnnotation;
|
|
187
|
+
readonly static: boolean;
|
|
188
|
+
readonly variance: null | Variance;
|
|
189
|
+
readonly declare: boolean;
|
|
190
|
+
readonly optional: null | boolean;
|
|
191
|
+
readonly computed: boolean;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
interface BabelClassPrivateProperty extends BaseNode {
|
|
195
|
+
readonly type: 'ClassPrivateProperty';
|
|
196
|
+
readonly key: BabelPrivateName;
|
|
197
|
+
readonly value: null | Expression;
|
|
198
|
+
readonly typeAnnotation: null | TypeAnnotation;
|
|
199
|
+
readonly static: boolean;
|
|
200
|
+
readonly variance: null | Variance;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
interface BabelExportNamespaceSpecifier extends BaseNode {
|
|
204
|
+
readonly type: 'ExportNamespaceSpecifier';
|
|
205
|
+
readonly exported: Identifier;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
interface BabelExportNamedDeclaration extends BaseNode {
|
|
209
|
+
readonly type: 'ExportNamedDeclaration';
|
|
210
|
+
readonly declaration?: null;
|
|
211
|
+
readonly specifiers: ReadonlyArray<BabelExportNamespaceSpecifier>;
|
|
212
|
+
readonly source?: StringLiteral | null;
|
|
213
|
+
readonly exportKind: 'value' | 'type';
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
interface BabelPrivateName extends BaseNode {
|
|
217
|
+
readonly type: 'PrivateName';
|
|
218
|
+
readonly id: Identifier;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
interface BabelOptionalMemberExpression extends BaseNode {
|
|
222
|
+
readonly type: 'OptionalMemberExpression';
|
|
223
|
+
readonly object:
|
|
224
|
+
| Expression
|
|
225
|
+
| Super
|
|
226
|
+
| BabelOptionalMemberExpression
|
|
227
|
+
| BabelOptionalCallExpression;
|
|
228
|
+
readonly property: Expression | Identifier | PrivateIdentifier;
|
|
229
|
+
readonly computed: boolean;
|
|
230
|
+
readonly optional: boolean;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
interface BabelOptionalCallExpression extends BaseNode {
|
|
234
|
+
readonly type: 'OptionalCallExpression';
|
|
235
|
+
readonly callee:
|
|
236
|
+
| Expression
|
|
237
|
+
| Super
|
|
238
|
+
| BabelOptionalMemberExpression
|
|
239
|
+
| BabelOptionalCallExpression;
|
|
240
|
+
readonly arguments: ReadonlyArray<Expression | SpreadElement>;
|
|
241
|
+
readonly optional: boolean;
|
|
242
|
+
readonly typeArguments?: TypeParameterInstantiation | null;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
interface BabelStringLiteral extends BaseNode {
|
|
246
|
+
readonly type: 'StringLiteral';
|
|
247
|
+
readonly value: string;
|
|
248
|
+
readonly extra: Readonly<{
|
|
249
|
+
readonly rawValue: string,
|
|
250
|
+
readonly raw: string,
|
|
251
|
+
}>;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
interface BabelNumericLiteral extends BaseNode {
|
|
255
|
+
readonly type: 'NumericLiteral';
|
|
256
|
+
readonly value: number;
|
|
257
|
+
readonly extra: Readonly<{
|
|
258
|
+
readonly rawValue: number,
|
|
259
|
+
readonly raw: string,
|
|
260
|
+
}>;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
interface BabelBigIntLiteral extends BaseNode {
|
|
264
|
+
readonly type: 'BigIntLiteral';
|
|
265
|
+
readonly value: string;
|
|
266
|
+
readonly extra: Readonly<{
|
|
267
|
+
readonly rawValue: string,
|
|
268
|
+
readonly raw: string,
|
|
269
|
+
}>;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
interface BabelBooleanLiteral extends BaseNode {
|
|
273
|
+
readonly type: 'BooleanLiteral';
|
|
274
|
+
readonly value: boolean;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
interface BabelNullLiteral extends BaseNode {
|
|
278
|
+
readonly type: 'NullLiteral';
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
interface BabelRegExpLiteral extends BaseNode {
|
|
282
|
+
readonly type: 'RegExpLiteral';
|
|
283
|
+
readonly extra: Readonly<{
|
|
284
|
+
readonly raw: string,
|
|
285
|
+
}>;
|
|
286
|
+
readonly pattern: string;
|
|
287
|
+
readonly flags: string;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
type BabelLiteral =
|
|
291
|
+
| BabelStringLiteral
|
|
292
|
+
| BabelNumericLiteral
|
|
293
|
+
| BabelBooleanLiteral
|
|
294
|
+
| BabelNullLiteral
|
|
295
|
+
| BabelRegExpLiteral
|
|
296
|
+
| BabelBigIntLiteral;
|
|
297
|
+
|
|
298
|
+
export type ESNodeOrBabelNode =
|
|
299
|
+
| ESNode
|
|
300
|
+
| BabelFile
|
|
301
|
+
| BabelObjectMethod
|
|
302
|
+
| BabelObjectProperty
|
|
303
|
+
| BabelClassPrivateMethod
|
|
304
|
+
| BabelClassMethod
|
|
305
|
+
| BabelClassProperty
|
|
306
|
+
| BabelClassPrivateProperty
|
|
307
|
+
| BabelExportNamespaceSpecifier
|
|
308
|
+
| BabelExportNamedDeclaration
|
|
309
|
+
| BabelPrivateName
|
|
310
|
+
| BabelOptionalMemberExpression
|
|
311
|
+
| BabelOptionalCallExpression
|
|
312
|
+
| BabelLiteral
|
|
313
|
+
| BabelComment;
|
|
314
|
+
|
|
315
|
+
function fixSourceLocation(
|
|
316
|
+
node: ESNodeOrBabelNode,
|
|
317
|
+
_options: ParserOptions,
|
|
318
|
+
): void {
|
|
319
|
+
const loc = node.loc;
|
|
320
|
+
if (loc == null) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// $FlowExpectedError[cannot-write]
|
|
325
|
+
node.loc = {
|
|
326
|
+
start: loc.start,
|
|
327
|
+
end: loc.end,
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
if (node.type === 'Identifier') {
|
|
331
|
+
// $FlowExpectedError[prop-missing]
|
|
332
|
+
node.loc.identifierName = node.name;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// $FlowExpectedError[prop-missing]
|
|
336
|
+
node.start = node.range[0];
|
|
337
|
+
// $FlowExpectedError[prop-missing]
|
|
338
|
+
node.end = node.range[1];
|
|
339
|
+
|
|
340
|
+
// $FlowExpectedError[cannot-write]
|
|
341
|
+
delete node.range;
|
|
342
|
+
|
|
343
|
+
// $FlowExpectedError[cannot-write]
|
|
344
|
+
// $FlowExpectedError[prop-missing]
|
|
345
|
+
delete node.parent;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function mapNodeWithDirectives<T extends Program | BlockStatement>(node: T): T {
|
|
349
|
+
// $FlowExpectedError[prop-missing]
|
|
350
|
+
if (node.directives != null) {
|
|
351
|
+
return node;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const directives = [];
|
|
355
|
+
for (const child of node.body) {
|
|
356
|
+
if (child.type === 'ExpressionStatement' && child.directive != null) {
|
|
357
|
+
// Construct Directive node with DirectiveLiteral value
|
|
358
|
+
directives.push({
|
|
359
|
+
type: 'Directive',
|
|
360
|
+
value: {
|
|
361
|
+
type: 'DirectiveLiteral',
|
|
362
|
+
value: child.directive,
|
|
363
|
+
extra: {
|
|
364
|
+
rawValue: child.directive,
|
|
365
|
+
raw:
|
|
366
|
+
child.expression.type === 'Literal' ? child.expression.raw : '',
|
|
367
|
+
},
|
|
368
|
+
loc: child.expression.loc,
|
|
369
|
+
range: child.expression.range,
|
|
370
|
+
parent: EMPTY_PARENT,
|
|
371
|
+
},
|
|
372
|
+
loc: child.loc,
|
|
373
|
+
range: child.range,
|
|
374
|
+
parent: node,
|
|
375
|
+
});
|
|
376
|
+
} else {
|
|
377
|
+
// Once we have found the first non-directive node we know there cannot be any more directives
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Move directives from body to new directives array
|
|
383
|
+
// $FlowExpectedError[incompatible-type] We are adding properties for babel that don't exist in the ESTree types.
|
|
384
|
+
return nodeWith(node, {
|
|
385
|
+
directives,
|
|
386
|
+
body:
|
|
387
|
+
directives.length === 0 ? node.body : node.body.slice(directives.length),
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function mapProgram(node: Program): BabelFile {
|
|
392
|
+
// Visit child nodes and convert to directives
|
|
393
|
+
const program = mapNodeWithDirectives(node);
|
|
394
|
+
|
|
395
|
+
// Adjust start loc to beginning of file
|
|
396
|
+
const startLoc = {line: 1, column: 0};
|
|
397
|
+
|
|
398
|
+
// Adjust end loc to include last comment if program ends with a comment
|
|
399
|
+
let endLoc = program.loc.end;
|
|
400
|
+
let endRange = program.range[1];
|
|
401
|
+
if (program.comments.length > 0) {
|
|
402
|
+
const lastComment = program.comments[program.comments.length - 1];
|
|
403
|
+
if (lastComment.range[1] > endRange) {
|
|
404
|
+
endLoc = lastComment.loc.end;
|
|
405
|
+
endRange = lastComment.range[1];
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const loc = {
|
|
410
|
+
start: startLoc,
|
|
411
|
+
end: endLoc,
|
|
412
|
+
};
|
|
413
|
+
const range = [0, endRange];
|
|
414
|
+
|
|
415
|
+
const babelComments: ReadonlyArray<BabelComment> = program.comments.map(
|
|
416
|
+
comment => {
|
|
417
|
+
switch (comment.type) {
|
|
418
|
+
case 'Line': {
|
|
419
|
+
return {
|
|
420
|
+
type: 'CommentLine',
|
|
421
|
+
value: comment.value,
|
|
422
|
+
loc: comment.loc,
|
|
423
|
+
range: comment.range,
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
case 'Block': {
|
|
427
|
+
return {
|
|
428
|
+
type: 'CommentBlock',
|
|
429
|
+
value: comment.value,
|
|
430
|
+
loc: comment.loc,
|
|
431
|
+
range: comment.range,
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
// Rename root node to File node and move Program node under program property
|
|
439
|
+
return {
|
|
440
|
+
type: 'File',
|
|
441
|
+
// $FlowExpectedError[incompatible-type] Comments, docblock and tokens are purposely missing to match the Babel AST.
|
|
442
|
+
program: {
|
|
443
|
+
type: 'Program',
|
|
444
|
+
body: program.body,
|
|
445
|
+
// $FlowExpectedError[prop-missing] This is added by `mapNodeWithDirectives`
|
|
446
|
+
directives: program.directives ?? [],
|
|
447
|
+
sourceType: program.sourceType,
|
|
448
|
+
interpreter: program.interpreter,
|
|
449
|
+
// TODO: Add back for BABEL?
|
|
450
|
+
// sourceFile: options.sourceFilename,
|
|
451
|
+
loc,
|
|
452
|
+
range,
|
|
453
|
+
parent: EMPTY_PARENT,
|
|
454
|
+
},
|
|
455
|
+
comments: babelComments,
|
|
456
|
+
loc,
|
|
457
|
+
range,
|
|
458
|
+
parent: EMPTY_PARENT,
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function mapTemplateElement(node: TemplateElement): TemplateElement {
|
|
463
|
+
// Adjust start loc to exclude "`" at beginning of template literal if this is the first quasi,
|
|
464
|
+
// otherwise exclude "}" from previous expression.
|
|
465
|
+
const startCharsToExclude = 1;
|
|
466
|
+
|
|
467
|
+
// Adjust end loc to exclude "`" at end of template literal if this is the last quasi,
|
|
468
|
+
// otherwise exclude "${" from next expression.
|
|
469
|
+
const endCharsToExclude = node.tail ? 1 : 2;
|
|
470
|
+
|
|
471
|
+
// Mutate the existing node to use the new location information.
|
|
472
|
+
// NOTE: because we don't add any new properties here we cannot detect if this change has been
|
|
473
|
+
// made, so its important we don't return a new node other wise it will recursive infinitely.
|
|
474
|
+
|
|
475
|
+
// $FlowExpectedError[cannot-write]
|
|
476
|
+
node.loc = {
|
|
477
|
+
start: {
|
|
478
|
+
line: node.loc.start.line,
|
|
479
|
+
column: node.loc.start.column + startCharsToExclude,
|
|
480
|
+
},
|
|
481
|
+
end: {
|
|
482
|
+
line: node.loc.end.line,
|
|
483
|
+
column: node.loc.end.column - endCharsToExclude,
|
|
484
|
+
},
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
// $FlowExpectedError[cannot-write]
|
|
488
|
+
node.range = [
|
|
489
|
+
node.range[0] + startCharsToExclude,
|
|
490
|
+
node.range[1] - endCharsToExclude,
|
|
491
|
+
];
|
|
492
|
+
|
|
493
|
+
return node;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function mapProperty(node: Property): BabelObjectMethod | BabelObjectProperty {
|
|
497
|
+
const key = node.key;
|
|
498
|
+
const value = node.value;
|
|
499
|
+
|
|
500
|
+
// Convert methods, getters, and setters to ObjectMethod nodes
|
|
501
|
+
if (node.method || node.kind !== 'init') {
|
|
502
|
+
if (value.type !== 'FunctionExpression') {
|
|
503
|
+
throw createSyntaxError(
|
|
504
|
+
node,
|
|
505
|
+
`Invalid method property, the value must be a "FunctionExpression" or "ArrowFunctionExpression. Instead got "${value.type}".`,
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
// Properties under the FunctionExpression value that should be moved
|
|
509
|
+
// to the ObjectMethod node itself.
|
|
510
|
+
const newNode: BabelObjectMethod = {
|
|
511
|
+
type: 'ObjectMethod',
|
|
512
|
+
// Non getter or setter methods have `kind = method`
|
|
513
|
+
kind: node.kind === 'init' ? 'method' : node.kind,
|
|
514
|
+
method: node.kind === 'init' ? true : false,
|
|
515
|
+
computed: node.computed,
|
|
516
|
+
key: key,
|
|
517
|
+
id: null,
|
|
518
|
+
params: value.params,
|
|
519
|
+
body: value.body,
|
|
520
|
+
async: value.async,
|
|
521
|
+
generator: value.generator,
|
|
522
|
+
returnType: value.returnType,
|
|
523
|
+
typeParameters: value.typeParameters,
|
|
524
|
+
loc: node.loc,
|
|
525
|
+
range: node.range,
|
|
526
|
+
parent: node.parent,
|
|
527
|
+
};
|
|
528
|
+
if (node.kind !== 'init') {
|
|
529
|
+
// babel emits an empty variance property on accessors for some reason
|
|
530
|
+
// $FlowExpectedError[cannot-write]
|
|
531
|
+
newNode.variance = null;
|
|
532
|
+
}
|
|
533
|
+
return newNode;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// Non-method property nodes should be renamed to ObjectProperty
|
|
537
|
+
// $FlowExpectedError[incompatible-type]
|
|
538
|
+
const propertyValue: Expression = value;
|
|
539
|
+
return {
|
|
540
|
+
type: 'ObjectProperty',
|
|
541
|
+
computed: node.computed,
|
|
542
|
+
key: node.key,
|
|
543
|
+
value: propertyValue,
|
|
544
|
+
method: node.method,
|
|
545
|
+
shorthand: node.shorthand,
|
|
546
|
+
loc: node.loc,
|
|
547
|
+
range: node.range,
|
|
548
|
+
parent: node.parent,
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function mapMethodDefinition(
|
|
553
|
+
node: MethodDefinition,
|
|
554
|
+
): BabelClassPrivateMethod | BabelClassMethod {
|
|
555
|
+
const value = node.value;
|
|
556
|
+
|
|
557
|
+
const BaseClassMethod = {
|
|
558
|
+
kind: node.kind,
|
|
559
|
+
computed: node.computed,
|
|
560
|
+
static: node.static,
|
|
561
|
+
key: node.key,
|
|
562
|
+
id: null,
|
|
563
|
+
|
|
564
|
+
// Properties under the FunctionExpression value that should be moved
|
|
565
|
+
// to the ClassMethod node itself.
|
|
566
|
+
params: value.params,
|
|
567
|
+
body: value.body,
|
|
568
|
+
async: value.async,
|
|
569
|
+
generator: value.generator,
|
|
570
|
+
returnType: value.returnType,
|
|
571
|
+
typeParameters: value.typeParameters,
|
|
572
|
+
predicate: null,
|
|
573
|
+
|
|
574
|
+
loc: node.loc,
|
|
575
|
+
range: node.range,
|
|
576
|
+
parent: node.parent,
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
if (node.key.type === 'PrivateIdentifier') {
|
|
580
|
+
return {
|
|
581
|
+
...BaseClassMethod,
|
|
582
|
+
type: 'ClassPrivateMethod',
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
return {
|
|
587
|
+
...BaseClassMethod,
|
|
588
|
+
type: 'ClassMethod',
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function mapExportAllDeclaration(
|
|
593
|
+
node: ExportAllDeclaration,
|
|
594
|
+
): ExportAllDeclaration | BabelExportNamedDeclaration {
|
|
595
|
+
if (node.exported != null) {
|
|
596
|
+
return {
|
|
597
|
+
type: 'ExportNamedDeclaration',
|
|
598
|
+
declaration: null,
|
|
599
|
+
specifiers: [
|
|
600
|
+
{
|
|
601
|
+
type: 'ExportNamespaceSpecifier',
|
|
602
|
+
exported: node.exported,
|
|
603
|
+
|
|
604
|
+
// The hermes AST emits the location as the location of the entire export
|
|
605
|
+
// but babel emits the location as *just* the "* as id" bit.
|
|
606
|
+
// The end will always align with the end of the identifier (ezpz)
|
|
607
|
+
// but the start will align with the "*" token - which we can't recover from just the AST
|
|
608
|
+
// so we just fudge the start location a bit to get it "good enough"
|
|
609
|
+
// it will be wrong if the AST is anything like "export * as x from 'y'"... but oh well
|
|
610
|
+
loc: {
|
|
611
|
+
start: {
|
|
612
|
+
column: node.loc.start.column + 'export '.length,
|
|
613
|
+
line: node.loc.start.line,
|
|
614
|
+
},
|
|
615
|
+
end: node.exported.loc.end,
|
|
616
|
+
},
|
|
617
|
+
range: [node.range[0] + 'export '.length, node.exported.range[1]],
|
|
618
|
+
parent: EMPTY_PARENT,
|
|
619
|
+
},
|
|
620
|
+
],
|
|
621
|
+
source: node.source,
|
|
622
|
+
exportKind: node.exportKind,
|
|
623
|
+
loc: node.loc,
|
|
624
|
+
range: node.range,
|
|
625
|
+
parent: node.parent,
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// $FlowExpectedError[cannot-write]
|
|
630
|
+
delete node.exported;
|
|
631
|
+
|
|
632
|
+
return node;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
function mapRestElement(node: RestElement): RestElement {
|
|
636
|
+
// ESTree puts type annotations on rest elements on the argument node,
|
|
637
|
+
// but Babel expects type annotations on the rest element node itself.
|
|
638
|
+
const argument = node.argument;
|
|
639
|
+
if (
|
|
640
|
+
(argument.type === 'Identifier' ||
|
|
641
|
+
argument.type === 'ObjectPattern' ||
|
|
642
|
+
argument.type === 'ArrayPattern') &&
|
|
643
|
+
argument.typeAnnotation != null
|
|
644
|
+
) {
|
|
645
|
+
// Unfortunately there's no way for us to recover the end location of
|
|
646
|
+
// the argument for the general case
|
|
647
|
+
const argumentRange = argument.range;
|
|
648
|
+
let argumentLoc = argument.loc;
|
|
649
|
+
if (argument.type === 'Identifier') {
|
|
650
|
+
argumentRange[1] = argumentRange[0] + argument.name.length;
|
|
651
|
+
argumentLoc = {
|
|
652
|
+
start: argumentLoc.start,
|
|
653
|
+
end: {
|
|
654
|
+
line: argumentLoc.start.line,
|
|
655
|
+
column: argumentLoc.start.column + argument.name.length,
|
|
656
|
+
},
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
return nodeWith(node, {
|
|
660
|
+
typeAnnotation: argument.typeAnnotation,
|
|
661
|
+
argument: nodeWith(argument, {
|
|
662
|
+
typeAnnotation: null,
|
|
663
|
+
range: argumentRange,
|
|
664
|
+
loc: argumentLoc,
|
|
665
|
+
}),
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
return node;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function mapImportExpression(node: ImportExpression): CallExpression {
|
|
673
|
+
// Babel expects ImportExpression to be structured as a regular
|
|
674
|
+
// CallExpression where the callee is an Import node.
|
|
675
|
+
|
|
676
|
+
// $FlowExpectedError[incompatible-type] optional and typeArguments are missing to match existing output.
|
|
677
|
+
return {
|
|
678
|
+
type: 'CallExpression',
|
|
679
|
+
// $FlowExpectedError[incompatible-type] This is a babel specific node
|
|
680
|
+
callee: {
|
|
681
|
+
type: 'Import',
|
|
682
|
+
loc: {
|
|
683
|
+
start: node.loc.start,
|
|
684
|
+
end: {
|
|
685
|
+
line: node.loc.start.line,
|
|
686
|
+
column: node.loc.start.column + 'import'.length,
|
|
687
|
+
},
|
|
688
|
+
},
|
|
689
|
+
range: [node.range[0], node.range[0] + 'import'.length],
|
|
690
|
+
parent: EMPTY_PARENT,
|
|
691
|
+
},
|
|
692
|
+
arguments: [node.source],
|
|
693
|
+
loc: node.loc,
|
|
694
|
+
range: node.range,
|
|
695
|
+
parent: node.parent,
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
function mapPrivateIdentifier(node: PrivateIdentifier): BabelPrivateName {
|
|
700
|
+
return {
|
|
701
|
+
type: 'PrivateName',
|
|
702
|
+
id: {
|
|
703
|
+
type: 'Identifier',
|
|
704
|
+
name: node.name,
|
|
705
|
+
optional: false,
|
|
706
|
+
typeAnnotation: null,
|
|
707
|
+
loc: {
|
|
708
|
+
start: {
|
|
709
|
+
line: node.loc.start.line,
|
|
710
|
+
// babel doesn't include the hash in the identifier
|
|
711
|
+
column: node.loc.start.column + 1,
|
|
712
|
+
},
|
|
713
|
+
end: node.loc.end,
|
|
714
|
+
},
|
|
715
|
+
range: [node.range[0] + 1, node.range[1]],
|
|
716
|
+
parent: EMPTY_PARENT,
|
|
717
|
+
},
|
|
718
|
+
loc: node.loc,
|
|
719
|
+
range: node.range,
|
|
720
|
+
parent: node.parent,
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
function mapPropertyDefinition(
|
|
725
|
+
node: PropertyDefinition,
|
|
726
|
+
): BabelClassProperty | BabelClassPrivateProperty {
|
|
727
|
+
if (node.key.type === 'PrivateIdentifier') {
|
|
728
|
+
return {
|
|
729
|
+
type: 'ClassPrivateProperty',
|
|
730
|
+
key: mapPrivateIdentifier(node.key),
|
|
731
|
+
value: node.value,
|
|
732
|
+
typeAnnotation: node.typeAnnotation,
|
|
733
|
+
static: node.static,
|
|
734
|
+
variance: node.variance,
|
|
735
|
+
loc: node.loc,
|
|
736
|
+
range: node.range,
|
|
737
|
+
parent: node.parent,
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
return {
|
|
742
|
+
type: 'ClassProperty',
|
|
743
|
+
key: node.key,
|
|
744
|
+
value: node.value,
|
|
745
|
+
typeAnnotation: node.typeAnnotation,
|
|
746
|
+
static: node.static,
|
|
747
|
+
variance: node.variance,
|
|
748
|
+
declare: node.declare,
|
|
749
|
+
optional: node.optional,
|
|
750
|
+
computed: node.computed,
|
|
751
|
+
loc: node.loc,
|
|
752
|
+
range: node.range,
|
|
753
|
+
parent: node.parent,
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
function mapTypeofTypeAnnotation(
|
|
758
|
+
node: TypeofTypeAnnotation,
|
|
759
|
+
): TypeofTypeAnnotation {
|
|
760
|
+
// $FlowExpectedError[cannot-write]
|
|
761
|
+
delete node.typeArguments;
|
|
762
|
+
// $FlowFixMe[incompatible-type]
|
|
763
|
+
// $FlowFixMe[invalid-compare]
|
|
764
|
+
if (node.argument.type !== 'GenericTypeAnnotation') {
|
|
765
|
+
return nodeWith(node, {
|
|
766
|
+
// $FlowExpectedError[incompatible-type] Special override for Babel
|
|
767
|
+
argument: {
|
|
768
|
+
type: 'GenericTypeAnnotation',
|
|
769
|
+
id: node.argument,
|
|
770
|
+
typeParameters: null,
|
|
771
|
+
loc: node.argument.loc,
|
|
772
|
+
range: node.argument.range,
|
|
773
|
+
parent: EMPTY_PARENT,
|
|
774
|
+
},
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
return node;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
function mapDeclareVariable(node: DeclareVariable): DeclareVariable {
|
|
781
|
+
if (node.kind != null) {
|
|
782
|
+
// $FlowExpectedError[cannot-write]
|
|
783
|
+
delete node.kind;
|
|
784
|
+
}
|
|
785
|
+
return node;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
function mapJSXElement(node: JSXElement): JSXElement {
|
|
789
|
+
if (node.openingElement.typeArguments != null) {
|
|
790
|
+
// $FlowExpectedError[cannot-write]
|
|
791
|
+
delete node.openingElement.typeArguments;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
return node;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
type ChainExpressionReturnType =
|
|
798
|
+
| BabelOptionalMemberExpression
|
|
799
|
+
| BabelOptionalCallExpression
|
|
800
|
+
| Expression
|
|
801
|
+
| Super;
|
|
802
|
+
|
|
803
|
+
function mapChainExpressionInnerNode(
|
|
804
|
+
node: Expression | Super,
|
|
805
|
+
): ChainExpressionReturnType {
|
|
806
|
+
switch (node.type) {
|
|
807
|
+
case 'MemberExpression': {
|
|
808
|
+
const innerObject = mapChainExpressionInnerNode(node.object);
|
|
809
|
+
if (
|
|
810
|
+
!node.optional &&
|
|
811
|
+
innerObject.type !== 'OptionalMemberExpression' &&
|
|
812
|
+
innerObject.type !== 'OptionalCallExpression'
|
|
813
|
+
) {
|
|
814
|
+
return node;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
return {
|
|
818
|
+
type: 'OptionalMemberExpression',
|
|
819
|
+
object: innerObject,
|
|
820
|
+
property: node.property,
|
|
821
|
+
computed: node.computed,
|
|
822
|
+
optional: node.optional,
|
|
823
|
+
loc: node.loc,
|
|
824
|
+
range: node.range,
|
|
825
|
+
parent: node.parent,
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
case 'CallExpression': {
|
|
829
|
+
const innerCallee = mapChainExpressionInnerNode(node.callee);
|
|
830
|
+
if (
|
|
831
|
+
!node.optional &&
|
|
832
|
+
innerCallee.type !== 'OptionalMemberExpression' &&
|
|
833
|
+
innerCallee.type !== 'OptionalCallExpression'
|
|
834
|
+
) {
|
|
835
|
+
return node;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
return {
|
|
839
|
+
type: 'OptionalCallExpression',
|
|
840
|
+
callee: innerCallee,
|
|
841
|
+
optional: node.optional,
|
|
842
|
+
arguments: node.arguments,
|
|
843
|
+
typeArguments: node.typeArguments,
|
|
844
|
+
loc: node.loc,
|
|
845
|
+
range: node.range,
|
|
846
|
+
parent: node.parent,
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
default: {
|
|
850
|
+
return node;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
function mapChainExpression(node: ChainExpression): ChainExpressionReturnType {
|
|
856
|
+
return mapChainExpressionInnerNode(node.expression);
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
function mapLiteral(node: Literal): BabelLiteral {
|
|
860
|
+
const base = {
|
|
861
|
+
loc: node.loc,
|
|
862
|
+
range: node.range,
|
|
863
|
+
parent: node.parent,
|
|
864
|
+
};
|
|
865
|
+
switch (node.literalType) {
|
|
866
|
+
case 'string': {
|
|
867
|
+
return {
|
|
868
|
+
type: 'StringLiteral',
|
|
869
|
+
value: node.value,
|
|
870
|
+
extra: {
|
|
871
|
+
rawValue: node.value,
|
|
872
|
+
raw: node.raw,
|
|
873
|
+
},
|
|
874
|
+
...base,
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
case 'numeric': {
|
|
878
|
+
return {
|
|
879
|
+
type: 'NumericLiteral',
|
|
880
|
+
value: node.value,
|
|
881
|
+
extra: {
|
|
882
|
+
rawValue: node.value,
|
|
883
|
+
raw: node.raw,
|
|
884
|
+
},
|
|
885
|
+
...base,
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
case 'bigint': {
|
|
889
|
+
return {
|
|
890
|
+
type: 'BigIntLiteral',
|
|
891
|
+
value: node.bigint,
|
|
892
|
+
extra: {
|
|
893
|
+
rawValue: node.bigint,
|
|
894
|
+
raw: node.raw,
|
|
895
|
+
},
|
|
896
|
+
...base,
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
case 'boolean': {
|
|
900
|
+
return {
|
|
901
|
+
type: 'BooleanLiteral',
|
|
902
|
+
value: node.value,
|
|
903
|
+
...base,
|
|
904
|
+
};
|
|
905
|
+
}
|
|
906
|
+
case 'null': {
|
|
907
|
+
return {
|
|
908
|
+
type: 'NullLiteral',
|
|
909
|
+
...base,
|
|
910
|
+
};
|
|
911
|
+
}
|
|
912
|
+
case 'regexp': {
|
|
913
|
+
return {
|
|
914
|
+
type: 'RegExpLiteral',
|
|
915
|
+
extra: {
|
|
916
|
+
raw: node.raw,
|
|
917
|
+
},
|
|
918
|
+
pattern: node.regex.pattern,
|
|
919
|
+
flags: node.regex.flags,
|
|
920
|
+
...base,
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
function transformNode(node: ESNodeOrBabelNode): ESNodeOrBabelNode | null {
|
|
927
|
+
switch (node.type) {
|
|
928
|
+
case 'Program': {
|
|
929
|
+
// Check if we have already processed this node.
|
|
930
|
+
// $FlowFixMe[incompatible-type]
|
|
931
|
+
// $FlowFixMe[invalid-compare]
|
|
932
|
+
if (node.parent?.type === 'File') {
|
|
933
|
+
return node;
|
|
934
|
+
}
|
|
935
|
+
return mapProgram(node);
|
|
936
|
+
}
|
|
937
|
+
case 'BlockStatement': {
|
|
938
|
+
return mapNodeWithDirectives(node);
|
|
939
|
+
}
|
|
940
|
+
case 'Property': {
|
|
941
|
+
return mapProperty(node);
|
|
942
|
+
}
|
|
943
|
+
case 'TemplateElement': {
|
|
944
|
+
return mapTemplateElement(node);
|
|
945
|
+
}
|
|
946
|
+
case 'MethodDefinition': {
|
|
947
|
+
return mapMethodDefinition(node);
|
|
948
|
+
}
|
|
949
|
+
case 'ExportAllDeclaration': {
|
|
950
|
+
return mapExportAllDeclaration(node);
|
|
951
|
+
}
|
|
952
|
+
case 'RestElement': {
|
|
953
|
+
return mapRestElement(node);
|
|
954
|
+
}
|
|
955
|
+
case 'ImportExpression': {
|
|
956
|
+
return mapImportExpression(node);
|
|
957
|
+
}
|
|
958
|
+
case 'PrivateIdentifier': {
|
|
959
|
+
return mapPrivateIdentifier(node);
|
|
960
|
+
}
|
|
961
|
+
case 'PropertyDefinition': {
|
|
962
|
+
return mapPropertyDefinition(node);
|
|
963
|
+
}
|
|
964
|
+
case 'TypeofTypeAnnotation': {
|
|
965
|
+
return mapTypeofTypeAnnotation(node);
|
|
966
|
+
}
|
|
967
|
+
case 'DeclareVariable': {
|
|
968
|
+
return mapDeclareVariable(node);
|
|
969
|
+
}
|
|
970
|
+
case 'JSXElement': {
|
|
971
|
+
return mapJSXElement(node);
|
|
972
|
+
}
|
|
973
|
+
case 'Literal': {
|
|
974
|
+
return mapLiteral(node);
|
|
975
|
+
}
|
|
976
|
+
case 'ChainExpression': {
|
|
977
|
+
return mapChainExpression(node);
|
|
978
|
+
}
|
|
979
|
+
case 'TypeCastExpression': {
|
|
980
|
+
// Babel uses different positions for TypeCastExpression locations.
|
|
981
|
+
|
|
982
|
+
// $FlowExpectedError[cannot-write]
|
|
983
|
+
node.loc.start.column = node.loc.start.column + 1;
|
|
984
|
+
// $FlowExpectedError[cannot-write]
|
|
985
|
+
node.loc.end.column = node.loc.end.column - 1;
|
|
986
|
+
// $FlowExpectedError[cannot-write]
|
|
987
|
+
node.range = [node.range[0] + 1, node.range[1] - 1];
|
|
988
|
+
return node;
|
|
989
|
+
}
|
|
990
|
+
case 'AsExpression': {
|
|
991
|
+
const {typeAnnotation} = node;
|
|
992
|
+
// $FlowExpectedError[cannot-write]
|
|
993
|
+
node.type = 'TypeCastExpression';
|
|
994
|
+
// $FlowExpectedError[cannot-write]
|
|
995
|
+
node.typeAnnotation = {
|
|
996
|
+
type: 'TypeAnnotation',
|
|
997
|
+
typeAnnotation,
|
|
998
|
+
loc: typeAnnotation.loc,
|
|
999
|
+
range: typeAnnotation.range,
|
|
1000
|
+
};
|
|
1001
|
+
return node;
|
|
1002
|
+
}
|
|
1003
|
+
case 'AsConstExpression': {
|
|
1004
|
+
return node.expression;
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
* Babel has a different format for Literals
|
|
1009
|
+
*/
|
|
1010
|
+
case 'NumberLiteralTypeAnnotation': {
|
|
1011
|
+
// $FlowExpectedError[prop-missing]
|
|
1012
|
+
node.extra = {
|
|
1013
|
+
rawValue: node.value,
|
|
1014
|
+
raw: node.raw,
|
|
1015
|
+
};
|
|
1016
|
+
// $FlowExpectedError[cannot-write]
|
|
1017
|
+
delete node.raw;
|
|
1018
|
+
return node;
|
|
1019
|
+
}
|
|
1020
|
+
case 'StringLiteralTypeAnnotation': {
|
|
1021
|
+
// $FlowExpectedError[prop-missing]
|
|
1022
|
+
node.extra = {
|
|
1023
|
+
rawValue: node.value,
|
|
1024
|
+
raw: node.raw,
|
|
1025
|
+
};
|
|
1026
|
+
// $FlowExpectedError[cannot-write]
|
|
1027
|
+
delete node.raw;
|
|
1028
|
+
return node;
|
|
1029
|
+
}
|
|
1030
|
+
case 'BigIntLiteralTypeAnnotation': {
|
|
1031
|
+
// $FlowExpectedError[prop-missing]
|
|
1032
|
+
node.extra = {
|
|
1033
|
+
rawValue: node.bigint,
|
|
1034
|
+
raw: node.raw,
|
|
1035
|
+
};
|
|
1036
|
+
// $FlowExpectedError[cannot-write]
|
|
1037
|
+
delete node.bigint;
|
|
1038
|
+
// $FlowExpectedError[cannot-write]
|
|
1039
|
+
delete node.raw;
|
|
1040
|
+
return node;
|
|
1041
|
+
}
|
|
1042
|
+
case 'BooleanLiteralTypeAnnotation': {
|
|
1043
|
+
// $FlowExpectedError[cannot-write]
|
|
1044
|
+
delete node.raw;
|
|
1045
|
+
return node;
|
|
1046
|
+
}
|
|
1047
|
+
case 'TupleTypeAnnotation': {
|
|
1048
|
+
// $FlowExpectedError[cannot-write]
|
|
1049
|
+
delete node.inexact;
|
|
1050
|
+
// $FlowExpectedError[prop-missing]
|
|
1051
|
+
node.types = node.elementTypes;
|
|
1052
|
+
// $FlowExpectedError[cannot-write]
|
|
1053
|
+
delete node.elementTypes;
|
|
1054
|
+
return node;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
case 'UnknownTypeAnnotation': {
|
|
1058
|
+
return {
|
|
1059
|
+
type: 'GenericTypeAnnotation',
|
|
1060
|
+
id: {
|
|
1061
|
+
type: 'Identifier',
|
|
1062
|
+
name: 'unknown',
|
|
1063
|
+
optional: false,
|
|
1064
|
+
typeAnnotation: null,
|
|
1065
|
+
loc: node.loc,
|
|
1066
|
+
range: node.range,
|
|
1067
|
+
parent: EMPTY_PARENT,
|
|
1068
|
+
},
|
|
1069
|
+
typeParameters: null,
|
|
1070
|
+
loc: node.loc,
|
|
1071
|
+
range: node.range,
|
|
1072
|
+
parent: EMPTY_PARENT,
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
case 'NeverTypeAnnotation': {
|
|
1076
|
+
return {
|
|
1077
|
+
type: 'GenericTypeAnnotation',
|
|
1078
|
+
id: {
|
|
1079
|
+
type: 'Identifier',
|
|
1080
|
+
name: 'never',
|
|
1081
|
+
optional: false,
|
|
1082
|
+
typeAnnotation: null,
|
|
1083
|
+
loc: node.loc,
|
|
1084
|
+
range: node.range,
|
|
1085
|
+
parent: EMPTY_PARENT,
|
|
1086
|
+
},
|
|
1087
|
+
typeParameters: null,
|
|
1088
|
+
loc: node.loc,
|
|
1089
|
+
range: node.range,
|
|
1090
|
+
parent: EMPTY_PARENT,
|
|
1091
|
+
};
|
|
1092
|
+
}
|
|
1093
|
+
case 'UndefinedTypeAnnotation': {
|
|
1094
|
+
return {
|
|
1095
|
+
type: 'GenericTypeAnnotation',
|
|
1096
|
+
id: {
|
|
1097
|
+
type: 'Identifier',
|
|
1098
|
+
name: 'undefined',
|
|
1099
|
+
optional: false,
|
|
1100
|
+
typeAnnotation: null,
|
|
1101
|
+
loc: node.loc,
|
|
1102
|
+
range: node.range,
|
|
1103
|
+
parent: EMPTY_PARENT,
|
|
1104
|
+
},
|
|
1105
|
+
typeParameters: null,
|
|
1106
|
+
loc: node.loc,
|
|
1107
|
+
range: node.range,
|
|
1108
|
+
parent: EMPTY_PARENT,
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
case 'JSXText': {
|
|
1113
|
+
// $FlowExpectedError[prop-missing]
|
|
1114
|
+
node.extra = {
|
|
1115
|
+
rawValue: node.value,
|
|
1116
|
+
raw: node.raw,
|
|
1117
|
+
};
|
|
1118
|
+
// $FlowExpectedError[cannot-write]
|
|
1119
|
+
delete node.raw;
|
|
1120
|
+
return node;
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
/**
|
|
1124
|
+
* Babel condenses there output AST by removing some "falsy" values.
|
|
1125
|
+
*/
|
|
1126
|
+
case 'Identifier': {
|
|
1127
|
+
// Babel only has these values if they are "set"
|
|
1128
|
+
if (node.optional === false || node.optional == null) {
|
|
1129
|
+
// $FlowExpectedError[cannot-write]
|
|
1130
|
+
delete node.optional;
|
|
1131
|
+
}
|
|
1132
|
+
if (node.typeAnnotation == null) {
|
|
1133
|
+
// $FlowExpectedError[cannot-write]
|
|
1134
|
+
delete node.typeAnnotation;
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
return node;
|
|
1138
|
+
}
|
|
1139
|
+
case 'CallExpression': {
|
|
1140
|
+
if (node.optional === false) {
|
|
1141
|
+
// $FlowExpectedError[cannot-write]
|
|
1142
|
+
delete node.optional;
|
|
1143
|
+
}
|
|
1144
|
+
if (node.typeArguments == null) {
|
|
1145
|
+
// $FlowExpectedError[cannot-write]
|
|
1146
|
+
delete node.typeArguments;
|
|
1147
|
+
}
|
|
1148
|
+
return node;
|
|
1149
|
+
}
|
|
1150
|
+
case 'OptionalCallExpression': {
|
|
1151
|
+
if (node.typeArguments == null) {
|
|
1152
|
+
// $FlowExpectedError[cannot-write]
|
|
1153
|
+
delete node.typeArguments;
|
|
1154
|
+
}
|
|
1155
|
+
return node;
|
|
1156
|
+
}
|
|
1157
|
+
case 'MemberExpression': {
|
|
1158
|
+
// $FlowExpectedError[cannot-write]
|
|
1159
|
+
delete node.optional;
|
|
1160
|
+
return node;
|
|
1161
|
+
}
|
|
1162
|
+
case 'ExpressionStatement': {
|
|
1163
|
+
// $FlowExpectedError[cannot-write]
|
|
1164
|
+
delete node.directive;
|
|
1165
|
+
return node;
|
|
1166
|
+
}
|
|
1167
|
+
case 'ObjectPattern':
|
|
1168
|
+
case 'ArrayPattern': {
|
|
1169
|
+
if (node.typeAnnotation == null) {
|
|
1170
|
+
// $FlowExpectedError[cannot-write]
|
|
1171
|
+
delete node.typeAnnotation;
|
|
1172
|
+
}
|
|
1173
|
+
return node;
|
|
1174
|
+
}
|
|
1175
|
+
case 'FunctionDeclaration':
|
|
1176
|
+
case 'FunctionExpression':
|
|
1177
|
+
case 'ArrowFunctionExpression':
|
|
1178
|
+
case 'ClassMethod': {
|
|
1179
|
+
// $FlowExpectedError[prop-missing]
|
|
1180
|
+
// $FlowExpectedError[cannot-write]
|
|
1181
|
+
delete node.expression;
|
|
1182
|
+
if (node.predicate == null) {
|
|
1183
|
+
// $FlowExpectedError[cannot-write]
|
|
1184
|
+
delete node.predicate;
|
|
1185
|
+
}
|
|
1186
|
+
if (node.returnType == null) {
|
|
1187
|
+
// $FlowExpectedError[cannot-write]
|
|
1188
|
+
delete node.returnType;
|
|
1189
|
+
}
|
|
1190
|
+
if (node.typeParameters == null) {
|
|
1191
|
+
// $FlowExpectedError[cannot-write]
|
|
1192
|
+
delete node.typeParameters;
|
|
1193
|
+
}
|
|
1194
|
+
return node;
|
|
1195
|
+
}
|
|
1196
|
+
case 'ObjectMethod': {
|
|
1197
|
+
if (node.returnType == null) {
|
|
1198
|
+
// $FlowExpectedError[cannot-write]
|
|
1199
|
+
delete node.returnType;
|
|
1200
|
+
}
|
|
1201
|
+
if (node.typeParameters == null) {
|
|
1202
|
+
// $FlowExpectedError[cannot-write]
|
|
1203
|
+
delete node.typeParameters;
|
|
1204
|
+
}
|
|
1205
|
+
return node;
|
|
1206
|
+
}
|
|
1207
|
+
case 'ClassPrivateMethod': {
|
|
1208
|
+
if (node.computed === false) {
|
|
1209
|
+
// $FlowExpectedError[cannot-write]
|
|
1210
|
+
delete node.computed;
|
|
1211
|
+
}
|
|
1212
|
+
if (node.predicate == null) {
|
|
1213
|
+
// $FlowExpectedError[cannot-write]
|
|
1214
|
+
delete node.predicate;
|
|
1215
|
+
}
|
|
1216
|
+
if (node.returnType == null) {
|
|
1217
|
+
// $FlowExpectedError[cannot-write]
|
|
1218
|
+
delete node.returnType;
|
|
1219
|
+
}
|
|
1220
|
+
if (node.typeParameters == null) {
|
|
1221
|
+
// $FlowExpectedError[cannot-write]
|
|
1222
|
+
delete node.typeParameters;
|
|
1223
|
+
}
|
|
1224
|
+
return node;
|
|
1225
|
+
}
|
|
1226
|
+
case 'ClassExpression':
|
|
1227
|
+
case 'ClassDeclaration': {
|
|
1228
|
+
if (node.decorators == null || node.decorators.length === 0) {
|
|
1229
|
+
// $FlowExpectedError[cannot-write]
|
|
1230
|
+
delete node.decorators;
|
|
1231
|
+
}
|
|
1232
|
+
if (node.implements == null || node.implements.length === 0) {
|
|
1233
|
+
// $FlowExpectedError[cannot-write]
|
|
1234
|
+
delete node.implements;
|
|
1235
|
+
}
|
|
1236
|
+
if (node.superTypeArguments == null) {
|
|
1237
|
+
// $FlowExpectedError[cannot-write]
|
|
1238
|
+
delete node.superTypeArguments;
|
|
1239
|
+
} else {
|
|
1240
|
+
// $FlowExpectedError[cannot-write]
|
|
1241
|
+
// $FlowExpectedError[prop-missing]
|
|
1242
|
+
node.superTypeParameters = node.superTypeArguments;
|
|
1243
|
+
// $FlowExpectedError[cannot-write]
|
|
1244
|
+
delete node.superTypeArguments;
|
|
1245
|
+
}
|
|
1246
|
+
if (node.typeParameters == null) {
|
|
1247
|
+
// $FlowExpectedError[cannot-write]
|
|
1248
|
+
delete node.typeParameters;
|
|
1249
|
+
}
|
|
1250
|
+
return node;
|
|
1251
|
+
}
|
|
1252
|
+
case 'ClassProperty': {
|
|
1253
|
+
if (node.optional === false) {
|
|
1254
|
+
// $FlowExpectedError[cannot-write]
|
|
1255
|
+
delete node.optional;
|
|
1256
|
+
}
|
|
1257
|
+
if (node.declare === false) {
|
|
1258
|
+
// $FlowExpectedError[cannot-write]
|
|
1259
|
+
delete node.declare;
|
|
1260
|
+
}
|
|
1261
|
+
if (node.typeAnnotation == null) {
|
|
1262
|
+
// $FlowExpectedError[cannot-write]
|
|
1263
|
+
delete node.typeAnnotation;
|
|
1264
|
+
}
|
|
1265
|
+
return node;
|
|
1266
|
+
}
|
|
1267
|
+
case 'ClassPrivateProperty': {
|
|
1268
|
+
if (node.typeAnnotation == null) {
|
|
1269
|
+
// $FlowExpectedError[cannot-write]
|
|
1270
|
+
delete node.typeAnnotation;
|
|
1271
|
+
}
|
|
1272
|
+
return node;
|
|
1273
|
+
}
|
|
1274
|
+
case 'ExportNamedDeclaration': {
|
|
1275
|
+
if (node.declaration == null) {
|
|
1276
|
+
// $FlowExpectedError[cannot-write]
|
|
1277
|
+
delete node.declaration;
|
|
1278
|
+
}
|
|
1279
|
+
return node;
|
|
1280
|
+
}
|
|
1281
|
+
case 'ImportDeclaration': {
|
|
1282
|
+
if (node.attributes == null || node.attributes.length === 0) {
|
|
1283
|
+
// $FlowExpectedError[cannot-write]
|
|
1284
|
+
delete node.attributes;
|
|
1285
|
+
}
|
|
1286
|
+
return node;
|
|
1287
|
+
}
|
|
1288
|
+
case 'ArrayExpression': {
|
|
1289
|
+
// $FlowExpectedError[cannot-write]
|
|
1290
|
+
delete node.trailingComma;
|
|
1291
|
+
return node;
|
|
1292
|
+
}
|
|
1293
|
+
case 'JSXOpeningElement': {
|
|
1294
|
+
if (node.typeArguments == null) {
|
|
1295
|
+
// $FlowExpectedError[cannot-write]
|
|
1296
|
+
delete node.typeArguments;
|
|
1297
|
+
}
|
|
1298
|
+
return node;
|
|
1299
|
+
}
|
|
1300
|
+
case 'DeclareOpaqueType':
|
|
1301
|
+
case 'OpaqueType': {
|
|
1302
|
+
if (node.lowerBound != null) {
|
|
1303
|
+
// $FlowExpectedError[cannot-write]
|
|
1304
|
+
delete node.lowerBound;
|
|
1305
|
+
}
|
|
1306
|
+
if (node.upperBound != null) {
|
|
1307
|
+
// $FlowExpectedError[cannot-write]
|
|
1308
|
+
delete node.upperBound;
|
|
1309
|
+
}
|
|
1310
|
+
return node;
|
|
1311
|
+
}
|
|
1312
|
+
default: {
|
|
1313
|
+
return node;
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
export function transformProgram(
|
|
1319
|
+
program: Program,
|
|
1320
|
+
options: ParserOptions,
|
|
1321
|
+
): BabelFile {
|
|
1322
|
+
const resultNode = SimpleTransform.transform(program, {
|
|
1323
|
+
transform(node) {
|
|
1324
|
+
// $FlowExpectedError[incompatible-type] We override the type to support the additional Babel types
|
|
1325
|
+
return transformNode(node);
|
|
1326
|
+
},
|
|
1327
|
+
visitorKeys: FlowESTreeAndBabelVisitorKeys,
|
|
1328
|
+
});
|
|
1329
|
+
|
|
1330
|
+
// $FlowExpectedError[incompatible-type] We override the type to support the additional Babel types
|
|
1331
|
+
SimpleTraverser.traverse(resultNode, {
|
|
1332
|
+
enter(node) {
|
|
1333
|
+
fixSourceLocation(node, options);
|
|
1334
|
+
},
|
|
1335
|
+
leave() {},
|
|
1336
|
+
visitorKeys: FlowESTreeAndBabelVisitorKeys,
|
|
1337
|
+
});
|
|
1338
|
+
|
|
1339
|
+
// $FlowFixMe[incompatible-type]
|
|
1340
|
+
// $FlowFixMe[invalid-compare]
|
|
1341
|
+
if (resultNode?.type === 'File') {
|
|
1342
|
+
return resultNode;
|
|
1343
|
+
}
|
|
1344
|
+
throw new Error(
|
|
1345
|
+
`Unknown AST node of type "${
|
|
1346
|
+
resultNode?.type ?? 'NULL'
|
|
1347
|
+
}" returned from Babel conversion`,
|
|
1348
|
+
);
|
|
1349
|
+
}
|