hermes-parser 0.21.1 → 0.23.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.
@@ -14,5 +14,5 @@ exports.ParserOptionsKeys = void 0;
14
14
  *
15
15
  * @format
16
16
  */
17
- const ParserOptionsKeys = new Set(['allowReturnOutsideFunction', 'babel', 'flow', 'enableExperimentalComponentSyntax', 'sourceFilename', 'sourceType', 'tokens']);
17
+ const ParserOptionsKeys = new Set(['allowReturnOutsideFunction', 'babel', 'flow', 'enableExperimentalComponentSyntax', 'reactRuntimeTarget', 'sourceFilename', 'sourceType', 'tokens']);
18
18
  exports.ParserOptionsKeys = ParserOptionsKeys;
@@ -13,6 +13,7 @@ export type ParserOptions = {
13
13
  babel?: boolean,
14
14
  flow?: 'all' | 'detect',
15
15
  enableExperimentalComponentSyntax?: boolean,
16
+ reactRuntimeTarget?: '18' | '19',
16
17
  sourceFilename?: string,
17
18
  sourceType?: 'module' | 'script' | 'unambiguous',
18
19
  tokens?: boolean,
@@ -23,6 +24,7 @@ export const ParserOptionsKeys: $ReadOnlySet<$Keys<ParserOptions>> = new Set([
23
24
  'babel',
24
25
  'flow',
25
26
  'enableExperimentalComponentSyntax',
27
+ 'reactRuntimeTarget',
26
28
  'sourceFilename',
27
29
  'sourceType',
28
30
  'tokens',
@@ -138,7 +138,9 @@ function createPropsTypeAnnotation(propTypes, spread, loc, range) {
138
138
  };
139
139
  }
140
140
 
141
- function mapComponentParameters(params) {
141
+ function mapComponentParameters(params, options) {
142
+ var _options$reactRuntime;
143
+
142
144
  if (params.length === 0) {
143
145
  return {
144
146
  props: null,
@@ -153,18 +155,19 @@ function mapComponentParameters(params) {
153
155
  props: restElementArgument,
154
156
  ref: null
155
157
  };
156
- } // Filter out any ref param and capture it's details.
158
+ } // Filter out any ref param and capture it's details when targeting React 18.
159
+ // React 19+ treats ref as a regular prop for function components.
157
160
 
158
161
 
159
162
  let refParam = null;
160
- const paramsWithoutRef = params.filter(param => {
163
+ const paramsWithoutRef = ((_options$reactRuntime = options.reactRuntimeTarget) != null ? _options$reactRuntime : '18') === '18' ? params.filter(param => {
161
164
  if (param.type === 'ComponentParameter' && getComponentParameterName(param.name) === 'ref') {
162
165
  refParam = param;
163
166
  return false;
164
167
  }
165
168
 
166
169
  return true;
167
- });
170
+ }) : params;
168
171
  const [propTypes, spread] = paramsWithoutRef.reduce(([propTypes, spread], param) => {
169
172
  switch (param.type) {
170
173
  case 'RestElement':
@@ -436,7 +439,7 @@ function createForwardRefWrapper(originalComponent) {
436
439
  };
437
440
  }
438
441
 
439
- function mapComponentDeclaration(node) {
442
+ function mapComponentDeclaration(node, options) {
440
443
  // Create empty loc for return type annotation nodes
441
444
  const createRendersTypeLoc = () => ({
442
445
  loc: {
@@ -477,7 +480,7 @@ function mapComponentDeclaration(node) {
477
480
  const {
478
481
  props,
479
482
  ref
480
- } = mapComponentParameters(node.params);
483
+ } = mapComponentParameters(node.params, options);
481
484
  let forwardRefDetails = null;
482
485
 
483
486
  if (ref != null) {
@@ -596,11 +599,11 @@ function scanForFirstComponentReference(compName, bodyList) {
596
599
  return null;
597
600
  }
598
601
 
599
- function mapComponentDeclarationIntoList(node, newBody, insertExport) {
602
+ function mapComponentDeclarationIntoList(node, newBody, options, insertExport) {
600
603
  const {
601
604
  comp,
602
605
  forwardRefDetails
603
- } = mapComponentDeclaration(node);
606
+ } = mapComponentDeclaration(node, options);
604
607
 
605
608
  if (forwardRefDetails != null) {
606
609
  // Scan for references to our component.
@@ -624,14 +627,14 @@ function mapComponentDeclarationIntoList(node, newBody, insertExport) {
624
627
  newBody.push(insertExport != null ? insertExport(comp) : comp);
625
628
  }
626
629
 
627
- function mapStatementList(stmts) {
630
+ function mapStatementList(stmts, options) {
628
631
  const newBody = [];
629
632
 
630
633
  for (const node of stmts) {
631
634
  switch (node.type) {
632
635
  case 'ComponentDeclaration':
633
636
  {
634
- mapComponentDeclarationIntoList(node, newBody);
637
+ mapComponentDeclarationIntoList(node, newBody, options);
635
638
  break;
636
639
  }
637
640
 
@@ -647,7 +650,7 @@ function mapStatementList(stmts) {
647
650
  var _node$declaration, _node$declaration2;
648
651
 
649
652
  if (((_node$declaration = node.declaration) == null ? void 0 : _node$declaration.type) === 'ComponentDeclaration') {
650
- mapComponentDeclarationIntoList(node.declaration, newBody, componentOrRef => {
653
+ mapComponentDeclarationIntoList(node.declaration, newBody, options, componentOrRef => {
651
654
  switch (componentOrRef.type) {
652
655
  case 'FunctionDeclaration':
653
656
  {
@@ -700,7 +703,7 @@ function mapStatementList(stmts) {
700
703
  var _node$declaration3, _node$declaration4;
701
704
 
702
705
  if (((_node$declaration3 = node.declaration) == null ? void 0 : _node$declaration3.type) === 'ComponentDeclaration') {
703
- mapComponentDeclarationIntoList(node.declaration, newBody, componentOrRef => nodeWith(node, {
706
+ mapComponentDeclarationIntoList(node.declaration, newBody, options, componentOrRef => nodeWith(node, {
704
707
  declaration: componentOrRef
705
708
  }));
706
709
  break;
@@ -728,7 +731,7 @@ function mapStatementList(stmts) {
728
731
  return newBody;
729
732
  }
730
733
 
731
- function transformProgram(program, _options) {
734
+ function transformProgram(program, options) {
732
735
  return _SimpleTransform.SimpleTransform.transformProgram(program, {
733
736
  transform(node) {
734
737
  switch (node.type) {
@@ -746,16 +749,17 @@ function transformProgram(program, _options) {
746
749
  case 'BlockStatement':
747
750
  {
748
751
  return nodeWith(node, {
749
- body: mapStatementList(node.body)
752
+ body: mapStatementList(node.body, options)
750
753
  });
751
754
  }
752
755
 
753
756
  case 'SwitchCase':
754
757
  {
758
+ const consequent = mapStatementList(node.consequent, options);
755
759
  return nodeWith(node, {
756
760
  /* $FlowExpectedError[incompatible-call] We know `mapStatementList` will
757
761
  not return `ModuleDeclaration` nodes if it is not passed any */
758
- consequent: mapStatementList(node.consequent)
762
+ consequent
759
763
  });
760
764
  }
761
765
 
@@ -174,6 +174,7 @@ function createPropsTypeAnnotation(
174
174
 
175
175
  function mapComponentParameters(
176
176
  params: $ReadOnlyArray<ComponentParameter | RestElement>,
177
+ options: ParserOptions,
177
178
  ): $ReadOnly<{
178
179
  props: ?(ObjectPattern | Identifier),
179
180
  ref: ?(BindingName | AssignmentPattern),
@@ -195,19 +196,22 @@ function mapComponentParameters(
195
196
  };
196
197
  }
197
198
 
198
- // Filter out any ref param and capture it's details.
199
+ // Filter out any ref param and capture it's details when targeting React 18.
200
+ // React 19+ treats ref as a regular prop for function components.
199
201
  let refParam = null;
200
- const paramsWithoutRef = params.filter(param => {
201
- if (
202
- param.type === 'ComponentParameter' &&
203
- getComponentParameterName(param.name) === 'ref'
204
- ) {
205
- refParam = param;
206
- return false;
207
- }
208
-
209
- return true;
210
- });
202
+ const paramsWithoutRef =
203
+ (options.reactRuntimeTarget ?? '18') === '18'
204
+ ? params.filter(param => {
205
+ if (
206
+ param.type === 'ComponentParameter' &&
207
+ getComponentParameterName(param.name) === 'ref'
208
+ ) {
209
+ refParam = param;
210
+ return false;
211
+ }
212
+ return true;
213
+ })
214
+ : params;
211
215
 
212
216
  const [propTypes, spread] = paramsWithoutRef.reduce<
213
217
  [Array<ObjectTypePropertySignature>, ?ObjectTypeSpreadProperty],
@@ -522,7 +526,10 @@ function createForwardRefWrapper(
522
526
  };
523
527
  }
524
528
 
525
- function mapComponentDeclaration(node: ComponentDeclaration): {
529
+ function mapComponentDeclaration(
530
+ node: ComponentDeclaration,
531
+ options: ParserOptions,
532
+ ): {
526
533
  comp: FunctionDeclaration,
527
534
  forwardRefDetails: ?ForwardRefDetails,
528
535
  } {
@@ -563,7 +570,7 @@ function mapComponentDeclaration(node: ComponentDeclaration): {
563
570
  ...createRendersTypeLoc(),
564
571
  };
565
572
 
566
- const {props, ref} = mapComponentParameters(node.params);
573
+ const {props, ref} = mapComponentParameters(node.params, options);
567
574
 
568
575
  let forwardRefDetails: ?ForwardRefDetails = null;
569
576
 
@@ -685,9 +692,10 @@ function scanForFirstComponentReference(
685
692
  function mapComponentDeclarationIntoList(
686
693
  node: ComponentDeclaration,
687
694
  newBody: Array<Statement | ModuleDeclaration>,
695
+ options: ParserOptions,
688
696
  insertExport?: (Identifier | FunctionDeclaration) => ModuleDeclaration,
689
697
  ) {
690
- const {comp, forwardRefDetails} = mapComponentDeclaration(node);
698
+ const {comp, forwardRefDetails} = mapComponentDeclaration(node, options);
691
699
  if (forwardRefDetails != null) {
692
700
  // Scan for references to our component.
693
701
  const referencePos = scanForFirstComponentReference(
@@ -715,12 +723,13 @@ function mapComponentDeclarationIntoList(
715
723
 
716
724
  function mapStatementList(
717
725
  stmts: $ReadOnlyArray<Statement | ModuleDeclaration>,
726
+ options: ParserOptions,
718
727
  ) {
719
728
  const newBody: Array<Statement | ModuleDeclaration> = [];
720
729
  for (const node of stmts) {
721
730
  switch (node.type) {
722
731
  case 'ComponentDeclaration': {
723
- mapComponentDeclarationIntoList(node, newBody);
732
+ mapComponentDeclarationIntoList(node, newBody, options);
724
733
  break;
725
734
  }
726
735
  case 'HookDeclaration': {
@@ -733,6 +742,7 @@ function mapStatementList(
733
742
  mapComponentDeclarationIntoList(
734
743
  node.declaration,
735
744
  newBody,
745
+ options,
736
746
  componentOrRef => {
737
747
  switch (componentOrRef.type) {
738
748
  case 'FunctionDeclaration': {
@@ -781,6 +791,7 @@ function mapStatementList(
781
791
  mapComponentDeclarationIntoList(
782
792
  node.declaration,
783
793
  newBody,
794
+ options,
784
795
  componentOrRef => nodeWith(node, {declaration: componentOrRef}),
785
796
  );
786
797
  break;
@@ -806,7 +817,7 @@ function mapStatementList(
806
817
 
807
818
  export function transformProgram(
808
819
  program: Program,
809
- _options: ParserOptions,
820
+ options: ParserOptions,
810
821
  ): Program {
811
822
  return SimpleTransform.transformProgram(program, {
812
823
  transform(node: ESNode) {
@@ -819,13 +830,14 @@ export function transformProgram(
819
830
  }
820
831
  case 'Program':
821
832
  case 'BlockStatement': {
822
- return nodeWith(node, {body: mapStatementList(node.body)});
833
+ return nodeWith(node, {body: mapStatementList(node.body, options)});
823
834
  }
824
835
  case 'SwitchCase': {
836
+ const consequent = mapStatementList(node.consequent, options);
825
837
  return nodeWith(node, {
826
838
  /* $FlowExpectedError[incompatible-call] We know `mapStatementList` will
827
839
  not return `ModuleDeclaration` nodes if it is not passed any */
828
- consequent: mapStatementList(node.consequent),
840
+ consequent,
829
841
  });
830
842
  }
831
843
  case 'ComponentDeclaration': {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-parser",
3
- "version": "0.21.1",
3
+ "version": "0.23.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.21.1"
12
+ "hermes-estree": "0.23.0"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@babel/parser": "7.7.4",