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