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