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