@react-native/codegen 0.84.0-nightly-20251204-5bb3a6d68 → 0.84.0-nightly-20251206-63b0aef13

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.
Files changed (48) hide show
  1. package/lib/CodegenSchema.d.ts +4 -13
  2. package/lib/CodegenSchema.js.flow +5 -13
  3. package/lib/generators/Utils.js +49 -0
  4. package/lib/generators/Utils.js.flow +58 -0
  5. package/lib/generators/components/CppHelpers.js +35 -2
  6. package/lib/generators/components/CppHelpers.js.flow +35 -2
  7. package/lib/generators/components/GenerateEventEmitterCpp.js +11 -2
  8. package/lib/generators/components/GenerateEventEmitterCpp.js.flow +11 -2
  9. package/lib/generators/components/GenerateEventEmitterH.js +24 -9
  10. package/lib/generators/components/GenerateEventEmitterH.js.flow +24 -10
  11. package/lib/generators/modules/GenerateModuleH.js +24 -17
  12. package/lib/generators/modules/GenerateModuleH.js.flow +24 -17
  13. package/lib/generators/modules/GenerateModuleJavaSpec.js +48 -34
  14. package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +48 -34
  15. package/lib/generators/modules/GenerateModuleJniCpp.js +29 -28
  16. package/lib/generators/modules/GenerateModuleJniCpp.js.flow +29 -28
  17. package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js +36 -22
  18. package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js.flow +39 -24
  19. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js +4 -3
  20. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js.flow +4 -3
  21. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js +7 -4
  22. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js.flow +7 -4
  23. package/lib/generators/modules/GenerateModuleObjCpp/serializeEventEmitter.js +19 -6
  24. package/lib/generators/modules/GenerateModuleObjCpp/serializeEventEmitter.js.flow +19 -6
  25. package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js +22 -23
  26. package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js.flow +22 -23
  27. package/lib/parsers/error-utils.js +1 -1
  28. package/lib/parsers/error-utils.js.flow +1 -1
  29. package/lib/parsers/errors.js +0 -15
  30. package/lib/parsers/errors.js.flow +0 -22
  31. package/lib/parsers/flow/components/events.js +1 -1
  32. package/lib/parsers/flow/components/events.js.flow +1 -1
  33. package/lib/parsers/flow/modules/index.js +12 -1
  34. package/lib/parsers/flow/modules/index.js.flow +12 -1
  35. package/lib/parsers/flow/parser.js +0 -3
  36. package/lib/parsers/flow/parser.js.flow +2 -8
  37. package/lib/parsers/parser.js.flow +2 -10
  38. package/lib/parsers/parserMock.js +0 -3
  39. package/lib/parsers/parserMock.js.flow +2 -8
  40. package/lib/parsers/parsers-primitives.js +41 -47
  41. package/lib/parsers/parsers-primitives.js.flow +45 -56
  42. package/lib/parsers/typescript/components/events.js +1 -1
  43. package/lib/parsers/typescript/components/events.js.flow +1 -1
  44. package/lib/parsers/typescript/modules/index.js +12 -1
  45. package/lib/parsers/typescript/modules/index.js.flow +12 -1
  46. package/lib/parsers/typescript/parser.js +0 -3
  47. package/lib/parsers/typescript/parser.js.flow +2 -8
  48. package/package.json +1 -1
@@ -41,7 +41,11 @@ const {
41
41
  unwrapNullable,
42
42
  wrapNullable,
43
43
  } = require('../../../parsers/parsers-commons');
44
- const {capitalize} = require('../../Utils');
44
+ const {
45
+ HeterogeneousUnionError,
46
+ capitalize,
47
+ parseValidUnionType,
48
+ } = require('../../Utils');
45
49
  class StructCollector {
46
50
  constructor() {
47
51
  _defineProperty(this, '_structs', new Map());
@@ -89,27 +93,37 @@ class StructCollector {
89
93
  case 'MixedTypeAnnotation':
90
94
  throw new Error('Mixed types are unsupported in structs');
91
95
  case 'UnionTypeAnnotation':
92
- switch (typeAnnotation.memberType) {
93
- case 'StringTypeAnnotation':
94
- return wrapNullable(nullable, {
95
- type: 'StringTypeAnnotation',
96
- });
97
- case 'NumberTypeAnnotation':
98
- return wrapNullable(nullable, {
99
- type: 'NumberTypeAnnotation',
100
- });
101
- case 'ObjectTypeAnnotation':
102
- // This isn't smart enough to actually know how to generate the
103
- // options on the native side. So we just treat it as an unknown object type
104
- return wrapNullable(nullable, {
105
- type: 'GenericObjectTypeAnnotation',
106
- });
107
- default:
108
- typeAnnotation.memberType;
109
- throw new Error(
110
- 'Union types are unsupported in structs' +
111
- JSON.stringify(typeAnnotation),
112
- );
96
+ try {
97
+ const validUnionType = parseValidUnionType(typeAnnotation);
98
+ switch (validUnionType) {
99
+ case 'boolean':
100
+ return wrapNullable(nullable, {
101
+ type: 'BooleanTypeAnnotation',
102
+ });
103
+ case 'number':
104
+ return wrapNullable(nullable, {
105
+ type: 'NumberTypeAnnotation',
106
+ });
107
+ case 'object':
108
+ // This isn't smart enough to actually know how to generate the
109
+ // options on the native side. So we just treat it as an unknown object type
110
+ return wrapNullable(nullable, {
111
+ type: 'GenericObjectTypeAnnotation',
112
+ });
113
+ case 'string':
114
+ return wrapNullable(nullable, {
115
+ type: 'StringTypeAnnotation',
116
+ });
117
+ default:
118
+ validUnionType;
119
+ throw new Error(`Unsupported union member types`);
120
+ }
121
+ } catch (ex) {
122
+ // TODO(T247151345): Implement proper heterogeneous union support.
123
+ if (ex instanceof HeterogeneousUnionError) {
124
+ return wrapNullable(nullable, typeAnnotation);
125
+ }
126
+ throw ex;
113
127
  }
114
128
  default: {
115
129
  return wrapNullable(nullable, typeAnnotation);
@@ -23,11 +23,11 @@ import type {
23
23
  NativeModuleNumberTypeAnnotation,
24
24
  NativeModuleObjectTypeAnnotation,
25
25
  NativeModuleTypeAliasTypeAnnotation,
26
+ NativeModuleUnionTypeAnnotation,
26
27
  Nullable,
27
28
  NumberLiteralTypeAnnotation,
28
29
  ReservedTypeAnnotation,
29
30
  StringLiteralTypeAnnotation,
30
- StringLiteralUnionTypeAnnotation,
31
31
  StringTypeAnnotation,
32
32
  } from '../../../CodegenSchema';
33
33
  import type {AliasResolver} from '../Utils';
@@ -36,7 +36,11 @@ const {
36
36
  unwrapNullable,
37
37
  wrapNullable,
38
38
  } = require('../../../parsers/parsers-commons');
39
- const {capitalize} = require('../../Utils');
39
+ const {
40
+ HeterogeneousUnionError,
41
+ capitalize,
42
+ parseValidUnionType,
43
+ } = require('../../Utils');
40
44
 
41
45
  type StructContext = 'CONSTANTS' | 'REGULAR';
42
46
 
@@ -63,7 +67,7 @@ export type StructProperty = $ReadOnly<{
63
67
  export type StructTypeAnnotation =
64
68
  | StringTypeAnnotation
65
69
  | StringLiteralTypeAnnotation
66
- | StringLiteralUnionTypeAnnotation
70
+ | NativeModuleUnionTypeAnnotation
67
71
  | NativeModuleNumberTypeAnnotation
68
72
  | NumberLiteralTypeAnnotation
69
73
  | BooleanLiteralTypeAnnotation
@@ -129,27 +133,38 @@ class StructCollector {
129
133
  case 'MixedTypeAnnotation':
130
134
  throw new Error('Mixed types are unsupported in structs');
131
135
  case 'UnionTypeAnnotation':
132
- switch (typeAnnotation.memberType) {
133
- case 'StringTypeAnnotation':
134
- return wrapNullable(nullable, {
135
- type: 'StringTypeAnnotation',
136
- });
137
- case 'NumberTypeAnnotation':
138
- return wrapNullable(nullable, {
139
- type: 'NumberTypeAnnotation',
140
- });
141
- case 'ObjectTypeAnnotation':
142
- // This isn't smart enough to actually know how to generate the
143
- // options on the native side. So we just treat it as an unknown object type
144
- return wrapNullable(nullable, {
145
- type: 'GenericObjectTypeAnnotation',
146
- });
147
- default:
148
- (typeAnnotation.memberType: empty);
149
- throw new Error(
150
- 'Union types are unsupported in structs' +
151
- JSON.stringify(typeAnnotation),
152
- );
136
+ try {
137
+ const validUnionType = parseValidUnionType(typeAnnotation);
138
+ switch (validUnionType) {
139
+ case 'boolean':
140
+ return wrapNullable(nullable, {
141
+ type: 'BooleanTypeAnnotation',
142
+ });
143
+ case 'number':
144
+ return wrapNullable(nullable, {
145
+ type: 'NumberTypeAnnotation',
146
+ });
147
+ case 'object':
148
+ // This isn't smart enough to actually know how to generate the
149
+ // options on the native side. So we just treat it as an unknown object type
150
+ return wrapNullable(nullable, {
151
+ type: 'GenericObjectTypeAnnotation',
152
+ });
153
+ case 'string':
154
+ return wrapNullable(nullable, {
155
+ type: 'StringTypeAnnotation',
156
+ });
157
+ default:
158
+ (validUnionType: empty);
159
+ throw new Error(`Unsupported union member types`);
160
+ }
161
+ } catch (ex) {
162
+ // TODO(T247151345): Implement proper heterogeneous union support.
163
+ if (ex instanceof HeterogeneousUnionError) {
164
+ return wrapNullable(nullable, typeAnnotation);
165
+ }
166
+
167
+ throw ex;
153
168
  }
154
169
  default: {
155
170
  return wrapNullable(nullable, typeAnnotation);
@@ -80,8 +80,9 @@ function toObjCType(
80
80
  return 'NSString *';
81
81
  case 'StringLiteralTypeAnnotation':
82
82
  return 'NSString *';
83
- case 'StringLiteralUnionTypeAnnotation':
84
- return 'NSString *';
83
+ case 'UnionTypeAnnotation':
84
+ // TODO(T247151345): Implement proper heterogeneous union support. This is unsafe.
85
+ return 'NSObject *';
85
86
  case 'NumberTypeAnnotation':
86
87
  return wrapCxxOptional('double', isRequired);
87
88
  case 'NumberLiteralTypeAnnotation':
@@ -160,7 +161,7 @@ function toObjCValue(
160
161
  return value;
161
162
  case 'StringLiteralTypeAnnotation':
162
163
  return value;
163
- case 'StringLiteralUnionTypeAnnotation':
164
+ case 'UnionTypeAnnotation':
164
165
  return value;
165
166
  case 'NumberTypeAnnotation':
166
167
  return wrapPrimitive('double');
@@ -96,8 +96,9 @@ function toObjCType(
96
96
  return 'NSString *';
97
97
  case 'StringLiteralTypeAnnotation':
98
98
  return 'NSString *';
99
- case 'StringLiteralUnionTypeAnnotation':
100
- return 'NSString *';
99
+ case 'UnionTypeAnnotation':
100
+ // TODO(T247151345): Implement proper heterogeneous union support. This is unsafe.
101
+ return 'NSObject *';
101
102
  case 'NumberTypeAnnotation':
102
103
  return wrapCxxOptional('double', isRequired);
103
104
  case 'NumberLiteralTypeAnnotation':
@@ -183,7 +184,7 @@ function toObjCValue(
183
184
  return value;
184
185
  case 'StringLiteralTypeAnnotation':
185
186
  return value;
186
- case 'StringLiteralUnionTypeAnnotation':
187
+ case 'UnionTypeAnnotation':
187
188
  return value;
188
189
  case 'NumberTypeAnnotation':
189
190
  return wrapPrimitive('double');
@@ -68,8 +68,9 @@ function toObjCType(
68
68
  return 'NSString *';
69
69
  case 'StringLiteralTypeAnnotation':
70
70
  return 'NSString *';
71
- case 'StringLiteralUnionTypeAnnotation':
72
- return 'NSString *';
71
+ case 'UnionTypeAnnotation':
72
+ // TODO(T247151345): Implement proper heterogeneous union support. This is unsafe.
73
+ return 'NSObject *';
73
74
  case 'NumberTypeAnnotation':
74
75
  return wrapCxxOptional('double', isRequired);
75
76
  case 'NumberLiteralTypeAnnotation':
@@ -149,8 +150,10 @@ function toObjCValue(
149
150
  return RCTBridgingTo('String');
150
151
  case 'StringLiteralTypeAnnotation':
151
152
  return RCTBridgingTo('String');
152
- case 'StringLiteralUnionTypeAnnotation':
153
- return RCTBridgingTo('String');
153
+ case 'UnionTypeAnnotation':
154
+ return !isRequired
155
+ ? `!RCTNilIfNull(${value}) ? std::optional<NSObject *>{} : std::optional<NSObject *>(${value})`
156
+ : value;
154
157
  case 'NumberTypeAnnotation':
155
158
  return RCTBridgingTo('Double');
156
159
  case 'NumberLiteralTypeAnnotation':
@@ -87,8 +87,9 @@ function toObjCType(
87
87
  return 'NSString *';
88
88
  case 'StringLiteralTypeAnnotation':
89
89
  return 'NSString *';
90
- case 'StringLiteralUnionTypeAnnotation':
91
- return 'NSString *';
90
+ case 'UnionTypeAnnotation':
91
+ // TODO(T247151345): Implement proper heterogeneous union support. This is unsafe.
92
+ return 'NSObject *';
92
93
  case 'NumberTypeAnnotation':
93
94
  return wrapCxxOptional('double', isRequired);
94
95
  case 'NumberLiteralTypeAnnotation':
@@ -173,8 +174,10 @@ function toObjCValue(
173
174
  return RCTBridgingTo('String');
174
175
  case 'StringLiteralTypeAnnotation':
175
176
  return RCTBridgingTo('String');
176
- case 'StringLiteralUnionTypeAnnotation':
177
- return RCTBridgingTo('String');
177
+ case 'UnionTypeAnnotation':
178
+ return !isRequired
179
+ ? `!RCTNilIfNull(${value}) ? std::optional<NSObject *>{} : std::optional<NSObject *>(${value})`
180
+ : value;
178
181
  case 'NumberTypeAnnotation':
179
182
  return RCTBridgingTo('Double');
180
183
  case 'NumberLiteralTypeAnnotation':
@@ -8,16 +8,29 @@
8
8
  * @format
9
9
  */
10
10
 
11
- const {toPascalCase} = require('../../Utils');
11
+ const {parseValidUnionType, toPascalCase} = require('../../Utils');
12
12
  function getEventEmitterTypeObjCType(eventEmitter) {
13
- const type = eventEmitter.typeAnnotation.typeAnnotation.type;
14
- switch (type) {
13
+ const typeAnnotation = eventEmitter.typeAnnotation.typeAnnotation;
14
+ switch (typeAnnotation.type) {
15
15
  case 'StringTypeAnnotation':
16
16
  return 'NSString *_Nonnull';
17
17
  case 'StringLiteralTypeAnnotation':
18
18
  return 'NSString *_Nonnull';
19
- case 'StringLiteralUnionTypeAnnotation':
20
- return 'NSString *_Nonnull';
19
+ case 'UnionTypeAnnotation':
20
+ const validUnionType = parseValidUnionType(typeAnnotation);
21
+ switch (validUnionType) {
22
+ case 'boolean':
23
+ return 'BOOL';
24
+ case 'number':
25
+ return 'NSNumber *_Nonnull';
26
+ case 'object':
27
+ return 'NSDictionary *';
28
+ case 'string':
29
+ return 'NSString *_Nonnull';
30
+ default:
31
+ validUnionType;
32
+ throw new Error(`Unsupported union member type`);
33
+ }
21
34
  case 'NumberTypeAnnotation':
22
35
  case 'NumberLiteralTypeAnnotation':
23
36
  return 'NSNumber *_Nonnull';
@@ -39,7 +52,7 @@ function getEventEmitterTypeObjCType(eventEmitter) {
39
52
  `Unsupported eventType for ${eventEmitter.name}. Found: ${eventEmitter.typeAnnotation.typeAnnotation.type}`,
40
53
  );
41
54
  default:
42
- type;
55
+ typeAnnotation.type;
43
56
  throw new Error(
44
57
  `Unsupported eventType for ${eventEmitter.name}. Found: ${eventEmitter.typeAnnotation.typeAnnotation.type}`,
45
58
  );
@@ -10,20 +10,33 @@
10
10
 
11
11
  import type {NativeModuleEventEmitterShape} from '../../../CodegenSchema';
12
12
 
13
- const {toPascalCase} = require('../../Utils');
13
+ const {parseValidUnionType, toPascalCase} = require('../../Utils');
14
14
 
15
15
  function getEventEmitterTypeObjCType(
16
16
  eventEmitter: NativeModuleEventEmitterShape,
17
17
  ): string {
18
- const type = eventEmitter.typeAnnotation.typeAnnotation.type;
18
+ const typeAnnotation = eventEmitter.typeAnnotation.typeAnnotation;
19
19
 
20
- switch (type) {
20
+ switch (typeAnnotation.type) {
21
21
  case 'StringTypeAnnotation':
22
22
  return 'NSString *_Nonnull';
23
23
  case 'StringLiteralTypeAnnotation':
24
24
  return 'NSString *_Nonnull';
25
- case 'StringLiteralUnionTypeAnnotation':
26
- return 'NSString *_Nonnull';
25
+ case 'UnionTypeAnnotation':
26
+ const validUnionType = parseValidUnionType(typeAnnotation);
27
+ switch (validUnionType) {
28
+ case 'boolean':
29
+ return 'BOOL';
30
+ case 'number':
31
+ return 'NSNumber *_Nonnull';
32
+ case 'object':
33
+ return 'NSDictionary *';
34
+ case 'string':
35
+ return 'NSString *_Nonnull';
36
+ default:
37
+ (validUnionType: empty);
38
+ throw new Error(`Unsupported union member type`);
39
+ }
27
40
  case 'NumberTypeAnnotation':
28
41
  case 'NumberLiteralTypeAnnotation':
29
42
  return 'NSNumber *_Nonnull';
@@ -45,7 +58,7 @@ function getEventEmitterTypeObjCType(
45
58
  `Unsupported eventType for ${eventEmitter.name}. Found: ${eventEmitter.typeAnnotation.typeAnnotation.type}`,
46
59
  );
47
60
  default:
48
- (type: empty);
61
+ (typeAnnotation.type: empty);
49
62
  throw new Error(
50
63
  `Unsupported eventType for ${eventEmitter.name}. Found: ${eventEmitter.typeAnnotation.typeAnnotation.type}`,
51
64
  );
@@ -15,7 +15,7 @@ const {
15
15
  wrapNullable,
16
16
  } = require('../../../parsers/parsers-commons');
17
17
  const {wrapOptional} = require('../../TypeUtils/Objective-C');
18
- const {capitalize} = require('../../Utils');
18
+ const {capitalize, parseValidUnionType} = require('../../Utils');
19
19
  const {getNamespacedStructName} = require('./Utils');
20
20
  const invariant = require('invariant');
21
21
  const ProtocolMethodTemplate = ({returnObjCType, methodName, params}) =>
@@ -210,8 +210,9 @@ function getParamObjCType(
210
210
  return notStruct(wrapOptional('NSString *', !nullable));
211
211
  case 'StringLiteralTypeAnnotation':
212
212
  return notStruct(wrapOptional('NSString *', !nullable));
213
- case 'StringLiteralUnionTypeAnnotation':
214
- return notStruct(wrapOptional('NSString *', !nullable));
213
+ case 'UnionTypeAnnotation':
214
+ // TODO(T247151345): Implement proper heterogeneous union support. This is unsafe.
215
+ return notStruct(wrapOptional('NSObject *', !nullable));
215
216
  case 'NumberTypeAnnotation':
216
217
  return notStruct(isRequired ? 'double' : 'NSNumber *');
217
218
  case 'NumberLiteralTypeAnnotation':
@@ -284,10 +285,6 @@ function getReturnObjCType(methodName, nullableTypeAnnotation) {
284
285
  // TODO: Can NSString * returns not be _Nullable?
285
286
  // In the legacy codegen, we don't surround NSSTring * with _Nullable
286
287
  return wrapOptional('NSString *', isRequired);
287
- case 'StringLiteralUnionTypeAnnotation':
288
- // TODO: Can NSString * returns not be _Nullable?
289
- // In the legacy codegen, we don't surround NSSTring * with _Nullable
290
- return wrapOptional('NSString *', isRequired);
291
288
  case 'NumberTypeAnnotation':
292
289
  return wrapOptional('NSNumber *', isRequired);
293
290
  case 'NumberLiteralTypeAnnotation':
@@ -314,19 +311,21 @@ function getReturnObjCType(methodName, nullableTypeAnnotation) {
314
311
  );
315
312
  }
316
313
  case 'UnionTypeAnnotation':
317
- switch (typeAnnotation.memberType) {
318
- case 'NumberTypeAnnotation':
314
+ const validUnionType = parseValidUnionType(typeAnnotation);
315
+ switch (validUnionType) {
316
+ case 'boolean':
319
317
  return wrapOptional('NSNumber *', isRequired);
320
- case 'ObjectTypeAnnotation':
318
+ case 'number':
319
+ return wrapOptional('NSNumber *', isRequired);
320
+ case 'object':
321
321
  return wrapOptional('NSDictionary *', isRequired);
322
- case 'StringTypeAnnotation':
322
+ case 'string':
323
323
  // TODO: Can NSString * returns not be _Nullable?
324
324
  // In the legacy codegen, we don't surround NSSTring * with _Nullable
325
325
  return wrapOptional('NSString *', isRequired);
326
326
  default:
327
- throw new Error(
328
- `Unsupported union return type for ${methodName}, found: ${typeAnnotation.memberType}"`,
329
- );
327
+ validUnionType;
328
+ throw new Error(`Unsupported union member type`);
330
329
  }
331
330
  case 'GenericObjectTypeAnnotation':
332
331
  return wrapOptional('NSDictionary *', isRequired);
@@ -356,8 +355,6 @@ function getReturnJSType(methodName, nullableTypeAnnotation) {
356
355
  return 'StringKind';
357
356
  case 'StringLiteralTypeAnnotation':
358
357
  return 'StringKind';
359
- case 'StringLiteralUnionTypeAnnotation':
360
- return 'StringKind';
361
358
  case 'NumberTypeAnnotation':
362
359
  return 'NumberKind';
363
360
  case 'NumberLiteralTypeAnnotation':
@@ -386,17 +383,19 @@ function getReturnJSType(methodName, nullableTypeAnnotation) {
386
383
  );
387
384
  }
388
385
  case 'UnionTypeAnnotation':
389
- switch (typeAnnotation.memberType) {
390
- case 'NumberTypeAnnotation':
386
+ const validUnionType = parseValidUnionType(typeAnnotation);
387
+ switch (validUnionType) {
388
+ case 'boolean':
389
+ return 'BooleanKind';
390
+ case 'number':
391
391
  return 'NumberKind';
392
- case 'ObjectTypeAnnotation':
392
+ case 'object':
393
393
  return 'ObjectKind';
394
- case 'StringTypeAnnotation':
394
+ case 'string':
395
395
  return 'StringKind';
396
396
  default:
397
- throw new Error(
398
- `Unsupported return type for ${methodName}. Found: ${typeAnnotation.type}`,
399
- );
397
+ validUnionType;
398
+ throw new Error(`Unsupported union member types`);
400
399
  }
401
400
  default:
402
401
  typeAnnotation.type;
@@ -25,7 +25,7 @@ const {
25
25
  wrapNullable,
26
26
  } = require('../../../parsers/parsers-commons');
27
27
  const {wrapOptional} = require('../../TypeUtils/Objective-C');
28
- const {capitalize} = require('../../Utils');
28
+ const {capitalize, parseValidUnionType} = require('../../Utils');
29
29
  const {getNamespacedStructName} = require('./Utils');
30
30
  const invariant = require('invariant');
31
31
 
@@ -259,8 +259,9 @@ function getParamObjCType(
259
259
  return notStruct(wrapOptional('NSString *', !nullable));
260
260
  case 'StringLiteralTypeAnnotation':
261
261
  return notStruct(wrapOptional('NSString *', !nullable));
262
- case 'StringLiteralUnionTypeAnnotation':
263
- return notStruct(wrapOptional('NSString *', !nullable));
262
+ case 'UnionTypeAnnotation':
263
+ // TODO(T247151345): Implement proper heterogeneous union support. This is unsafe.
264
+ return notStruct(wrapOptional('NSObject *', !nullable));
264
265
  case 'NumberTypeAnnotation':
265
266
  return notStruct(isRequired ? 'double' : 'NSNumber *');
266
267
  case 'NumberLiteralTypeAnnotation':
@@ -342,10 +343,6 @@ function getReturnObjCType(
342
343
  // TODO: Can NSString * returns not be _Nullable?
343
344
  // In the legacy codegen, we don't surround NSSTring * with _Nullable
344
345
  return wrapOptional('NSString *', isRequired);
345
- case 'StringLiteralUnionTypeAnnotation':
346
- // TODO: Can NSString * returns not be _Nullable?
347
- // In the legacy codegen, we don't surround NSSTring * with _Nullable
348
- return wrapOptional('NSString *', isRequired);
349
346
  case 'NumberTypeAnnotation':
350
347
  return wrapOptional('NSNumber *', isRequired);
351
348
  case 'NumberLiteralTypeAnnotation':
@@ -372,19 +369,21 @@ function getReturnObjCType(
372
369
  );
373
370
  }
374
371
  case 'UnionTypeAnnotation':
375
- switch (typeAnnotation.memberType) {
376
- case 'NumberTypeAnnotation':
372
+ const validUnionType = parseValidUnionType(typeAnnotation);
373
+ switch (validUnionType) {
374
+ case 'boolean':
377
375
  return wrapOptional('NSNumber *', isRequired);
378
- case 'ObjectTypeAnnotation':
376
+ case 'number':
377
+ return wrapOptional('NSNumber *', isRequired);
378
+ case 'object':
379
379
  return wrapOptional('NSDictionary *', isRequired);
380
- case 'StringTypeAnnotation':
380
+ case 'string':
381
381
  // TODO: Can NSString * returns not be _Nullable?
382
382
  // In the legacy codegen, we don't surround NSSTring * with _Nullable
383
383
  return wrapOptional('NSString *', isRequired);
384
384
  default:
385
- throw new Error(
386
- `Unsupported union return type for ${methodName}, found: ${typeAnnotation.memberType}"`,
387
- );
385
+ (validUnionType: empty);
386
+ throw new Error(`Unsupported union member type`);
388
387
  }
389
388
  case 'GenericObjectTypeAnnotation':
390
389
  return wrapOptional('NSDictionary *', isRequired);
@@ -418,8 +417,6 @@ function getReturnJSType(
418
417
  return 'StringKind';
419
418
  case 'StringLiteralTypeAnnotation':
420
419
  return 'StringKind';
421
- case 'StringLiteralUnionTypeAnnotation':
422
- return 'StringKind';
423
420
  case 'NumberTypeAnnotation':
424
421
  return 'NumberKind';
425
422
  case 'NumberLiteralTypeAnnotation':
@@ -448,17 +445,19 @@ function getReturnJSType(
448
445
  );
449
446
  }
450
447
  case 'UnionTypeAnnotation':
451
- switch (typeAnnotation.memberType) {
452
- case 'NumberTypeAnnotation':
448
+ const validUnionType = parseValidUnionType(typeAnnotation);
449
+ switch (validUnionType) {
450
+ case 'boolean':
451
+ return 'BooleanKind';
452
+ case 'number':
453
453
  return 'NumberKind';
454
- case 'ObjectTypeAnnotation':
454
+ case 'object':
455
455
  return 'ObjectKind';
456
- case 'StringTypeAnnotation':
456
+ case 'string':
457
457
  return 'StringKind';
458
458
  default:
459
- throw new Error(
460
- `Unsupported return type for ${methodName}. Found: ${typeAnnotation.type}`,
461
- );
459
+ (validUnionType: empty);
460
+ throw new Error(`Unsupported union member types`);
462
461
  }
463
462
  default:
464
463
  (typeAnnotation.type: 'MixedTypeAnnotation');
@@ -255,7 +255,7 @@ function throwIfArrayElementTypeAnnotationIsUnsupported(
255
255
  PromiseTypeAnnotation: 'Promise',
256
256
  // TODO: Added as a work-around for now until TupleTypeAnnotation are fully supported in both flow and TS
257
257
  // Right now they are partially treated as UnionTypeAnnotation
258
- UnionTypeAnnotation: 'UnionTypeAnnotation',
258
+ // UnionTypeAnnotation: 'UnionTypeAnnotation',
259
259
  };
260
260
  if (type in TypeMap) {
261
261
  throw new UnsupportedArrayElementTypeAnnotationParserError(
@@ -278,7 +278,7 @@ function throwIfArrayElementTypeAnnotationIsUnsupported(
278
278
  PromiseTypeAnnotation: 'Promise',
279
279
  // TODO: Added as a work-around for now until TupleTypeAnnotation are fully supported in both flow and TS
280
280
  // Right now they are partially treated as UnionTypeAnnotation
281
- UnionTypeAnnotation: 'UnionTypeAnnotation',
281
+ // UnionTypeAnnotation: 'UnionTypeAnnotation',
282
282
  };
283
283
 
284
284
  if (type in TypeMap) {
@@ -265,20 +265,6 @@ class UnsupportedEnumDeclarationParserError extends ParserError {
265
265
  }
266
266
  }
267
267
 
268
- /**
269
- * Union parsing errors
270
- */
271
-
272
- class UnsupportedUnionTypeAnnotationParserError extends ParserError {
273
- constructor(nativeModuleName, arrayElementTypeAST, types) {
274
- super(
275
- nativeModuleName,
276
- arrayElementTypeAST,
277
- `Union members must be of the same type, but multiple types were found ${types.join(', ')}'.`,
278
- );
279
- }
280
- }
281
-
282
268
  /**
283
269
  * Module parsing errors
284
270
  */
@@ -357,7 +343,6 @@ module.exports = {
357
343
  UnsupportedFunctionParamTypeAnnotationParserError,
358
344
  UnsupportedFunctionReturnTypeAnnotationParserError,
359
345
  UnsupportedEnumDeclarationParserError,
360
- UnsupportedUnionTypeAnnotationParserError,
361
346
  UnsupportedModuleEventEmitterTypePropertyParserError,
362
347
  UnsupportedModuleEventEmitterPropertyParserError,
363
348
  UnsupportedModulePropertyParserError,
@@ -10,7 +10,6 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- import type {UnionTypeAnnotationMemberType} from '../CodegenSchema';
14
13
  import type {Parser} from './parser';
15
14
 
16
15
  export type ParserType = 'Flow' | 'TypeScript';
@@ -337,26 +336,6 @@ class UnsupportedEnumDeclarationParserError extends ParserError {
337
336
  }
338
337
  }
339
338
 
340
- /**
341
- * Union parsing errors
342
- */
343
-
344
- class UnsupportedUnionTypeAnnotationParserError extends ParserError {
345
- constructor(
346
- nativeModuleName: string,
347
- arrayElementTypeAST: $FlowFixMe,
348
- types: UnionTypeAnnotationMemberType[],
349
- ) {
350
- super(
351
- nativeModuleName,
352
- arrayElementTypeAST,
353
- `Union members must be of the same type, but multiple types were found ${types.join(
354
- ', ',
355
- )}'.`,
356
- );
357
- }
358
- }
359
-
360
339
  /**
361
340
  * Module parsing errors
362
341
  */
@@ -460,7 +439,6 @@ module.exports = {
460
439
  UnsupportedFunctionParamTypeAnnotationParserError,
461
440
  UnsupportedFunctionReturnTypeAnnotationParserError,
462
441
  UnsupportedEnumDeclarationParserError,
463
- UnsupportedUnionTypeAnnotationParserError,
464
442
  UnsupportedModuleEventEmitterTypePropertyParserError,
465
443
  UnsupportedModuleEventEmitterPropertyParserError,
466
444
  UnsupportedModulePropertyParserError,
@@ -107,7 +107,7 @@ function extractArrayElementType(typeAnnotation, name, parser) {
107
107
  };
108
108
  case 'UnionTypeAnnotation':
109
109
  return {
110
- type: 'StringLiteralUnionTypeAnnotation',
110
+ type: 'UnionTypeAnnotation',
111
111
  types: typeAnnotation.types.map(option => ({
112
112
  type: 'StringLiteralTypeAnnotation',
113
113
  value: parser.getLiteralValue(option),