hermes-estree 0.32.1 → 0.33.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 (87) hide show
  1. package/dist/generated/HermesESTreeSelectorTypes.js.flow +262 -27
  2. package/dist/generated/predicates.js +120 -0
  3. package/dist/generated/predicates.js.flow +72 -0
  4. package/dist/predicates.js +2 -4
  5. package/dist/predicates.js.flow +5 -4
  6. package/dist/src/generated/predicates.js +120 -0
  7. package/dist/src/predicates.js +2 -4
  8. package/dist/types.js.flow +103 -11
  9. package/package.json +1 -1
  10. package/dist/HermesAST.js.flow +0 -57
  11. package/dist/HermesASTAdapter.js +0 -192
  12. package/dist/HermesASTAdapter.js.flow +0 -189
  13. package/dist/HermesParser.js +0 -108
  14. package/dist/HermesParser.js.flow +0 -161
  15. package/dist/HermesParserDecodeUTF8String.js +0 -68
  16. package/dist/HermesParserDecodeUTF8String.js.flow +0 -65
  17. package/dist/HermesParserDeserializer.js +0 -242
  18. package/dist/HermesParserDeserializer.js.flow +0 -260
  19. package/dist/HermesParserNodeDeserializers.js +0 -2477
  20. package/dist/HermesParserNodeDeserializers.js.flow +0 -16
  21. package/dist/HermesParserWASM.js +0 -6
  22. package/dist/HermesParserWASM.js.flow +0 -87
  23. package/dist/HermesToESTreeAdapter.js +0 -439
  24. package/dist/HermesToESTreeAdapter.js.flow +0 -421
  25. package/dist/ParserOptions.js +0 -18
  26. package/dist/ParserOptions.js.flow +0 -41
  27. package/dist/babel/TransformESTreeToBabel.js +0 -1123
  28. package/dist/babel/TransformESTreeToBabel.js.flow +0 -1277
  29. package/dist/estree/StripFlowTypes.js +0 -175
  30. package/dist/estree/StripFlowTypes.js.flow +0 -158
  31. package/dist/estree/StripFlowTypesForBabel.js +0 -215
  32. package/dist/estree/StripFlowTypesForBabel.js.flow +0 -216
  33. package/dist/estree/TransformComponentSyntax.js +0 -788
  34. package/dist/estree/TransformComponentSyntax.js.flow +0 -864
  35. package/dist/estree/TransformEnumSyntax.js +0 -106
  36. package/dist/estree/TransformEnumSyntax.js.flow +0 -125
  37. package/dist/estree/TransformMatchSyntax.js +0 -1006
  38. package/dist/estree/TransformMatchSyntax.js.flow +0 -912
  39. package/dist/generated/ESTreeVisitorKeys.js +0 -220
  40. package/dist/generated/ESTreeVisitorKeys.js.flow +0 -15
  41. package/dist/generated/ParserVisitorKeys.js +0 -794
  42. package/dist/generated/ParserVisitorKeys.js.flow +0 -17
  43. package/dist/getModuleDocblock.js +0 -112
  44. package/dist/getModuleDocblock.js.flow +0 -118
  45. package/dist/src/HermesASTAdapter.js +0 -192
  46. package/dist/src/HermesParser.js +0 -108
  47. package/dist/src/HermesParserDecodeUTF8String.js +0 -68
  48. package/dist/src/HermesParserDeserializer.js +0 -242
  49. package/dist/src/HermesParserNodeDeserializers.js +0 -2477
  50. package/dist/src/HermesToESTreeAdapter.js +0 -439
  51. package/dist/src/ParserOptions.js +0 -18
  52. package/dist/src/babel/TransformESTreeToBabel.js +0 -1123
  53. package/dist/src/estree/StripFlowTypes.js +0 -175
  54. package/dist/src/estree/StripFlowTypesForBabel.js +0 -215
  55. package/dist/src/estree/TransformComponentSyntax.js +0 -788
  56. package/dist/src/estree/TransformEnumSyntax.js +0 -106
  57. package/dist/src/estree/TransformMatchSyntax.js +0 -1006
  58. package/dist/src/generated/ESTreeVisitorKeys.js +0 -220
  59. package/dist/src/generated/ParserVisitorKeys.js +0 -794
  60. package/dist/src/getModuleDocblock.js +0 -112
  61. package/dist/src/transform/SimpleTransform.js +0 -136
  62. package/dist/src/transform/astArrayMutationHelpers.js +0 -62
  63. package/dist/src/transform/astNodeMutationHelpers.js +0 -200
  64. package/dist/src/traverse/SimpleTraverser.js +0 -137
  65. package/dist/src/traverse/getVisitorKeys.js +0 -37
  66. package/dist/src/utils/Builders.js +0 -191
  67. package/dist/src/utils/GenID.js +0 -41
  68. package/dist/src/utils/createSyntaxError.js +0 -25
  69. package/dist/src/utils/mutateESTreeASTForPrettier.js +0 -127
  70. package/dist/transform/SimpleTransform.js +0 -136
  71. package/dist/transform/SimpleTransform.js.flow +0 -169
  72. package/dist/transform/astArrayMutationHelpers.js +0 -62
  73. package/dist/transform/astArrayMutationHelpers.js.flow +0 -71
  74. package/dist/transform/astNodeMutationHelpers.js +0 -200
  75. package/dist/transform/astNodeMutationHelpers.js.flow +0 -246
  76. package/dist/traverse/SimpleTraverser.js +0 -137
  77. package/dist/traverse/SimpleTraverser.js.flow +0 -133
  78. package/dist/traverse/getVisitorKeys.js +0 -37
  79. package/dist/traverse/getVisitorKeys.js.flow +0 -36
  80. package/dist/utils/Builders.js +0 -191
  81. package/dist/utils/Builders.js.flow +0 -218
  82. package/dist/utils/GenID.js +0 -41
  83. package/dist/utils/GenID.js.flow +0 -38
  84. package/dist/utils/createSyntaxError.js +0 -25
  85. package/dist/utils/createSyntaxError.js.flow +0 -24
  86. package/dist/utils/mutateESTreeASTForPrettier.js +0 -127
  87. package/dist/utils/mutateESTreeASTForPrettier.js.flow +0 -130
@@ -1,1123 +0,0 @@
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-type] 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[incompatible-type] 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-type]
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[incompatible-type] optional and typeArguments are missing to match existing output.
402
- return {
403
- type: 'CallExpression',
404
- // $FlowExpectedError[incompatible-type] 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
- // $FlowFixMe[invalid-compare]
484
-
485
- if (node.argument.type !== 'GenericTypeAnnotation') {
486
- return nodeWith(node, {
487
- // $FlowExpectedError[incompatible-type] Special override for Babel
488
- argument: {
489
- type: 'GenericTypeAnnotation',
490
- id: node.argument,
491
- typeParameters: null,
492
- loc: node.argument.loc,
493
- range: node.argument.range,
494
- parent: EMPTY_PARENT
495
- }
496
- });
497
- }
498
-
499
- return node;
500
- }
501
-
502
- function mapDeclareVariable(node) {
503
- if (node.kind != null) {
504
- // $FlowExpectedError[cannot-write]
505
- delete node.kind;
506
- }
507
-
508
- return node;
509
- }
510
-
511
- function mapJSXElement(node) {
512
- if (node.openingElement.typeArguments != null) {
513
- // $FlowExpectedError[cannot-write]
514
- delete node.openingElement.typeArguments;
515
- }
516
-
517
- return node;
518
- }
519
-
520
- function mapChainExpressionInnerNode(node) {
521
- switch (node.type) {
522
- case 'MemberExpression':
523
- {
524
- const innerObject = mapChainExpressionInnerNode(node.object);
525
-
526
- if (!node.optional && innerObject.type !== 'OptionalMemberExpression' && innerObject.type !== 'OptionalCallExpression') {
527
- return node;
528
- }
529
-
530
- return {
531
- type: 'OptionalMemberExpression',
532
- object: innerObject,
533
- property: node.property,
534
- computed: node.computed,
535
- optional: node.optional,
536
- loc: node.loc,
537
- range: node.range,
538
- parent: node.parent
539
- };
540
- }
541
-
542
- case 'CallExpression':
543
- {
544
- const innerCallee = mapChainExpressionInnerNode(node.callee);
545
-
546
- if (!node.optional && innerCallee.type !== 'OptionalMemberExpression' && innerCallee.type !== 'OptionalCallExpression') {
547
- return node;
548
- }
549
-
550
- return {
551
- type: 'OptionalCallExpression',
552
- callee: innerCallee,
553
- optional: node.optional,
554
- arguments: node.arguments,
555
- typeArguments: node.typeArguments,
556
- loc: node.loc,
557
- range: node.range,
558
- parent: node.parent
559
- };
560
- }
561
-
562
- default:
563
- {
564
- return node;
565
- }
566
- }
567
- }
568
-
569
- function mapChainExpression(node) {
570
- return mapChainExpressionInnerNode(node.expression);
571
- }
572
-
573
- function mapLiteral(node) {
574
- const base = {
575
- loc: node.loc,
576
- range: node.range,
577
- parent: node.parent
578
- };
579
-
580
- switch (node.literalType) {
581
- case 'string':
582
- {
583
- return {
584
- type: 'StringLiteral',
585
- value: node.value,
586
- extra: {
587
- rawValue: node.value,
588
- raw: node.raw
589
- },
590
- ...base
591
- };
592
- }
593
-
594
- case 'numeric':
595
- {
596
- return {
597
- type: 'NumericLiteral',
598
- value: node.value,
599
- extra: {
600
- rawValue: node.value,
601
- raw: node.raw
602
- },
603
- ...base
604
- };
605
- }
606
-
607
- case 'bigint':
608
- {
609
- return {
610
- type: 'BigIntLiteral',
611
- value: node.bigint,
612
- extra: {
613
- rawValue: node.bigint,
614
- raw: node.raw
615
- },
616
- ...base
617
- };
618
- }
619
-
620
- case 'boolean':
621
- {
622
- return {
623
- type: 'BooleanLiteral',
624
- value: node.value,
625
- ...base
626
- };
627
- }
628
-
629
- case 'null':
630
- {
631
- return {
632
- type: 'NullLiteral',
633
- ...base
634
- };
635
- }
636
-
637
- case 'regexp':
638
- {
639
- return {
640
- type: 'RegExpLiteral',
641
- extra: {
642
- raw: node.raw
643
- },
644
- pattern: node.regex.pattern,
645
- flags: node.regex.flags,
646
- ...base
647
- };
648
- }
649
- }
650
- }
651
-
652
- function transformNode(node) {
653
- switch (node.type) {
654
- case 'Program':
655
- {
656
- var _node$parent;
657
-
658
- // Check if we have already processed this node.
659
- // $FlowFixMe[incompatible-type]
660
- // $FlowFixMe[invalid-compare]
661
- if (((_node$parent = node.parent) == null ? void 0 : _node$parent.type) === 'File') {
662
- return node;
663
- }
664
-
665
- return mapProgram(node);
666
- }
667
-
668
- case 'BlockStatement':
669
- {
670
- return mapNodeWithDirectives(node);
671
- }
672
-
673
- case 'Property':
674
- {
675
- return mapProperty(node);
676
- }
677
-
678
- case 'TemplateElement':
679
- {
680
- return mapTemplateElement(node);
681
- }
682
-
683
- case 'MethodDefinition':
684
- {
685
- return mapMethodDefinition(node);
686
- }
687
-
688
- case 'ExportAllDeclaration':
689
- {
690
- return mapExportAllDeclaration(node);
691
- }
692
-
693
- case 'RestElement':
694
- {
695
- return mapRestElement(node);
696
- }
697
-
698
- case 'ImportExpression':
699
- {
700
- return mapImportExpression(node);
701
- }
702
-
703
- case 'PrivateIdentifier':
704
- {
705
- return mapPrivateIdentifier(node);
706
- }
707
-
708
- case 'PropertyDefinition':
709
- {
710
- return mapPropertyDefinition(node);
711
- }
712
-
713
- case 'TypeofTypeAnnotation':
714
- {
715
- return mapTypeofTypeAnnotation(node);
716
- }
717
-
718
- case 'DeclareVariable':
719
- {
720
- return mapDeclareVariable(node);
721
- }
722
-
723
- case 'JSXElement':
724
- {
725
- return mapJSXElement(node);
726
- }
727
-
728
- case 'Literal':
729
- {
730
- return mapLiteral(node);
731
- }
732
-
733
- case 'ChainExpression':
734
- {
735
- return mapChainExpression(node);
736
- }
737
-
738
- case 'TypeCastExpression':
739
- {
740
- // Babel uses different positions for TypeCastExpression locations.
741
- // $FlowExpectedError[cannot-write]
742
- node.loc.start.column = node.loc.start.column + 1; // $FlowExpectedError[cannot-write]
743
-
744
- node.loc.end.column = node.loc.end.column - 1; // $FlowExpectedError[cannot-write]
745
-
746
- node.range = [node.range[0] + 1, node.range[1] - 1];
747
- return node;
748
- }
749
-
750
- case 'AsExpression':
751
- {
752
- const {
753
- typeAnnotation
754
- } = node; // $FlowExpectedError[cannot-write]
755
-
756
- node.type = 'TypeCastExpression'; // $FlowExpectedError[cannot-write]
757
-
758
- node.typeAnnotation = {
759
- type: 'TypeAnnotation',
760
- typeAnnotation,
761
- loc: typeAnnotation.loc,
762
- range: typeAnnotation.range
763
- };
764
- return node;
765
- }
766
-
767
- case 'AsConstExpression':
768
- {
769
- return node.expression;
770
- }
771
-
772
- /**
773
- * Babel has a different format for Literals
774
- */
775
-
776
- case 'NumberLiteralTypeAnnotation':
777
- {
778
- // $FlowExpectedError[prop-missing]
779
- node.extra = {
780
- rawValue: node.value,
781
- raw: node.raw
782
- }; // $FlowExpectedError[cannot-write]
783
-
784
- delete node.raw;
785
- return node;
786
- }
787
-
788
- case 'StringLiteralTypeAnnotation':
789
- {
790
- // $FlowExpectedError[prop-missing]
791
- node.extra = {
792
- rawValue: node.value,
793
- raw: node.raw
794
- }; // $FlowExpectedError[cannot-write]
795
-
796
- delete node.raw;
797
- return node;
798
- }
799
-
800
- case 'BigIntLiteralTypeAnnotation':
801
- {
802
- // $FlowExpectedError[prop-missing]
803
- node.extra = {
804
- rawValue: node.bigint,
805
- raw: node.raw
806
- }; // $FlowExpectedError[cannot-write]
807
-
808
- delete node.bigint; // $FlowExpectedError[cannot-write]
809
-
810
- delete node.raw;
811
- return node;
812
- }
813
-
814
- case 'BooleanLiteralTypeAnnotation':
815
- {
816
- // $FlowExpectedError[cannot-write]
817
- delete node.raw;
818
- return node;
819
- }
820
-
821
- case 'TupleTypeAnnotation':
822
- {
823
- // $FlowExpectedError[cannot-write]
824
- delete node.inexact;
825
- return node;
826
- }
827
-
828
- case 'JSXText':
829
- {
830
- // $FlowExpectedError[prop-missing]
831
- node.extra = {
832
- rawValue: node.value,
833
- raw: node.raw
834
- }; // $FlowExpectedError[cannot-write]
835
-
836
- delete node.raw;
837
- return node;
838
- }
839
-
840
- /**
841
- * Babel condenses there output AST by removing some "falsy" values.
842
- */
843
-
844
- case 'Identifier':
845
- {
846
- // Babel only has these values if they are "set"
847
- if (node.optional === false || node.optional == null) {
848
- // $FlowExpectedError[cannot-write]
849
- delete node.optional;
850
- }
851
-
852
- if (node.typeAnnotation == null) {
853
- // $FlowExpectedError[cannot-write]
854
- delete node.typeAnnotation;
855
- }
856
-
857
- return node;
858
- }
859
-
860
- case 'CallExpression':
861
- {
862
- if (node.optional === false) {
863
- // $FlowExpectedError[cannot-write]
864
- delete node.optional;
865
- }
866
-
867
- if (node.typeArguments == null) {
868
- // $FlowExpectedError[cannot-write]
869
- delete node.typeArguments;
870
- }
871
-
872
- return node;
873
- }
874
-
875
- case 'OptionalCallExpression':
876
- {
877
- if (node.typeArguments == null) {
878
- // $FlowExpectedError[cannot-write]
879
- delete node.typeArguments;
880
- }
881
-
882
- return node;
883
- }
884
-
885
- case 'MemberExpression':
886
- {
887
- // $FlowExpectedError[cannot-write]
888
- delete node.optional;
889
- return node;
890
- }
891
-
892
- case 'ExpressionStatement':
893
- {
894
- // $FlowExpectedError[cannot-write]
895
- delete node.directive;
896
- return node;
897
- }
898
-
899
- case 'ObjectPattern':
900
- case 'ArrayPattern':
901
- {
902
- if (node.typeAnnotation == null) {
903
- // $FlowExpectedError[cannot-write]
904
- delete node.typeAnnotation;
905
- }
906
-
907
- return node;
908
- }
909
-
910
- case 'FunctionDeclaration':
911
- case 'FunctionExpression':
912
- case 'ArrowFunctionExpression':
913
- case 'ClassMethod':
914
- {
915
- // $FlowExpectedError[prop-missing]
916
- // $FlowExpectedError[cannot-write]
917
- delete node.expression;
918
-
919
- if (node.predicate == null) {
920
- // $FlowExpectedError[cannot-write]
921
- delete node.predicate;
922
- }
923
-
924
- if (node.returnType == null) {
925
- // $FlowExpectedError[cannot-write]
926
- delete node.returnType;
927
- }
928
-
929
- if (node.typeParameters == null) {
930
- // $FlowExpectedError[cannot-write]
931
- delete node.typeParameters;
932
- }
933
-
934
- return node;
935
- }
936
-
937
- case 'ObjectMethod':
938
- {
939
- if (node.returnType == null) {
940
- // $FlowExpectedError[cannot-write]
941
- delete node.returnType;
942
- }
943
-
944
- if (node.typeParameters == null) {
945
- // $FlowExpectedError[cannot-write]
946
- delete node.typeParameters;
947
- }
948
-
949
- return node;
950
- }
951
-
952
- case 'ClassPrivateMethod':
953
- {
954
- if (node.computed === false) {
955
- // $FlowExpectedError[cannot-write]
956
- delete node.computed;
957
- }
958
-
959
- if (node.predicate == null) {
960
- // $FlowExpectedError[cannot-write]
961
- delete node.predicate;
962
- }
963
-
964
- if (node.returnType == null) {
965
- // $FlowExpectedError[cannot-write]
966
- delete node.returnType;
967
- }
968
-
969
- if (node.typeParameters == null) {
970
- // $FlowExpectedError[cannot-write]
971
- delete node.typeParameters;
972
- }
973
-
974
- return node;
975
- }
976
-
977
- case 'ClassExpression':
978
- case 'ClassDeclaration':
979
- {
980
- if (node.decorators == null || node.decorators.length === 0) {
981
- // $FlowExpectedError[cannot-write]
982
- delete node.decorators;
983
- }
984
-
985
- if (node.implements == null || node.implements.length === 0) {
986
- // $FlowExpectedError[cannot-write]
987
- delete node.implements;
988
- }
989
-
990
- if (node.superTypeParameters == null) {
991
- // $FlowExpectedError[cannot-write]
992
- delete node.superTypeParameters;
993
- }
994
-
995
- if (node.typeParameters == null) {
996
- // $FlowExpectedError[cannot-write]
997
- delete node.typeParameters;
998
- }
999
-
1000
- return node;
1001
- }
1002
-
1003
- case 'ClassProperty':
1004
- {
1005
- if (node.optional === false) {
1006
- // $FlowExpectedError[cannot-write]
1007
- delete node.optional;
1008
- }
1009
-
1010
- if (node.declare === false) {
1011
- // $FlowExpectedError[cannot-write]
1012
- delete node.declare;
1013
- }
1014
-
1015
- if (node.typeAnnotation == null) {
1016
- // $FlowExpectedError[cannot-write]
1017
- delete node.typeAnnotation;
1018
- }
1019
-
1020
- return node;
1021
- }
1022
-
1023
- case 'ClassPrivateProperty':
1024
- {
1025
- if (node.typeAnnotation == null) {
1026
- // $FlowExpectedError[cannot-write]
1027
- delete node.typeAnnotation;
1028
- }
1029
-
1030
- return node;
1031
- }
1032
-
1033
- case 'ExportNamedDeclaration':
1034
- {
1035
- if (node.declaration == null) {
1036
- // $FlowExpectedError[cannot-write]
1037
- delete node.declaration;
1038
- }
1039
-
1040
- return node;
1041
- }
1042
-
1043
- case 'ImportDeclaration':
1044
- {
1045
- if (node.assertions == null || node.assertions.length === 0) {
1046
- // $FlowExpectedError[cannot-write]
1047
- delete node.assertions;
1048
- }
1049
-
1050
- return node;
1051
- }
1052
-
1053
- case 'ArrayExpression':
1054
- {
1055
- // $FlowExpectedError[cannot-write]
1056
- delete node.trailingComma;
1057
- return node;
1058
- }
1059
-
1060
- case 'JSXOpeningElement':
1061
- {
1062
- if (node.typeArguments == null) {
1063
- // $FlowExpectedError[cannot-write]
1064
- delete node.typeArguments;
1065
- }
1066
-
1067
- return node;
1068
- }
1069
-
1070
- case 'DeclareOpaqueType':
1071
- case 'OpaqueType':
1072
- {
1073
- if (node.lowerBound != null) {
1074
- // $FlowExpectedError[cannot-write]
1075
- delete node.lowerBound;
1076
- }
1077
-
1078
- if (node.upperBound != null) {
1079
- // $FlowExpectedError[cannot-write]
1080
- delete node.upperBound;
1081
- }
1082
-
1083
- return node;
1084
- }
1085
-
1086
- default:
1087
- {
1088
- return node;
1089
- }
1090
- }
1091
- }
1092
-
1093
- function transformProgram(program, options) {
1094
- var _resultNode$type;
1095
-
1096
- const resultNode = _SimpleTransform.SimpleTransform.transform(program, {
1097
- transform(node) {
1098
- // $FlowExpectedError[incompatible-type] We override the type to support the additional Babel types
1099
- return transformNode(node);
1100
- },
1101
-
1102
- visitorKeys: FlowESTreeAndBabelVisitorKeys
1103
- }); // $FlowExpectedError[incompatible-type] We override the type to support the additional Babel types
1104
-
1105
-
1106
- _SimpleTraverser.SimpleTraverser.traverse(resultNode, {
1107
- enter(node) {
1108
- fixSourceLocation(node, options);
1109
- },
1110
-
1111
- leave() {},
1112
-
1113
- visitorKeys: FlowESTreeAndBabelVisitorKeys
1114
- }); // $FlowFixMe[incompatible-type]
1115
- // $FlowFixMe[invalid-compare]
1116
-
1117
-
1118
- if ((resultNode == null ? void 0 : resultNode.type) === 'File') {
1119
- return resultNode;
1120
- }
1121
-
1122
- throw new Error(`Unknown AST node of type "${(_resultNode$type = resultNode == null ? void 0 : resultNode.type) != null ? _resultNode$type : 'NULL'}" returned from Babel conversion`);
1123
- }