hermes-parser 0.28.0 → 0.29.0

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