hermes-parser 0.18.2 → 0.19.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.
@@ -478,6 +478,9 @@ function mapPropertyDefinition(node) {
478
478
  }
479
479
 
480
480
  function mapTypeofTypeAnnotation(node) {
481
+ // $FlowExpectedError[cannot-write]
482
+ delete node.typeArguments;
483
+
481
484
  if (node.argument.type !== 'GenericTypeAnnotation') {
482
485
  return nodeWith(node, {
483
486
  // $FlowExpectedError[incompatible-call] Special override for Babel
@@ -748,6 +748,8 @@ function mapPropertyDefinition(
748
748
  function mapTypeofTypeAnnotation(
749
749
  node: TypeofTypeAnnotation,
750
750
  ): TypeofTypeAnnotation {
751
+ // $FlowExpectedError[cannot-write]
752
+ delete node.typeArguments;
751
753
  if (node.argument.type !== 'GenericTypeAnnotation') {
752
754
  return nodeWith(node, {
753
755
  // $FlowExpectedError[incompatible-call] Special override for Babel
@@ -503,6 +503,60 @@ function mapComponentDeclaration(node) {
503
503
  forwardRefDetails
504
504
  };
505
505
  }
506
+
507
+ function mapDeclareHook(node) {
508
+ return {
509
+ type: 'DeclareFunction',
510
+ id: {
511
+ type: 'Identifier',
512
+ name: node.id.name,
513
+ optional: node.id.optional,
514
+ typeAnnotation: {
515
+ type: 'TypeAnnotation',
516
+ typeAnnotation: {
517
+ type: 'FunctionTypeAnnotation',
518
+ this: null,
519
+ params: node.id.typeAnnotation.typeAnnotation.params,
520
+ typeParameters: node.id.typeAnnotation.typeAnnotation.typeParameters,
521
+ rest: node.id.typeAnnotation.typeAnnotation.rest,
522
+ returnType: node.id.typeAnnotation.typeAnnotation.returnType,
523
+ loc: node.id.typeAnnotation.typeAnnotation.loc,
524
+ range: node.id.typeAnnotation.typeAnnotation.range,
525
+ parent: node.id.typeAnnotation.typeAnnotation.parent
526
+ },
527
+ loc: node.id.typeAnnotation.loc,
528
+ range: node.id.typeAnnotation.range,
529
+ parent: node.id.typeAnnotation.parent
530
+ },
531
+ loc: node.id.loc,
532
+ range: node.id.range,
533
+ parent: node.id.parent
534
+ },
535
+ loc: node.loc,
536
+ range: node.range,
537
+ parent: node.parent,
538
+ predicate: null
539
+ };
540
+ }
541
+
542
+ function mapHookDeclaration(node) {
543
+ const comp = {
544
+ type: 'FunctionDeclaration',
545
+ id: node.id && (0, _astNodeMutationHelpers.shallowCloneNode)(node.id),
546
+ __hookDeclaration: true,
547
+ typeParameters: node.typeParameters,
548
+ params: node.params,
549
+ returnType: node.returnType,
550
+ body: node.body,
551
+ async: false,
552
+ generator: false,
553
+ predicate: null,
554
+ loc: node.loc,
555
+ range: node.range,
556
+ parent: node.parent
557
+ };
558
+ return comp;
559
+ }
506
560
  /**
507
561
  * Scan a list of statements and return the position of the
508
562
  * first statement that contains a reference to a given component
@@ -580,9 +634,16 @@ function mapStatementList(stmts) {
580
634
  break;
581
635
  }
582
636
 
637
+ case 'HookDeclaration':
638
+ {
639
+ const decl = mapHookDeclaration(node);
640
+ newBody.push(decl);
641
+ break;
642
+ }
643
+
583
644
  case 'ExportNamedDeclaration':
584
645
  {
585
- var _node$declaration;
646
+ var _node$declaration, _node$declaration2;
586
647
 
587
648
  if (((_node$declaration = node.declaration) == null ? void 0 : _node$declaration.type) === 'ComponentDeclaration') {
588
649
  mapComponentDeclarationIntoList(node.declaration, newBody, componentOrRef => {
@@ -621,21 +682,33 @@ function mapStatementList(stmts) {
621
682
  break;
622
683
  }
623
684
 
685
+ if (((_node$declaration2 = node.declaration) == null ? void 0 : _node$declaration2.type) === 'HookDeclaration') {
686
+ const comp = mapHookDeclaration(node.declaration);
687
+ newBody.push(comp);
688
+ break;
689
+ }
690
+
624
691
  newBody.push(node);
625
692
  break;
626
693
  }
627
694
 
628
695
  case 'ExportDefaultDeclaration':
629
696
  {
630
- var _node$declaration2;
697
+ var _node$declaration3, _node$declaration4;
631
698
 
632
- if (((_node$declaration2 = node.declaration) == null ? void 0 : _node$declaration2.type) === 'ComponentDeclaration') {
699
+ if (((_node$declaration3 = node.declaration) == null ? void 0 : _node$declaration3.type) === 'ComponentDeclaration') {
633
700
  mapComponentDeclarationIntoList(node.declaration, newBody, componentOrRef => nodeWith(node, {
634
701
  declaration: componentOrRef
635
702
  }));
636
703
  break;
637
704
  }
638
705
 
706
+ if (((_node$declaration4 = node.declaration) == null ? void 0 : _node$declaration4.type) === 'HookDeclaration') {
707
+ const comp = mapHookDeclaration(node.declaration);
708
+ newBody.push(comp);
709
+ break;
710
+ }
711
+
639
712
  newBody.push(node);
640
713
  break;
641
714
  }
@@ -659,6 +732,11 @@ function transformProgram(program, _options) {
659
732
  return mapDeclareComponent(node);
660
733
  }
661
734
 
735
+ case 'DeclareHook':
736
+ {
737
+ return mapDeclareHook(node);
738
+ }
739
+
662
740
  case 'Program':
663
741
  case 'BlockStatement':
664
742
  {
@@ -683,6 +761,13 @@ function transformProgram(program, _options) {
683
761
  throw (0, _createSyntaxError.createSyntaxError)(node, `Components must be defined at the top level of a module or within a ` + `BlockStatement, instead got parent of "${(_node$parent = node.parent) == null ? void 0 : _node$parent.type}".`);
684
762
  }
685
763
 
764
+ case 'HookDeclaration':
765
+ {
766
+ var _node$parent2;
767
+
768
+ throw (0, _createSyntaxError.createSyntaxError)(node, `Hooks must be defined at the top level of a module or within a ` + `BlockStatement, instead got parent of "${(_node$parent2 = node.parent) == null ? void 0 : _node$parent2.type}".`);
769
+ }
770
+
686
771
  default:
687
772
  {
688
773
  return node;
@@ -37,6 +37,9 @@ import type {
37
37
  DestructuringObjectProperty,
38
38
  VariableDeclaration,
39
39
  ModuleDeclaration,
40
+ DeclareHook,
41
+ DeclareFunction,
42
+ HookDeclaration,
40
43
  Statement,
41
44
  AssignmentPattern,
42
45
  BindingName,
@@ -589,6 +592,61 @@ function mapComponentDeclaration(node: ComponentDeclaration): {
589
592
  return {comp, forwardRefDetails};
590
593
  }
591
594
 
595
+ function mapDeclareHook(node: DeclareHook): DeclareFunction {
596
+ return {
597
+ type: 'DeclareFunction',
598
+ id: {
599
+ type: 'Identifier',
600
+ name: node.id.name,
601
+ optional: node.id.optional,
602
+ typeAnnotation: {
603
+ type: 'TypeAnnotation',
604
+ typeAnnotation: {
605
+ type: 'FunctionTypeAnnotation',
606
+ this: null,
607
+ params: node.id.typeAnnotation.typeAnnotation.params,
608
+ typeParameters: node.id.typeAnnotation.typeAnnotation.typeParameters,
609
+ rest: node.id.typeAnnotation.typeAnnotation.rest,
610
+ returnType: node.id.typeAnnotation.typeAnnotation.returnType,
611
+ loc: node.id.typeAnnotation.typeAnnotation.loc,
612
+ range: node.id.typeAnnotation.typeAnnotation.range,
613
+ parent: node.id.typeAnnotation.typeAnnotation.parent,
614
+ },
615
+ loc: node.id.typeAnnotation.loc,
616
+ range: node.id.typeAnnotation.range,
617
+ parent: node.id.typeAnnotation.parent,
618
+ },
619
+ loc: node.id.loc,
620
+ range: node.id.range,
621
+ parent: node.id.parent,
622
+ },
623
+ loc: node.loc,
624
+ range: node.range,
625
+ parent: node.parent,
626
+ predicate: null,
627
+ };
628
+ }
629
+
630
+ function mapHookDeclaration(node: HookDeclaration): FunctionDeclaration {
631
+ const comp = {
632
+ type: 'FunctionDeclaration',
633
+ id: node.id && shallowCloneNode(node.id),
634
+ __hookDeclaration: true,
635
+ typeParameters: node.typeParameters,
636
+ params: node.params,
637
+ returnType: node.returnType,
638
+ body: node.body,
639
+ async: false,
640
+ generator: false,
641
+ predicate: null,
642
+ loc: node.loc,
643
+ range: node.range,
644
+ parent: node.parent,
645
+ };
646
+
647
+ return comp;
648
+ }
649
+
592
650
  /**
593
651
  * Scan a list of statements and return the position of the
594
652
  * first statement that contains a reference to a given component
@@ -664,6 +722,11 @@ function mapStatementList(
664
722
  mapComponentDeclarationIntoList(node, newBody);
665
723
  break;
666
724
  }
725
+ case 'HookDeclaration': {
726
+ const decl = mapHookDeclaration(node);
727
+ newBody.push(decl);
728
+ break;
729
+ }
667
730
  case 'ExportNamedDeclaration': {
668
731
  if (node.declaration?.type === 'ComponentDeclaration') {
669
732
  mapComponentDeclarationIntoList(
@@ -703,6 +766,12 @@ function mapStatementList(
703
766
  break;
704
767
  }
705
768
 
769
+ if (node.declaration?.type === 'HookDeclaration') {
770
+ const comp = mapHookDeclaration(node.declaration);
771
+ newBody.push(comp);
772
+ break;
773
+ }
774
+
706
775
  newBody.push(node);
707
776
  break;
708
777
  }
@@ -716,6 +785,12 @@ function mapStatementList(
716
785
  break;
717
786
  }
718
787
 
788
+ if (node.declaration?.type === 'HookDeclaration') {
789
+ const comp = mapHookDeclaration(node.declaration);
790
+ newBody.push(comp);
791
+ break;
792
+ }
793
+
719
794
  newBody.push(node);
720
795
  break;
721
796
  }
@@ -738,6 +813,9 @@ export function transformProgram(
738
813
  case 'DeclareComponent': {
739
814
  return mapDeclareComponent(node);
740
815
  }
816
+ case 'DeclareHook': {
817
+ return mapDeclareHook(node);
818
+ }
741
819
  case 'Program':
742
820
  case 'BlockStatement': {
743
821
  return nodeWith(node, {body: mapStatementList(node.body)});
@@ -756,6 +834,13 @@ export function transformProgram(
756
834
  `BlockStatement, instead got parent of "${node.parent?.type}".`,
757
835
  );
758
836
  }
837
+ case 'HookDeclaration': {
838
+ throw createSyntaxError(
839
+ node,
840
+ `Hooks must be defined at the top level of a module or within a ` +
841
+ `BlockStatement, instead got parent of "${node.parent?.type}".`,
842
+ );
843
+ }
759
844
  default: {
760
845
  return node;
761
846
  }
@@ -152,6 +152,7 @@ function transformProgram(program, _options) {
152
152
  case 'TupleTypeLabeledElement':
153
153
  case 'TupleTypeSpreadElement':
154
154
  case 'ComponentTypeAnnotation':
155
+ case 'HookTypeAnnotation':
155
156
  case 'TypeOperator':
156
157
  case 'TypePredicate':
157
158
  {
@@ -165,6 +165,7 @@ export function transformProgram(
165
165
  case 'TupleTypeLabeledElement':
166
166
  case 'TupleTypeSpreadElement':
167
167
  case 'ComponentTypeAnnotation':
168
+ case 'HookTypeAnnotation':
168
169
  case 'TypeOperator':
169
170
  case 'TypePredicate': {
170
171
  // Babel does not support these generic types, so convert to any
@@ -60,6 +60,7 @@ module.exports = {
60
60
  DeclareExportAllDeclaration: ['source'],
61
61
  DeclareExportDeclaration: ['declaration', 'specifiers', 'source'],
62
62
  DeclareFunction: ['id', 'predicate'],
63
+ DeclareHook: ['id'],
63
64
  DeclareInterface: ['id', 'typeParameters', 'extends', 'body'],
64
65
  DeclareModule: ['id', 'body'],
65
66
  DeclareModuleExports: ['typeAnnotation'],
@@ -92,6 +93,8 @@ module.exports = {
92
93
  FunctionTypeAnnotation: ['params', 'this', 'returnType', 'rest', 'typeParameters'],
93
94
  FunctionTypeParam: ['name', 'typeAnnotation'],
94
95
  GenericTypeAnnotation: ['id', 'typeParameters'],
96
+ HookDeclaration: ['id', 'params', 'body', 'typeParameters', 'returnType'],
97
+ HookTypeAnnotation: ['params', 'returnType', 'rest', 'typeParameters'],
95
98
  Identifier: ['typeAnnotation'],
96
99
  IfStatement: ['test', 'consequent', 'alternate'],
97
100
  ImportAttribute: ['key', 'value'],
@@ -174,7 +177,7 @@ module.exports = {
174
177
  TypeAlias: ['id', 'typeParameters', 'right'],
175
178
  TypeAnnotation: ['typeAnnotation'],
176
179
  TypeCastExpression: ['expression', 'typeAnnotation'],
177
- TypeofTypeAnnotation: ['argument'],
180
+ TypeofTypeAnnotation: ['argument', 'typeArguments'],
178
181
  TypeOperator: ['typeAnnotation'],
179
182
  TypeParameter: ['bound', 'variance', 'default'],
180
183
  TypeParameterDeclaration: ['params'],
@@ -187,6 +187,9 @@ const HERMES_AST_VISITOR_KEYS = {
187
187
  id: 'Node',
188
188
  predicate: 'Node'
189
189
  },
190
+ DeclareHook: {
191
+ id: 'Node'
192
+ },
190
193
  DeclareInterface: {
191
194
  id: 'Node',
192
195
  typeParameters: 'Node',
@@ -318,6 +321,19 @@ const HERMES_AST_VISITOR_KEYS = {
318
321
  id: 'Node',
319
322
  typeParameters: 'Node'
320
323
  },
324
+ HookDeclaration: {
325
+ id: 'Node',
326
+ params: 'NodeList',
327
+ body: 'Node',
328
+ typeParameters: 'Node',
329
+ returnType: 'Node'
330
+ },
331
+ HookTypeAnnotation: {
332
+ params: 'NodeList',
333
+ returnType: 'Node',
334
+ rest: 'Node',
335
+ typeParameters: 'Node'
336
+ },
321
337
  Identifier: {
322
338
  typeAnnotation: 'Node'
323
339
  },
@@ -597,7 +613,8 @@ const HERMES_AST_VISITOR_KEYS = {
597
613
  typeAnnotation: 'Node'
598
614
  },
599
615
  TypeofTypeAnnotation: {
600
- argument: 'Node'
616
+ argument: 'Node',
617
+ typeArguments: 'Node'
601
618
  },
602
619
  TypeOperator: {
603
620
  typeAnnotation: 'Node'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-parser",
3
- "version": "0.18.2",
3
+ "version": "0.19.0",
4
4
  "description": "A JavaScript parser built from the Hermes engine",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -9,7 +9,7 @@
9
9
  "url": "git@github.com:facebook/hermes.git"
10
10
  },
11
11
  "dependencies": {
12
- "hermes-estree": "0.18.2"
12
+ "hermes-estree": "0.19.0"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@babel/parser": "7.7.4",