hermes-parser 0.15.1 → 0.17.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.
@@ -0,0 +1,1246 @@
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-call] 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 = program.comments.map(comment => {
410
+ switch (comment.type) {
411
+ case 'Line': {
412
+ return {
413
+ type: 'CommentLine',
414
+ value: comment.value,
415
+ loc: comment.loc,
416
+ range: comment.range,
417
+ };
418
+ }
419
+ case 'Block': {
420
+ return {
421
+ type: 'CommentBlock',
422
+ value: comment.value,
423
+ loc: comment.loc,
424
+ range: comment.range,
425
+ };
426
+ }
427
+ }
428
+ });
429
+
430
+ // Rename root node to File node and move Program node under program property
431
+ return {
432
+ type: 'File',
433
+ // $FlowExpectedError[prop-missing] Comments, docblock and tokens are purposely missing to match the Babel AST.
434
+ program: {
435
+ type: 'Program',
436
+ body: program.body,
437
+ // $FlowExpectedError[prop-missing] This is added by `mapNodeWithDirectives`
438
+ directives: program.directives ?? [],
439
+ sourceType: program.sourceType,
440
+ interpreter: program.interpreter,
441
+ // TODO: Add back for BABEL?
442
+ // sourceFile: options.sourceFilename,
443
+ loc,
444
+ range,
445
+ parent: EMPTY_PARENT,
446
+ },
447
+ comments: babelComments,
448
+ loc,
449
+ range,
450
+ parent: EMPTY_PARENT,
451
+ };
452
+ }
453
+
454
+ function mapTemplateElement(node: TemplateElement): TemplateElement {
455
+ // Adjust start loc to exclude "`" at beginning of template literal if this is the first quasi,
456
+ // otherwise exclude "}" from previous expression.
457
+ const startCharsToExclude = 1;
458
+
459
+ // Adjust end loc to exclude "`" at end of template literal if this is the last quasi,
460
+ // otherwise exclude "${" from next expression.
461
+ const endCharsToExclude = node.tail ? 1 : 2;
462
+
463
+ // Mutate the existing node to use the new location information.
464
+ // NOTE: because we don't add any new properties here we cannot detect if this change has been
465
+ // made, so its important we don't return a new node other wise it will recursive infinitely.
466
+
467
+ // $FlowExpectedError[cannot-write]
468
+ node.loc = {
469
+ start: {
470
+ line: node.loc.start.line,
471
+ column: node.loc.start.column + startCharsToExclude,
472
+ },
473
+ end: {
474
+ line: node.loc.end.line,
475
+ column: node.loc.end.column - endCharsToExclude,
476
+ },
477
+ };
478
+
479
+ // $FlowExpectedError[cannot-write]
480
+ node.range = [
481
+ node.range[0] + startCharsToExclude,
482
+ node.range[1] - endCharsToExclude,
483
+ ];
484
+
485
+ return node;
486
+ }
487
+
488
+ function mapProperty(node: Property): BabelObjectMethod | BabelObjectProperty {
489
+ const key = node.key;
490
+ const value = node.value;
491
+
492
+ // Convert methods, getters, and setters to ObjectMethod nodes
493
+ if (node.method || node.kind !== 'init') {
494
+ if (value.type !== 'FunctionExpression') {
495
+ throw createSyntaxError(
496
+ node,
497
+ `Invalid method property, the value must be a "FunctionExpression" or "ArrowFunctionExpression. Instead got "${value.type}".`,
498
+ );
499
+ }
500
+ // Properties under the FunctionExpression value that should be moved
501
+ // to the ObjectMethod node itself.
502
+ const newNode: BabelObjectMethod = {
503
+ type: 'ObjectMethod',
504
+ // Non getter or setter methods have `kind = method`
505
+ kind: node.kind === 'init' ? 'method' : node.kind,
506
+ method: node.kind === 'init' ? true : false,
507
+ computed: node.computed,
508
+ key: key,
509
+ id: null,
510
+ params: value.params,
511
+ body: value.body,
512
+ async: value.async,
513
+ generator: value.generator,
514
+ returnType: value.returnType,
515
+ typeParameters: value.typeParameters,
516
+ loc: node.loc,
517
+ range: node.range,
518
+ parent: node.parent,
519
+ };
520
+ if (node.kind !== 'init') {
521
+ // babel emits an empty variance property on accessors for some reason
522
+ // $FlowExpectedError[cannot-write]
523
+ newNode.variance = null;
524
+ }
525
+ return newNode;
526
+ }
527
+
528
+ // Non-method property nodes should be renamed to ObjectProperty
529
+ return {
530
+ type: 'ObjectProperty',
531
+ computed: node.computed,
532
+ key: node.key,
533
+ // $FlowExpectedError[incompatible-cast]
534
+ value: (node.value: Expression),
535
+ method: node.method,
536
+ shorthand: node.shorthand,
537
+ loc: node.loc,
538
+ range: node.range,
539
+ parent: node.parent,
540
+ };
541
+ }
542
+
543
+ function mapMethodDefinition(
544
+ node: MethodDefinition,
545
+ ): BabelClassPrivateMethod | BabelClassMethod {
546
+ const value = node.value;
547
+
548
+ const BaseClassMethod = {
549
+ kind: node.kind,
550
+ computed: node.computed,
551
+ static: node.static,
552
+ key: node.key,
553
+ id: null,
554
+
555
+ // Properties under the FunctionExpression value that should be moved
556
+ // to the ClassMethod node itself.
557
+ params: value.params,
558
+ body: value.body,
559
+ async: value.async,
560
+ generator: value.generator,
561
+ returnType: value.returnType,
562
+ typeParameters: value.typeParameters,
563
+ predicate: null,
564
+
565
+ loc: node.loc,
566
+ range: node.range,
567
+ parent: node.parent,
568
+ };
569
+
570
+ if (node.key.type === 'PrivateIdentifier') {
571
+ return {
572
+ ...BaseClassMethod,
573
+ type: 'ClassPrivateMethod',
574
+ };
575
+ }
576
+
577
+ return {
578
+ ...BaseClassMethod,
579
+ type: 'ClassMethod',
580
+ };
581
+ }
582
+
583
+ function mapExportAllDeclaration(
584
+ node: ExportAllDeclaration,
585
+ ): ExportAllDeclaration | BabelExportNamedDeclaration {
586
+ if (node.exported != null) {
587
+ return {
588
+ type: 'ExportNamedDeclaration',
589
+ declaration: null,
590
+ specifiers: [
591
+ {
592
+ type: 'ExportNamespaceSpecifier',
593
+ exported: node.exported,
594
+
595
+ // The hermes AST emits the location as the location of the entire export
596
+ // but babel emits the location as *just* the "* as id" bit.
597
+ // The end will always align with the end of the identifier (ezpz)
598
+ // but the start will align with the "*" token - which we can't recover from just the AST
599
+ // so we just fudge the start location a bit to get it "good enough"
600
+ // it will be wrong if the AST is anything like "export * as x from 'y'"... but oh well
601
+ loc: {
602
+ start: {
603
+ column: node.loc.start.column + 'export '.length,
604
+ line: node.loc.start.line,
605
+ },
606
+ end: node.exported.loc.end,
607
+ },
608
+ range: [node.range[0] + 'export '.length, node.exported.range[1]],
609
+ parent: EMPTY_PARENT,
610
+ },
611
+ ],
612
+ source: node.source,
613
+ exportKind: node.exportKind,
614
+ loc: node.loc,
615
+ range: node.range,
616
+ parent: node.parent,
617
+ };
618
+ }
619
+
620
+ // $FlowExpectedError[cannot-write]
621
+ delete node.exported;
622
+
623
+ return node;
624
+ }
625
+
626
+ function mapRestElement(node: RestElement): RestElement {
627
+ // ESTree puts type annotations on rest elements on the argument node,
628
+ // but Babel expects type annotations on the rest element node itself.
629
+ const argument = node.argument;
630
+ if (
631
+ (argument.type === 'Identifier' ||
632
+ argument.type === 'ObjectPattern' ||
633
+ argument.type === 'ArrayPattern') &&
634
+ argument.typeAnnotation != null
635
+ ) {
636
+ // Unfortunately there's no way for us to recover the end location of
637
+ // the argument for the general case
638
+ const argumentRange = argument.range;
639
+ let argumentLoc = argument.loc;
640
+ if (argument.type === 'Identifier') {
641
+ argumentRange[1] = argumentRange[0] + argument.name.length;
642
+ argumentLoc = {
643
+ start: argumentLoc.start,
644
+ end: {
645
+ line: argumentLoc.start.line,
646
+ column: argumentLoc.start.column + argument.name.length,
647
+ },
648
+ };
649
+ }
650
+ return nodeWith(node, {
651
+ typeAnnotation: argument.typeAnnotation,
652
+ argument: nodeWith(argument, {
653
+ typeAnnotation: null,
654
+ range: argumentRange,
655
+ loc: argumentLoc,
656
+ }),
657
+ });
658
+ }
659
+
660
+ return node;
661
+ }
662
+
663
+ function mapImportExpression(node: ImportExpression): CallExpression {
664
+ // Babel expects ImportExpression to be structured as a regular
665
+ // CallExpression where the callee is an Import node.
666
+
667
+ // $FlowExpectedError[prop-missing] optional and typeArguments are missing to match existing output.
668
+ return {
669
+ type: 'CallExpression',
670
+ // $FlowExpectedError[incompatible-return] This is a babel specific node
671
+ callee: {
672
+ type: 'Import',
673
+ loc: {
674
+ start: node.loc.start,
675
+ end: {
676
+ line: node.loc.start.line,
677
+ column: node.loc.start.column + 'import'.length,
678
+ },
679
+ },
680
+ range: [node.range[0], node.range[0] + 'import'.length],
681
+ parent: EMPTY_PARENT,
682
+ },
683
+ arguments: [node.source],
684
+ loc: node.loc,
685
+ range: node.range,
686
+ parent: node.parent,
687
+ };
688
+ }
689
+
690
+ function mapPrivateIdentifier(node: PrivateIdentifier): BabelPrivateName {
691
+ return {
692
+ type: 'PrivateName',
693
+ id: {
694
+ type: 'Identifier',
695
+ name: node.name,
696
+ optional: false,
697
+ typeAnnotation: null,
698
+ loc: {
699
+ start: {
700
+ line: node.loc.start.line,
701
+ // babel doesn't include the hash in the identifier
702
+ column: node.loc.start.column + 1,
703
+ },
704
+ end: node.loc.end,
705
+ },
706
+ range: [node.range[0] + 1, node.range[1]],
707
+ parent: EMPTY_PARENT,
708
+ },
709
+ loc: node.loc,
710
+ range: node.range,
711
+ parent: node.parent,
712
+ };
713
+ }
714
+
715
+ function mapPropertyDefinition(
716
+ node: PropertyDefinition,
717
+ ): BabelClassProperty | BabelClassPrivateProperty {
718
+ if (node.key.type === 'PrivateIdentifier') {
719
+ return {
720
+ type: 'ClassPrivateProperty',
721
+ key: mapPrivateIdentifier(node.key),
722
+ value: node.value,
723
+ typeAnnotation: node.typeAnnotation,
724
+ static: node.static,
725
+ variance: node.variance,
726
+ loc: node.loc,
727
+ range: node.range,
728
+ parent: node.parent,
729
+ };
730
+ }
731
+
732
+ return {
733
+ type: 'ClassProperty',
734
+ key: node.key,
735
+ value: node.value,
736
+ typeAnnotation: node.typeAnnotation,
737
+ static: node.static,
738
+ variance: node.variance,
739
+ declare: node.declare,
740
+ optional: node.optional,
741
+ computed: node.computed,
742
+ loc: node.loc,
743
+ range: node.range,
744
+ parent: node.parent,
745
+ };
746
+ }
747
+
748
+ function mapTypeofTypeAnnotation(
749
+ node: TypeofTypeAnnotation,
750
+ ): TypeofTypeAnnotation {
751
+ if (node.argument.type !== 'GenericTypeAnnotation') {
752
+ return nodeWith(node, {
753
+ // $FlowExpectedError[incompatible-call] Special override for Babel
754
+ argument: {
755
+ type: 'GenericTypeAnnotation',
756
+ id: node.argument,
757
+ typeParameters: null,
758
+ loc: node.argument.loc,
759
+ range: node.argument.range,
760
+ parent: EMPTY_PARENT,
761
+ },
762
+ });
763
+ }
764
+ return node;
765
+ }
766
+
767
+ function mapDeclareVariable(node: DeclareVariable): DeclareVariable {
768
+ if (node.kind != null) {
769
+ // $FlowExpectedError[cannot-write]
770
+ delete node.kind;
771
+ }
772
+ return node;
773
+ }
774
+
775
+ function mapJSXElement(node: JSXElement): JSXElement {
776
+ if (node.openingElement.typeArguments != null) {
777
+ // $FlowExpectedError[cannot-write]
778
+ delete node.openingElement.typeArguments;
779
+ }
780
+
781
+ return node;
782
+ }
783
+
784
+ type ChainExpressionReturnType =
785
+ | BabelOptionalMemberExpression
786
+ | BabelOptionalCallExpression
787
+ | Expression
788
+ | Super;
789
+
790
+ function mapChainExpressionInnerNode(
791
+ node: Expression | Super,
792
+ ): ChainExpressionReturnType {
793
+ switch (node.type) {
794
+ case 'MemberExpression': {
795
+ const innerObject = mapChainExpressionInnerNode(node.object);
796
+ if (
797
+ !node.optional &&
798
+ innerObject.type !== 'OptionalMemberExpression' &&
799
+ innerObject.type !== 'OptionalCallExpression'
800
+ ) {
801
+ return node;
802
+ }
803
+
804
+ return {
805
+ type: 'OptionalMemberExpression',
806
+ object: innerObject,
807
+ property: node.property,
808
+ computed: node.computed,
809
+ optional: node.optional,
810
+ loc: node.loc,
811
+ range: node.range,
812
+ parent: node.parent,
813
+ };
814
+ }
815
+ case 'CallExpression': {
816
+ const innerCallee = mapChainExpressionInnerNode(node.callee);
817
+ if (
818
+ !node.optional &&
819
+ innerCallee.type !== 'OptionalMemberExpression' &&
820
+ innerCallee.type !== 'OptionalCallExpression'
821
+ ) {
822
+ return node;
823
+ }
824
+
825
+ return {
826
+ type: 'OptionalCallExpression',
827
+ callee: innerCallee,
828
+ optional: node.optional,
829
+ arguments: node.arguments,
830
+ typeArguments: node.typeArguments,
831
+ loc: node.loc,
832
+ range: node.range,
833
+ parent: node.parent,
834
+ };
835
+ }
836
+ default: {
837
+ return node;
838
+ }
839
+ }
840
+ }
841
+
842
+ function mapChainExpression(node: ChainExpression): ChainExpressionReturnType {
843
+ return mapChainExpressionInnerNode(node.expression);
844
+ }
845
+
846
+ function mapLiteral(node: Literal): BabelLiteral {
847
+ const base = {
848
+ loc: node.loc,
849
+ range: node.range,
850
+ parent: node.parent,
851
+ };
852
+ switch (node.literalType) {
853
+ case 'string': {
854
+ return {
855
+ type: 'StringLiteral',
856
+ value: node.value,
857
+ extra: {
858
+ rawValue: node.value,
859
+ raw: node.raw,
860
+ },
861
+ ...base,
862
+ };
863
+ }
864
+ case 'numeric': {
865
+ return {
866
+ type: 'NumericLiteral',
867
+ value: node.value,
868
+ extra: {
869
+ rawValue: node.value,
870
+ raw: node.raw,
871
+ },
872
+ ...base,
873
+ };
874
+ }
875
+ case 'bigint': {
876
+ return {
877
+ type: 'BigIntLiteral',
878
+ value: node.bigint,
879
+ extra: {
880
+ rawValue: node.bigint,
881
+ raw: node.raw,
882
+ },
883
+ ...base,
884
+ };
885
+ }
886
+ case 'boolean': {
887
+ return {
888
+ type: 'BooleanLiteral',
889
+ value: node.value,
890
+ ...base,
891
+ };
892
+ }
893
+ case 'null': {
894
+ return {
895
+ type: 'NullLiteral',
896
+ ...base,
897
+ };
898
+ }
899
+ case 'regexp': {
900
+ return {
901
+ type: 'RegExpLiteral',
902
+ extra: {
903
+ raw: node.raw,
904
+ },
905
+ pattern: node.regex.pattern,
906
+ flags: node.regex.flags,
907
+ ...base,
908
+ };
909
+ }
910
+ }
911
+ }
912
+
913
+ function transformNode(node: ESNodeOrBabelNode): ESNodeOrBabelNode | null {
914
+ switch (node.type) {
915
+ case 'Program': {
916
+ // Check if we have already processed this node.
917
+ if (node.parent?.type === 'File') {
918
+ return node;
919
+ }
920
+ return mapProgram(node);
921
+ }
922
+ case 'BlockStatement': {
923
+ return mapNodeWithDirectives(node);
924
+ }
925
+ case 'Property': {
926
+ return mapProperty(node);
927
+ }
928
+ case 'TemplateElement': {
929
+ return mapTemplateElement(node);
930
+ }
931
+ case 'MethodDefinition': {
932
+ return mapMethodDefinition(node);
933
+ }
934
+ case 'ExportAllDeclaration': {
935
+ return mapExportAllDeclaration(node);
936
+ }
937
+ case 'RestElement': {
938
+ return mapRestElement(node);
939
+ }
940
+ case 'ImportExpression': {
941
+ return mapImportExpression(node);
942
+ }
943
+ case 'PrivateIdentifier': {
944
+ return mapPrivateIdentifier(node);
945
+ }
946
+ case 'PropertyDefinition': {
947
+ return mapPropertyDefinition(node);
948
+ }
949
+ case 'TypeofTypeAnnotation': {
950
+ return mapTypeofTypeAnnotation(node);
951
+ }
952
+ case 'DeclareVariable': {
953
+ return mapDeclareVariable(node);
954
+ }
955
+ case 'JSXElement': {
956
+ return mapJSXElement(node);
957
+ }
958
+ case 'Literal': {
959
+ return mapLiteral(node);
960
+ }
961
+ case 'ChainExpression': {
962
+ return mapChainExpression(node);
963
+ }
964
+ case 'TypeCastExpression': {
965
+ // Babel uses different positions for TypeCastExpression locations.
966
+
967
+ // $FlowExpectedError[cannot-write]
968
+ node.loc.start.column = node.loc.start.column + 1;
969
+ // $FlowExpectedError[cannot-write]
970
+ node.loc.end.column = node.loc.end.column - 1;
971
+ // $FlowExpectedError[cannot-write]
972
+ node.range = [node.range[0] + 1, node.range[1] - 1];
973
+ return node;
974
+ }
975
+ case 'AsExpression': {
976
+ const {typeAnnotation} = node;
977
+ // $FlowExpectedError[cannot-write]
978
+ node.type = 'TypeCastExpression';
979
+ // $FlowExpectedError[cannot-write]
980
+ node.typeAnnotation = {
981
+ type: 'TypeAnnotation',
982
+ typeAnnotation,
983
+ loc: typeAnnotation.loc,
984
+ range: typeAnnotation.range,
985
+ };
986
+ return node;
987
+ }
988
+
989
+ /**
990
+ * Babel has a different format for Literals
991
+ */
992
+ case 'NumberLiteralTypeAnnotation': {
993
+ // $FlowExpectedError[prop-missing]
994
+ node.extra = {
995
+ rawValue: node.value,
996
+ raw: node.raw,
997
+ };
998
+ // $FlowExpectedError[cannot-write]
999
+ delete node.raw;
1000
+ return node;
1001
+ }
1002
+ case 'StringLiteralTypeAnnotation': {
1003
+ // $FlowExpectedError[prop-missing]
1004
+ node.extra = {
1005
+ rawValue: node.value,
1006
+ raw: node.raw,
1007
+ };
1008
+ // $FlowExpectedError[cannot-write]
1009
+ delete node.raw;
1010
+ return node;
1011
+ }
1012
+ case 'BigIntLiteralTypeAnnotation': {
1013
+ // $FlowExpectedError[prop-missing]
1014
+ node.extra = {
1015
+ rawValue: node.bigint,
1016
+ raw: node.raw,
1017
+ };
1018
+ // $FlowExpectedError[cannot-write]
1019
+ delete node.bigint;
1020
+ // $FlowExpectedError[cannot-write]
1021
+ delete node.raw;
1022
+ return node;
1023
+ }
1024
+ case 'BooleanLiteralTypeAnnotation': {
1025
+ // $FlowExpectedError[cannot-write]
1026
+ delete node.raw;
1027
+ return node;
1028
+ }
1029
+ case 'JSXText': {
1030
+ // $FlowExpectedError[prop-missing]
1031
+ node.extra = {
1032
+ rawValue: node.value,
1033
+ raw: node.raw,
1034
+ };
1035
+ // $FlowExpectedError[cannot-write]
1036
+ delete node.raw;
1037
+ return node;
1038
+ }
1039
+
1040
+ /**
1041
+ * Babel condenses there output AST by removing some "falsy" values.
1042
+ */
1043
+ case 'Identifier': {
1044
+ // Babel only has these values if they are "set"
1045
+ if (node.optional === false || node.optional == null) {
1046
+ // $FlowExpectedError[cannot-write]
1047
+ delete node.optional;
1048
+ }
1049
+ if (node.typeAnnotation == null) {
1050
+ // $FlowExpectedError[cannot-write]
1051
+ delete node.typeAnnotation;
1052
+ }
1053
+
1054
+ return node;
1055
+ }
1056
+ case 'CallExpression': {
1057
+ if (node.optional === false) {
1058
+ // $FlowExpectedError[cannot-write]
1059
+ delete node.optional;
1060
+ }
1061
+ if (node.typeArguments == null) {
1062
+ // $FlowExpectedError[cannot-write]
1063
+ delete node.typeArguments;
1064
+ }
1065
+ return node;
1066
+ }
1067
+ case 'OptionalCallExpression': {
1068
+ if (node.typeArguments == null) {
1069
+ // $FlowExpectedError[cannot-write]
1070
+ delete node.typeArguments;
1071
+ }
1072
+ return node;
1073
+ }
1074
+ case 'MemberExpression': {
1075
+ // $FlowExpectedError[cannot-write]
1076
+ delete node.optional;
1077
+ return node;
1078
+ }
1079
+ case 'ExpressionStatement': {
1080
+ // $FlowExpectedError[cannot-write]
1081
+ delete node.directive;
1082
+ return node;
1083
+ }
1084
+ case 'ObjectPattern':
1085
+ case 'ArrayPattern': {
1086
+ if (node.typeAnnotation == null) {
1087
+ // $FlowExpectedError[cannot-write]
1088
+ delete node.typeAnnotation;
1089
+ }
1090
+ return node;
1091
+ }
1092
+ case 'FunctionDeclaration':
1093
+ case 'FunctionExpression':
1094
+ case 'ArrowFunctionExpression':
1095
+ case 'ClassMethod': {
1096
+ // $FlowExpectedError[prop-missing]
1097
+ // $FlowExpectedError[cannot-write]
1098
+ delete node.expression;
1099
+ if (node.predicate == null) {
1100
+ // $FlowExpectedError[cannot-write]
1101
+ delete node.predicate;
1102
+ }
1103
+ if (node.returnType == null) {
1104
+ // $FlowExpectedError[cannot-write]
1105
+ delete node.returnType;
1106
+ }
1107
+ if (node.typeParameters == null) {
1108
+ // $FlowExpectedError[cannot-write]
1109
+ delete node.typeParameters;
1110
+ }
1111
+ return node;
1112
+ }
1113
+ case 'ObjectMethod': {
1114
+ if (node.returnType == null) {
1115
+ // $FlowExpectedError[cannot-write]
1116
+ delete node.returnType;
1117
+ }
1118
+ if (node.typeParameters == null) {
1119
+ // $FlowExpectedError[cannot-write]
1120
+ delete node.typeParameters;
1121
+ }
1122
+ return node;
1123
+ }
1124
+ case 'ClassPrivateMethod': {
1125
+ if (node.computed === false) {
1126
+ // $FlowExpectedError[cannot-write]
1127
+ delete node.computed;
1128
+ }
1129
+ if (node.predicate == null) {
1130
+ // $FlowExpectedError[cannot-write]
1131
+ delete node.predicate;
1132
+ }
1133
+ if (node.returnType == null) {
1134
+ // $FlowExpectedError[cannot-write]
1135
+ delete node.returnType;
1136
+ }
1137
+ if (node.typeParameters == null) {
1138
+ // $FlowExpectedError[cannot-write]
1139
+ delete node.typeParameters;
1140
+ }
1141
+ return node;
1142
+ }
1143
+ case 'ClassExpression':
1144
+ case 'ClassDeclaration': {
1145
+ if (node.decorators == null || node.decorators.length === 0) {
1146
+ // $FlowExpectedError[cannot-write]
1147
+ delete node.decorators;
1148
+ }
1149
+ if (node.implements == null || node.implements.length === 0) {
1150
+ // $FlowExpectedError[cannot-write]
1151
+ delete node.implements;
1152
+ }
1153
+ if (node.superTypeParameters == null) {
1154
+ // $FlowExpectedError[cannot-write]
1155
+ delete node.superTypeParameters;
1156
+ }
1157
+ if (node.typeParameters == null) {
1158
+ // $FlowExpectedError[cannot-write]
1159
+ delete node.typeParameters;
1160
+ }
1161
+ return node;
1162
+ }
1163
+ case 'ClassProperty': {
1164
+ if (node.optional === false) {
1165
+ // $FlowExpectedError[cannot-write]
1166
+ delete node.optional;
1167
+ }
1168
+ if (node.declare === false) {
1169
+ // $FlowExpectedError[cannot-write]
1170
+ delete node.declare;
1171
+ }
1172
+ if (node.typeAnnotation == null) {
1173
+ // $FlowExpectedError[cannot-write]
1174
+ delete node.typeAnnotation;
1175
+ }
1176
+ return node;
1177
+ }
1178
+ case 'ClassPrivateProperty': {
1179
+ if (node.typeAnnotation == null) {
1180
+ // $FlowExpectedError[cannot-write]
1181
+ delete node.typeAnnotation;
1182
+ }
1183
+ return node;
1184
+ }
1185
+ case 'ExportNamedDeclaration': {
1186
+ if (node.declaration == null) {
1187
+ // $FlowExpectedError[cannot-write]
1188
+ delete node.declaration;
1189
+ }
1190
+ return node;
1191
+ }
1192
+ case 'ImportDeclaration': {
1193
+ if (node.assertions == null || node.assertions.length === 0) {
1194
+ // $FlowExpectedError[cannot-write]
1195
+ delete node.assertions;
1196
+ }
1197
+ return node;
1198
+ }
1199
+ case 'ArrayExpression': {
1200
+ // $FlowExpectedError[cannot-write]
1201
+ delete node.trailingComma;
1202
+ return node;
1203
+ }
1204
+ case 'JSXOpeningElement': {
1205
+ if (node.typeArguments == null) {
1206
+ // $FlowExpectedError[cannot-write]
1207
+ delete node.typeArguments;
1208
+ }
1209
+ return node;
1210
+ }
1211
+ default: {
1212
+ return node;
1213
+ }
1214
+ }
1215
+ }
1216
+
1217
+ export function transformProgram(
1218
+ program: Program,
1219
+ options: ParserOptions,
1220
+ ): BabelFile {
1221
+ const resultNode = SimpleTransform.transform(program, {
1222
+ transform(node) {
1223
+ // $FlowExpectedError[incompatible-call] We override the type to support the additional Babel types
1224
+ return transformNode(node);
1225
+ },
1226
+ visitorKeys: FlowESTreeAndBabelVisitorKeys,
1227
+ });
1228
+
1229
+ // $FlowExpectedError[incompatible-call] We override the type to support the additional Babel types
1230
+ SimpleTraverser.traverse(resultNode, {
1231
+ enter(node) {
1232
+ fixSourceLocation(node, options);
1233
+ },
1234
+ leave() {},
1235
+ visitorKeys: FlowESTreeAndBabelVisitorKeys,
1236
+ });
1237
+
1238
+ if (resultNode?.type === 'File') {
1239
+ return resultNode;
1240
+ }
1241
+ throw new Error(
1242
+ `Unknown AST node of type "${
1243
+ resultNode?.type ?? 'NULL'
1244
+ }" returned from Babel conversion`,
1245
+ );
1246
+ }