@react-native/codegen 0.77.0-nightly-20241002-d19a2178b → 0.77.0-nightly-20241005-0b0ac81fb

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.
@@ -122,8 +122,12 @@ const ModuleSpecClassDeclarationTemplate = ({
122
122
  return `template <typename T>
123
123
  class JSI_EXPORT ${hasteModuleName}CxxSpec : public TurboModule {
124
124
  public:
125
- jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propName) override {
126
- return delegate_.get(rt, propName);
125
+ jsi::Value create(jsi::Runtime &rt, const jsi::PropNameID &propName) override {
126
+ return delegate_.create(rt, propName);
127
+ }
128
+
129
+ std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& runtime) override {
130
+ return delegate_.getPropertyNames(runtime);
127
131
  }
128
132
 
129
133
  static constexpr std::string_view kModuleName = "${moduleName}";
@@ -76,8 +76,12 @@ const ModuleSpecClassDeclarationTemplate = ({
76
76
  return `template <typename T>
77
77
  class JSI_EXPORT ${hasteModuleName}CxxSpec : public TurboModule {
78
78
  public:
79
- jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propName) override {
80
- return delegate_.get(rt, propName);
79
+ jsi::Value create(jsi::Runtime &rt, const jsi::PropNameID &propName) override {
80
+ return delegate_.create(rt, propName);
81
+ }
82
+
83
+ std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& runtime) override {
84
+ return delegate_.getPropertyNames(runtime);
81
85
  }
82
86
 
83
87
  static constexpr std::string_view kModuleName = "${moduleName}";
@@ -191,7 +191,28 @@ class StructCollector {
191
191
  case 'MixedTypeAnnotation':
192
192
  throw new Error('Mixed types are unsupported in structs');
193
193
  case 'UnionTypeAnnotation':
194
- throw new Error('Union types are unsupported in structs');
194
+ switch (typeAnnotation.memberType) {
195
+ case 'StringTypeAnnotation':
196
+ return wrapNullable(nullable, {
197
+ type: 'StringTypeAnnotation',
198
+ });
199
+ case 'NumberTypeAnnotation':
200
+ return wrapNullable(nullable, {
201
+ type: 'NumberTypeAnnotation',
202
+ });
203
+ case 'ObjectTypeAnnotation':
204
+ // This isn't smart enough to actually know how to generate the
205
+ // options on the native side. So we just treat it as an unknown object type
206
+ return wrapNullable(nullable, {
207
+ type: 'GenericObjectTypeAnnotation',
208
+ });
209
+ default:
210
+ typeAnnotation.memberType;
211
+ throw new Error(
212
+ 'Union types are unsupported in structs' +
213
+ JSON.stringify(typeAnnotation),
214
+ );
215
+ }
195
216
  default: {
196
217
  return wrapNullable(nullable, typeAnnotation);
197
218
  }
@@ -121,7 +121,28 @@ class StructCollector {
121
121
  case 'MixedTypeAnnotation':
122
122
  throw new Error('Mixed types are unsupported in structs');
123
123
  case 'UnionTypeAnnotation':
124
- throw new Error('Union types are unsupported in structs');
124
+ switch (typeAnnotation.memberType) {
125
+ case 'StringTypeAnnotation':
126
+ return wrapNullable(nullable, {
127
+ type: 'StringTypeAnnotation',
128
+ });
129
+ case 'NumberTypeAnnotation':
130
+ return wrapNullable(nullable, {
131
+ type: 'NumberTypeAnnotation',
132
+ });
133
+ case 'ObjectTypeAnnotation':
134
+ // This isn't smart enough to actually know how to generate the
135
+ // options on the native side. So we just treat it as an unknown object type
136
+ return wrapNullable(nullable, {
137
+ type: 'GenericObjectTypeAnnotation',
138
+ });
139
+ default:
140
+ (typeAnnotation.memberType: empty);
141
+ throw new Error(
142
+ 'Union types are unsupported in structs' +
143
+ JSON.stringify(typeAnnotation),
144
+ );
145
+ }
125
146
  default: {
126
147
  return wrapNullable(nullable, typeAnnotation);
127
148
  }
@@ -2562,6 +2562,66 @@ const SAMPLE_WITH_UPPERCASE_NAME = {
2562
2562
  },
2563
2563
  },
2564
2564
  };
2565
+ const UNION_MODULE = {
2566
+ modules: {
2567
+ NativeSampleTurboModule: {
2568
+ type: 'NativeModule',
2569
+ aliasMap: {},
2570
+ enumMap: {},
2571
+ spec: {
2572
+ eventEmitters: [],
2573
+ methods: [
2574
+ {
2575
+ name: 'getUnion',
2576
+ optional: false,
2577
+ typeAnnotation: {
2578
+ type: 'FunctionTypeAnnotation',
2579
+ returnTypeAnnotation: {
2580
+ type: 'UnionTypeAnnotation',
2581
+ memberType: 'ObjectTypeAnnotation',
2582
+ },
2583
+ params: [
2584
+ {
2585
+ name: 'chooseInt',
2586
+ optional: false,
2587
+ typeAnnotation: {
2588
+ type: 'UnionTypeAnnotation',
2589
+ memberType: 'NumberTypeAnnotation',
2590
+ },
2591
+ },
2592
+ {
2593
+ name: 'chooseFloat',
2594
+ optional: false,
2595
+ typeAnnotation: {
2596
+ type: 'UnionTypeAnnotation',
2597
+ memberType: 'NumberTypeAnnotation',
2598
+ },
2599
+ },
2600
+ {
2601
+ name: 'chooseObject',
2602
+ optional: false,
2603
+ typeAnnotation: {
2604
+ type: 'UnionTypeAnnotation',
2605
+ memberType: 'ObjectTypeAnnotation',
2606
+ },
2607
+ },
2608
+ {
2609
+ name: 'chooseString',
2610
+ optional: false,
2611
+ typeAnnotation: {
2612
+ type: 'UnionTypeAnnotation',
2613
+ memberType: 'StringTypeAnnotation',
2614
+ },
2615
+ },
2616
+ ],
2617
+ },
2618
+ },
2619
+ ],
2620
+ },
2621
+ moduleName: 'SampleTurboModule',
2622
+ },
2623
+ },
2624
+ };
2565
2625
  module.exports = {
2566
2626
  complex_objects: COMPLEX_OBJECTS,
2567
2627
  two_modules_different_files: TWO_MODULES_DIFFERENT_FILES,
@@ -2572,4 +2632,5 @@ module.exports = {
2572
2632
  real_module_example: REAL_MODULE_EXAMPLE,
2573
2633
  cxx_only_native_modules: CXX_ONLY_NATIVE_MODULES,
2574
2634
  SampleWithUppercaseName: SAMPLE_WITH_UPPERCASE_NAME,
2635
+ union_module: UNION_MODULE,
2575
2636
  };
@@ -2575,6 +2575,67 @@ const SAMPLE_WITH_UPPERCASE_NAME: SchemaType = {
2575
2575
  },
2576
2576
  };
2577
2577
 
2578
+ const UNION_MODULE: SchemaType = {
2579
+ modules: {
2580
+ NativeSampleTurboModule: {
2581
+ type: 'NativeModule',
2582
+ aliasMap: {},
2583
+ enumMap: {},
2584
+ spec: {
2585
+ eventEmitters: [],
2586
+ methods: [
2587
+ {
2588
+ name: 'getUnion',
2589
+ optional: false,
2590
+ typeAnnotation: {
2591
+ type: 'FunctionTypeAnnotation',
2592
+ returnTypeAnnotation: {
2593
+ type: 'UnionTypeAnnotation',
2594
+ memberType: 'ObjectTypeAnnotation',
2595
+ },
2596
+ params: [
2597
+ {
2598
+ name: 'chooseInt',
2599
+ optional: false,
2600
+ typeAnnotation: {
2601
+ type: 'UnionTypeAnnotation',
2602
+ memberType: 'NumberTypeAnnotation',
2603
+ },
2604
+ },
2605
+ {
2606
+ name: 'chooseFloat',
2607
+ optional: false,
2608
+ typeAnnotation: {
2609
+ type: 'UnionTypeAnnotation',
2610
+ memberType: 'NumberTypeAnnotation',
2611
+ },
2612
+ },
2613
+ {
2614
+ name: 'chooseObject',
2615
+ optional: false,
2616
+ typeAnnotation: {
2617
+ type: 'UnionTypeAnnotation',
2618
+ memberType: 'ObjectTypeAnnotation',
2619
+ },
2620
+ },
2621
+ {
2622
+ name: 'chooseString',
2623
+ optional: false,
2624
+ typeAnnotation: {
2625
+ type: 'UnionTypeAnnotation',
2626
+ memberType: 'StringTypeAnnotation',
2627
+ },
2628
+ },
2629
+ ],
2630
+ },
2631
+ },
2632
+ ],
2633
+ },
2634
+ moduleName: 'SampleTurboModule',
2635
+ },
2636
+ },
2637
+ };
2638
+
2578
2639
  module.exports = {
2579
2640
  complex_objects: COMPLEX_OBJECTS,
2580
2641
  two_modules_different_files: TWO_MODULES_DIFFERENT_FILES,
@@ -2585,4 +2646,5 @@ module.exports = {
2585
2646
  real_module_example: REAL_MODULE_EXAMPLE,
2586
2647
  cxx_only_native_modules: CXX_ONLY_NATIVE_MODULES,
2587
2648
  SampleWithUppercaseName: SAMPLE_WITH_UPPERCASE_NAME,
2649
+ union_module: UNION_MODULE,
2588
2650
  };
@@ -599,7 +599,34 @@ export interface Spec extends TurboModule {
599
599
  }
600
600
 
601
601
  export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
602
+ `;
603
+ const NATIVE_MODULE_WITH_UNION_RETURN_TYPES = `
604
+ /**
605
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
606
+ *
607
+ * This source code is licensed under the MIT license found in the
608
+ * LICENSE file in the root directory of this source tree.
609
+ *
610
+ * @flow strict
611
+ * @format
612
+ */
613
+
614
+ import type {TurboModule} from '../../../../Libraries/TurboModule/RCTExport';
615
+
616
+ import * as TurboModuleRegistry from '../../../../Libraries/TurboModule/TurboModuleRegistry';
617
+
618
+ export interface Spec extends TurboModule {
619
+ +getStringUnion: () => 'light' | 'dark';
620
+ +setStringUnion: (strings: 'light' | 'dark') => void;
621
+
622
+ +getNumberUnion: () => 1 | 2;
623
+ +setNumberUnion: (numbers: 1 | 2) => void;
624
+
625
+ +getObjectUnion: () => {a: 1} | {b: 2};
626
+ +setObjectUnion: (objects: {a: 1} | {b: 2}) => void;
627
+ }
602
628
 
629
+ export default (TurboModuleRegistry.get<Spec>('SampleTurboModule'): ?Spec);
603
630
  `;
604
631
  const NATIVE_MODULE_WITH_EVENT_EMITTERS = `
605
632
  /**
@@ -828,6 +855,7 @@ module.exports = {
828
855
  NATIVE_MODULE_WITH_BASIC_PARAM_TYPES,
829
856
  NATIVE_MODULE_WITH_CALLBACK,
830
857
  NATIVE_MODULE_WITH_UNION,
858
+ NATIVE_MODULE_WITH_UNION_RETURN_TYPES,
831
859
  NATIVE_MODULE_WITH_EVENT_EMITTERS,
832
860
  EMPTY_NATIVE_MODULE,
833
861
  ANDROID_ONLY_NATIVE_MODULE,
@@ -619,7 +619,35 @@ export interface Spec extends TurboModule {
619
619
  }
620
620
 
621
621
  export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
622
+ `;
623
+
624
+ const NATIVE_MODULE_WITH_UNION_RETURN_TYPES = `
625
+ /**
626
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
627
+ *
628
+ * This source code is licensed under the MIT license found in the
629
+ * LICENSE file in the root directory of this source tree.
630
+ *
631
+ * @flow strict
632
+ * @format
633
+ */
634
+
635
+ import type {TurboModule} from '../../../../Libraries/TurboModule/RCTExport';
636
+
637
+ import * as TurboModuleRegistry from '../../../../Libraries/TurboModule/TurboModuleRegistry';
638
+
639
+ export interface Spec extends TurboModule {
640
+ +getStringUnion: () => 'light' | 'dark';
641
+ +setStringUnion: (strings: 'light' | 'dark') => void;
642
+
643
+ +getNumberUnion: () => 1 | 2;
644
+ +setNumberUnion: (numbers: 1 | 2) => void;
645
+
646
+ +getObjectUnion: () => {a: 1} | {b: 2};
647
+ +setObjectUnion: (objects: {a: 1} | {b: 2}) => void;
648
+ }
622
649
 
650
+ export default (TurboModuleRegistry.get<Spec>('SampleTurboModule'): ?Spec);
623
651
  `;
624
652
 
625
653
  const NATIVE_MODULE_WITH_EVENT_EMITTERS = `
@@ -854,6 +882,7 @@ module.exports = {
854
882
  NATIVE_MODULE_WITH_BASIC_PARAM_TYPES,
855
883
  NATIVE_MODULE_WITH_CALLBACK,
856
884
  NATIVE_MODULE_WITH_UNION,
885
+ NATIVE_MODULE_WITH_UNION_RETURN_TYPES,
857
886
  NATIVE_MODULE_WITH_EVENT_EMITTERS,
858
887
  EMPTY_NATIVE_MODULE,
859
888
  ANDROID_ONLY_NATIVE_MODULE,
@@ -697,6 +697,32 @@ export interface Spec extends TurboModule {
697
697
 
698
698
  export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
699
699
 
700
+ `;
701
+ const NATIVE_MODULE_WITH_UNION_RETURN_TYPES = `
702
+ /**
703
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
704
+ *
705
+ * This source code is licensed under the MIT license found in the
706
+ * LICENSE file in the root directory of this source tree.
707
+ *
708
+ * @format
709
+ */
710
+
711
+ import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
712
+ import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
713
+
714
+ export interface Spec extends TurboModule {
715
+ readonly getStringUnion: () => 'light' | 'dark';
716
+ readonly setStringUnion: (strings: 'light' | 'dark') => void;
717
+
718
+ readonly getNumberUnion: () => 1 | 2;
719
+ readonly setNumberUnion: (numbers: 1 | 2) => void;
720
+
721
+ readonly getObjectUnion: () => {a: 1} | {b: 2};
722
+ readonly setObjectUnion: (objects: {a: 1} | {b: 2}) => void;
723
+ }
724
+
725
+ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
700
726
  `;
701
727
  const NATIVE_MODULE_WITH_EVENT_EMITTERS = `
702
728
  /**
@@ -881,6 +907,7 @@ module.exports = {
881
907
  NATIVE_MODULE_WITH_BASIC_PARAM_TYPES,
882
908
  NATIVE_MODULE_WITH_CALLBACK,
883
909
  NATIVE_MODULE_WITH_UNION,
910
+ NATIVE_MODULE_WITH_UNION_RETURN_TYPES,
884
911
  NATIVE_MODULE_WITH_EVENT_EMITTERS,
885
912
  EMPTY_NATIVE_MODULE,
886
913
  ANDROID_ONLY_NATIVE_MODULE,
@@ -725,6 +725,33 @@ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
725
725
 
726
726
  `;
727
727
 
728
+ const NATIVE_MODULE_WITH_UNION_RETURN_TYPES = `
729
+ /**
730
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
731
+ *
732
+ * This source code is licensed under the MIT license found in the
733
+ * LICENSE file in the root directory of this source tree.
734
+ *
735
+ * @format
736
+ */
737
+
738
+ import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
739
+ import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
740
+
741
+ export interface Spec extends TurboModule {
742
+ readonly getStringUnion: () => 'light' | 'dark';
743
+ readonly setStringUnion: (strings: 'light' | 'dark') => void;
744
+
745
+ readonly getNumberUnion: () => 1 | 2;
746
+ readonly setNumberUnion: (numbers: 1 | 2) => void;
747
+
748
+ readonly getObjectUnion: () => {a: 1} | {b: 2};
749
+ readonly setObjectUnion: (objects: {a: 1} | {b: 2}) => void;
750
+ }
751
+
752
+ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
753
+ `;
754
+
728
755
  const NATIVE_MODULE_WITH_EVENT_EMITTERS = `
729
756
  /**
730
757
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -912,6 +939,7 @@ module.exports = {
912
939
  NATIVE_MODULE_WITH_BASIC_PARAM_TYPES,
913
940
  NATIVE_MODULE_WITH_CALLBACK,
914
941
  NATIVE_MODULE_WITH_UNION,
942
+ NATIVE_MODULE_WITH_UNION_RETURN_TYPES,
915
943
  NATIVE_MODULE_WITH_EVENT_EMITTERS,
916
944
  EMPTY_NATIVE_MODULE,
917
945
  ANDROID_ONLY_NATIVE_MODULE,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native/codegen",
3
- "version": "0.77.0-nightly-20241002-d19a2178b",
3
+ "version": "0.77.0-nightly-20241005-0b0ac81fb",
4
4
  "description": "Code generation tools for React Native",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -31,7 +31,7 @@
31
31
  "dependencies": {
32
32
  "@babel/parser": "^7.25.3",
33
33
  "glob": "^7.1.1",
34
- "hermes-parser": "0.23.1",
34
+ "hermes-parser": "0.24.0",
35
35
  "invariant": "^2.2.4",
36
36
  "jscodeshift": "^17.0.0",
37
37
  "nullthrows": "^1.1.1",
@@ -49,7 +49,7 @@
49
49
  "@babel/plugin-transform-flow-strip-types": "^7.25.2",
50
50
  "@babel/preset-env": "^7.25.3",
51
51
  "chalk": "^4.0.0",
52
- "hermes-estree": "0.23.1",
52
+ "hermes-estree": "0.24.0",
53
53
  "micromatch": "^4.0.4",
54
54
  "prettier": "2.8.8",
55
55
  "rimraf": "^3.0.2"