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