hermes-parser 0.18.2 → 0.19.1

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
  }
@@ -66,6 +66,7 @@ function transformProgram(program, _options) {
66
66
  case 'DeclareInterface':
67
67
  case 'DeclareModule':
68
68
  case 'DeclareModuleExports':
69
+ case 'DeclareNamespace':
69
70
  case 'DeclareOpaqueType':
70
71
  case 'DeclareTypeAlias':
71
72
  case 'DeclareVariable':
@@ -59,6 +59,7 @@ export function transformProgram(
59
59
  case 'DeclareInterface':
60
60
  case 'DeclareModule':
61
61
  case 'DeclareModuleExports':
62
+ case 'DeclareNamespace':
62
63
  case 'DeclareOpaqueType':
63
64
  case 'DeclareTypeAlias':
64
65
  case 'DeclareVariable':
@@ -80,6 +80,29 @@ function mapDeclareEnum(node) {
80
80
  parent: node.parent
81
81
  };
82
82
  }
83
+ /**
84
+ * Convert DeclareNamespace nodes to DeclareVariable
85
+ */
86
+
87
+
88
+ function mapDeclareNamespace(node) {
89
+ return {
90
+ type: 'DeclareVariable',
91
+ kind: 'const',
92
+ id: nodeWith(node.id, {
93
+ typeAnnotation: {
94
+ type: 'TypeAnnotation',
95
+ typeAnnotation: createAnyTypeAnnotation(node.body),
96
+ loc: node.body.loc,
97
+ range: node.body.range,
98
+ parent: EMPTY_PARENT
99
+ }
100
+ }),
101
+ loc: node.loc,
102
+ range: node.range,
103
+ parent: node.parent
104
+ };
105
+ }
83
106
  /**
84
107
  * Remove `this` param from functions.
85
108
  */
@@ -152,6 +175,7 @@ function transformProgram(program, _options) {
152
175
  case 'TupleTypeLabeledElement':
153
176
  case 'TupleTypeSpreadElement':
154
177
  case 'ComponentTypeAnnotation':
178
+ case 'HookTypeAnnotation':
155
179
  case 'TypeOperator':
156
180
  case 'TypePredicate':
157
181
  {
@@ -169,6 +193,11 @@ function transformProgram(program, _options) {
169
193
  return mapDeclareEnum(node);
170
194
  }
171
195
 
196
+ case 'DeclareNamespace':
197
+ {
198
+ return mapDeclareNamespace(node);
199
+ }
200
+
172
201
  case 'FunctionDeclaration':
173
202
  case 'FunctionExpression':
174
203
  {
@@ -23,6 +23,7 @@ import type {
23
23
  Program,
24
24
  ESNode,
25
25
  DeclareEnum,
26
+ DeclareNamespace,
26
27
  DeclareVariable,
27
28
  AnyTypeAnnotation,
28
29
  GenericTypeAnnotation,
@@ -92,6 +93,28 @@ function mapDeclareEnum(node: DeclareEnum): DeclareVariable {
92
93
  };
93
94
  }
94
95
 
96
+ /**
97
+ * Convert DeclareNamespace nodes to DeclareVariable
98
+ */
99
+ function mapDeclareNamespace(node: DeclareNamespace): DeclareVariable {
100
+ return {
101
+ type: 'DeclareVariable',
102
+ kind: 'const',
103
+ id: nodeWith(node.id, {
104
+ typeAnnotation: {
105
+ type: 'TypeAnnotation',
106
+ typeAnnotation: createAnyTypeAnnotation(node.body),
107
+ loc: node.body.loc,
108
+ range: node.body.range,
109
+ parent: EMPTY_PARENT,
110
+ },
111
+ }),
112
+ loc: node.loc,
113
+ range: node.range,
114
+ parent: node.parent,
115
+ };
116
+ }
117
+
95
118
  /**
96
119
  * Remove `this` param from functions.
97
120
  */
@@ -165,6 +188,7 @@ export function transformProgram(
165
188
  case 'TupleTypeLabeledElement':
166
189
  case 'TupleTypeSpreadElement':
167
190
  case 'ComponentTypeAnnotation':
191
+ case 'HookTypeAnnotation':
168
192
  case 'TypeOperator':
169
193
  case 'TypePredicate': {
170
194
  // Babel does not support these generic types, so convert to any
@@ -176,6 +200,9 @@ export function transformProgram(
176
200
  case 'DeclareEnum': {
177
201
  return mapDeclareEnum(node);
178
202
  }
203
+ case 'DeclareNamespace': {
204
+ return mapDeclareNamespace(node);
205
+ }
179
206
  case 'FunctionDeclaration':
180
207
  case 'FunctionExpression': {
181
208
  return mapFunction(node);
@@ -60,9 +60,11 @@ 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'],
67
+ DeclareNamespace: ['id', 'body'],
66
68
  DeclareOpaqueType: ['id', 'typeParameters', 'impltype', 'supertype'],
67
69
  DeclareTypeAlias: ['id', 'typeParameters', 'right'],
68
70
  DeclareVariable: ['id'],
@@ -92,6 +94,8 @@ module.exports = {
92
94
  FunctionTypeAnnotation: ['params', 'this', 'returnType', 'rest', 'typeParameters'],
93
95
  FunctionTypeParam: ['name', 'typeAnnotation'],
94
96
  GenericTypeAnnotation: ['id', 'typeParameters'],
97
+ HookDeclaration: ['id', 'params', 'body', 'typeParameters', 'returnType'],
98
+ HookTypeAnnotation: ['params', 'returnType', 'rest', 'typeParameters'],
95
99
  Identifier: ['typeAnnotation'],
96
100
  IfStatement: ['test', 'consequent', 'alternate'],
97
101
  ImportAttribute: ['key', 'value'],
@@ -174,7 +178,7 @@ module.exports = {
174
178
  TypeAlias: ['id', 'typeParameters', 'right'],
175
179
  TypeAnnotation: ['typeAnnotation'],
176
180
  TypeCastExpression: ['expression', 'typeAnnotation'],
177
- TypeofTypeAnnotation: ['argument'],
181
+ TypeofTypeAnnotation: ['argument', 'typeArguments'],
178
182
  TypeOperator: ['typeAnnotation'],
179
183
  TypeParameter: ['bound', 'variance', 'default'],
180
184
  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',
@@ -200,6 +203,10 @@ const HERMES_AST_VISITOR_KEYS = {
200
203
  DeclareModuleExports: {
201
204
  typeAnnotation: 'Node'
202
205
  },
206
+ DeclareNamespace: {
207
+ id: 'Node',
208
+ body: 'Node'
209
+ },
203
210
  DeclareOpaqueType: {
204
211
  id: 'Node',
205
212
  typeParameters: 'Node',
@@ -318,6 +325,19 @@ const HERMES_AST_VISITOR_KEYS = {
318
325
  id: 'Node',
319
326
  typeParameters: 'Node'
320
327
  },
328
+ HookDeclaration: {
329
+ id: 'Node',
330
+ params: 'NodeList',
331
+ body: 'Node',
332
+ typeParameters: 'Node',
333
+ returnType: 'Node'
334
+ },
335
+ HookTypeAnnotation: {
336
+ params: 'NodeList',
337
+ returnType: 'Node',
338
+ rest: 'Node',
339
+ typeParameters: 'Node'
340
+ },
321
341
  Identifier: {
322
342
  typeAnnotation: 'Node'
323
343
  },
@@ -597,7 +617,8 @@ const HERMES_AST_VISITOR_KEYS = {
597
617
  typeAnnotation: 'Node'
598
618
  },
599
619
  TypeofTypeAnnotation: {
600
- argument: 'Node'
620
+ argument: 'Node',
621
+ typeArguments: 'Node'
601
622
  },
602
623
  TypeOperator: {
603
624
  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.1",
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.1"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@babel/parser": "7.7.4",