hermes-parser 0.5.0 → 0.8.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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -21,6 +21,8 @@ import type {HermesNode} from './HermesAST';
21
21
 
22
22
  import HermesASTAdapter from './HermesASTAdapter';
23
23
 
24
+ declare var BigInt: ?(value: $FlowFixMe) => mixed;
25
+
24
26
  export default class HermesToBabelAdapter extends HermesASTAdapter {
25
27
  fixSourceLocation(node: HermesNode): void {
26
28
  const loc = node.loc;
@@ -67,13 +69,18 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
67
69
  return this.mapExportDefaultDeclaration(node);
68
70
  case 'ExportNamedDeclaration':
69
71
  return this.mapExportNamedDeclaration(node);
72
+ case 'ExportNamespaceSpecifier':
73
+ return this.mapExportNamespaceSpecifier(node);
70
74
  case 'ExportAllDeclaration':
71
75
  return this.mapExportAllDeclaration(node);
72
76
  case 'RestElement':
73
77
  return this.mapRestElement(node);
74
78
  case 'ImportExpression':
75
79
  return this.mapImportExpression(node);
80
+ case 'JSXStringLiteral':
81
+ return this.mapJSXStringLiteral(node);
76
82
  case 'PrivateName':
83
+ return this.mapPrivateName(node);
77
84
  case 'ClassPrivateProperty':
78
85
  return this.mapPrivateProperty(node);
79
86
  case 'FunctionDeclaration':
@@ -82,6 +89,8 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
82
89
  case 'IndexedAccessType':
83
90
  case 'OptionalIndexedAccessType':
84
91
  return this.mapUnsupportedTypeAnnotation(node);
92
+ case 'BigIntLiteral':
93
+ return this.mapBigIntLiteral(node);
85
94
  default:
86
95
  return this.mapNodeDefault(node);
87
96
  }
@@ -198,7 +207,7 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
198
207
  mapGenericTypeAnnotation(node: HermesNode): HermesNode {
199
208
  // Convert simple `this` generic type to ThisTypeAnnotation
200
209
  if (
201
- node.typeParameters === null &&
210
+ node.typeParameters == null &&
202
211
  node.id.type === 'Identifier' &&
203
212
  node.id.name === 'this'
204
213
  ) {
@@ -249,13 +258,14 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
249
258
  predicate,
250
259
  } = value;
251
260
 
252
- return {
261
+ const newNode: HermesNode = {
253
262
  type: 'ObjectMethod',
254
263
  loc: node.loc,
255
264
  start: node.start,
256
265
  end: node.end,
257
266
  // Non getter or setter methods have `kind = method`
258
267
  kind: node.kind === 'init' ? 'method' : node.kind,
268
+ method: node.kind === 'init' ? true : false,
259
269
  computed: node.computed,
260
270
  key,
261
271
  id,
@@ -267,6 +277,11 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
267
277
  typeParameters,
268
278
  predicate,
269
279
  };
280
+ if (node.kind !== 'init') {
281
+ // babel emits an empty variance property on accessors for some reason
282
+ newNode.variance = null;
283
+ }
284
+ return newNode;
270
285
  } else {
271
286
  // Non-method property nodes should be renamed to ObjectProperty
272
287
  node.type = 'ObjectProperty';
@@ -320,6 +335,18 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
320
335
  if (annotation != null) {
321
336
  restElement.typeAnnotation = annotation;
322
337
  restElement.argument.typeAnnotation = null;
338
+ // Unfortunately there's no way for us to recover the end location of
339
+ // the argument for the general case
340
+ if (restElement.argument.type === 'Identifier') {
341
+ restElement.argument.end =
342
+ restElement.argument.start + restElement.argument.name.length;
343
+ restElement.argument.loc.end = {
344
+ ...restElement.argument.loc.start,
345
+ column:
346
+ restElement.argument.loc.start.column +
347
+ restElement.argument.name.length,
348
+ };
349
+ }
323
350
  }
324
351
 
325
352
  return restElement;
@@ -335,14 +362,33 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
335
362
  end: node.end,
336
363
  callee: {
337
364
  type: 'Import',
338
- loc: node.loc,
365
+ loc: {
366
+ ...node.loc,
367
+ end: {
368
+ ...node.loc.start,
369
+ column: node.loc.start.column + 'import'.length,
370
+ },
371
+ },
339
372
  start: node.start,
340
- end: node.end,
373
+ end: node.start + 'import'.length,
341
374
  },
342
375
  arguments: [this.mapNode(node.source)],
343
376
  };
344
377
  }
345
378
 
379
+ mapJSXStringLiteral(node: HermesNode): HermesNode {
380
+ // Babel expects StringLiterals in JSX,
381
+ // but Hermes uses JSXStringLiteral to attach the raw value without
382
+ // having to internally attach it to every single string literal.
383
+ return {
384
+ type: 'StringLiteral',
385
+ loc: node.loc,
386
+ start: node.start,
387
+ end: node.end,
388
+ value: node.value,
389
+ };
390
+ }
391
+
346
392
  mapFunction(node: HermesNode): HermesNode {
347
393
  // Remove the first parameter if it is a this-type annotation,
348
394
  // which is not recognized by Babel.
@@ -365,4 +411,62 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
365
411
  end: node.end,
366
412
  };
367
413
  }
414
+
415
+ mapBigIntLiteral(node: HermesNode): HermesNode {
416
+ const bigint = node.bigint.replace(/n$/, '').replace(/_/, '');
417
+ node.value = typeof BigInt === 'function' ? BigInt(bigint) : null;
418
+ return node;
419
+ }
420
+
421
+ mapPrivateProperty(nodeUnprocessed: HermesNode): HermesNode {
422
+ const node = this.mapNodeDefault(nodeUnprocessed);
423
+ node.key = {
424
+ type: 'PrivateName',
425
+ id: {
426
+ ...node.key,
427
+ // babel doesn't include the hash in the identifier
428
+ start: node.key.start + 1,
429
+ loc: {
430
+ ...node.key.loc,
431
+ start: {
432
+ ...node.key.loc.start,
433
+ column: node.key.loc.start.column + 1,
434
+ },
435
+ },
436
+ },
437
+ start: node.key.start,
438
+ end: node.key.end,
439
+ loc: node.key.loc,
440
+ };
441
+
442
+ return node;
443
+ }
444
+
445
+ mapPrivateName(node: HermesNode): HermesNode {
446
+ // babel doesn't include the hash in the identifier
447
+ node.id.start += 1;
448
+ node.id.loc.start.column += 1;
449
+ return node;
450
+ }
451
+
452
+ mapExportNamespaceSpecifier(nodeUnprocessed: HermesNode): HermesNode {
453
+ const node = this.mapNodeDefault(nodeUnprocessed);
454
+
455
+ // the hermes AST emits the location as the location of the entire export
456
+ // but babel emits the location as *just* the "* as id" bit
457
+
458
+ // the end will always align with the end of the identifier (ezpz)
459
+ // but the start will align with the "*" token - which we can't recover from just the AST
460
+ // so we just fudge the start location a bit to get it "good enough"
461
+ // it will be wrong if the AST is anything like "export * as x from 'y'"... but oh well
462
+ node.start = node.start + 'export '.length;
463
+ node.loc.start.column = node.loc.start.column + 'export '.length;
464
+ node.end = node.exported.end;
465
+ node.loc.end = {
466
+ column: node.exported.loc.end.column,
467
+ line: node.exported.loc.end.line,
468
+ };
469
+
470
+ return node;
471
+ }
368
472
  }