@react-native/codegen 0.84.0-nightly-20251203-a5e6addc6 → 0.84.0-nightly-20251205-95cc1e767

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 (50) hide show
  1. package/lib/CodegenSchema.d.ts +4 -13
  2. package/lib/CodegenSchema.js.flow +5 -13
  3. package/lib/cli/combine/combine-js-to-schema.js +6 -8
  4. package/lib/cli/combine/combine-js-to-schema.js.flow +6 -8
  5. package/lib/generators/Utils.js +49 -0
  6. package/lib/generators/Utils.js.flow +58 -0
  7. package/lib/generators/components/CppHelpers.js +35 -2
  8. package/lib/generators/components/CppHelpers.js.flow +35 -2
  9. package/lib/generators/components/GenerateEventEmitterCpp.js +11 -2
  10. package/lib/generators/components/GenerateEventEmitterCpp.js.flow +11 -2
  11. package/lib/generators/components/GenerateEventEmitterH.js +24 -9
  12. package/lib/generators/components/GenerateEventEmitterH.js.flow +24 -10
  13. package/lib/generators/modules/GenerateModuleH.js +24 -17
  14. package/lib/generators/modules/GenerateModuleH.js.flow +24 -17
  15. package/lib/generators/modules/GenerateModuleJavaSpec.js +48 -34
  16. package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +48 -34
  17. package/lib/generators/modules/GenerateModuleJniCpp.js +29 -28
  18. package/lib/generators/modules/GenerateModuleJniCpp.js.flow +29 -28
  19. package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js +36 -22
  20. package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js.flow +39 -24
  21. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js +4 -3
  22. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js.flow +4 -3
  23. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js +7 -4
  24. package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js.flow +7 -4
  25. package/lib/generators/modules/GenerateModuleObjCpp/serializeEventEmitter.js +19 -6
  26. package/lib/generators/modules/GenerateModuleObjCpp/serializeEventEmitter.js.flow +19 -6
  27. package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js +22 -23
  28. package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js.flow +22 -23
  29. package/lib/parsers/error-utils.js +1 -1
  30. package/lib/parsers/error-utils.js.flow +1 -1
  31. package/lib/parsers/errors.js +0 -15
  32. package/lib/parsers/errors.js.flow +0 -22
  33. package/lib/parsers/flow/components/events.js +1 -1
  34. package/lib/parsers/flow/components/events.js.flow +1 -1
  35. package/lib/parsers/flow/modules/index.js +12 -1
  36. package/lib/parsers/flow/modules/index.js.flow +12 -1
  37. package/lib/parsers/flow/parser.js +0 -3
  38. package/lib/parsers/flow/parser.js.flow +2 -8
  39. package/lib/parsers/parser.js.flow +2 -10
  40. package/lib/parsers/parserMock.js +0 -3
  41. package/lib/parsers/parserMock.js.flow +2 -8
  42. package/lib/parsers/parsers-primitives.js +41 -47
  43. package/lib/parsers/parsers-primitives.js.flow +45 -56
  44. package/lib/parsers/typescript/components/events.js +1 -1
  45. package/lib/parsers/typescript/components/events.js.flow +1 -1
  46. package/lib/parsers/typescript/modules/index.js +12 -1
  47. package/lib/parsers/typescript/modules/index.js.flow +12 -1
  48. package/lib/parsers/typescript/parser.js +0 -3
  49. package/lib/parsers/typescript/parser.js.flow +2 -8
  50. package/package.json +2 -2
@@ -318,10 +318,8 @@ export interface StringLiteralTypeAnnotation {
318
318
  readonly value: string;
319
319
  }
320
320
 
321
- export interface StringLiteralUnionTypeAnnotation {
322
- readonly type: 'StringLiteralUnionTypeAnnotation';
323
- readonly types: StringLiteralTypeAnnotation[];
324
- }
321
+ export type StringLiteralUnionTypeAnnotation =
322
+ UnionTypeAnnotation<StringLiteralTypeAnnotation>;
325
323
 
326
324
  export interface NativeModuleNumberTypeAnnotation {
327
325
  readonly type: 'NumberTypeAnnotation';
@@ -383,11 +381,6 @@ export interface NativeModulePromiseTypeAnnotation {
383
381
  readonly elementType: Nullable<NativeModuleBaseTypeAnnotation> | VoidTypeAnnotation;
384
382
  }
385
383
 
386
- export type UnionTypeAnnotationMemberType =
387
- | 'NumberTypeAnnotation'
388
- | 'ObjectTypeAnnotation'
389
- | 'StringTypeAnnotation';
390
-
391
384
  export type NativeModuleUnionTypeAnnotationMemberType =
392
385
  | NativeModuleObjectTypeAnnotation
393
386
  | StringLiteralTypeAnnotation
@@ -397,10 +390,8 @@ export type NativeModuleUnionTypeAnnotationMemberType =
397
390
  | StringTypeAnnotation
398
391
  | NumberTypeAnnotation;
399
392
 
400
- export interface NativeModuleUnionTypeAnnotation {
401
- readonly type: 'UnionTypeAnnotation';
402
- readonly memberType: UnionTypeAnnotationMemberType;
403
- }
393
+ export type NativeModuleUnionTypeAnnotation =
394
+ UnionTypeAnnotation<NativeModuleUnionTypeAnnotationMemberType>;
404
395
 
405
396
  export interface NativeModuleMixedTypeAnnotation {
406
397
  readonly type: 'MixedTypeAnnotation';
@@ -61,10 +61,8 @@ export type BooleanLiteralTypeAnnotation = $ReadOnly<{
61
61
  value: boolean,
62
62
  }>;
63
63
 
64
- export type StringLiteralUnionTypeAnnotation = $ReadOnly<{
65
- type: 'StringLiteralUnionTypeAnnotation',
66
- types: $ReadOnlyArray<StringLiteralTypeAnnotation>,
67
- }>;
64
+ export type StringLiteralUnionTypeAnnotation =
65
+ UnionTypeAnnotation<StringLiteralTypeAnnotation>;
68
66
 
69
67
  export type VoidTypeAnnotation = $ReadOnly<{
70
68
  type: 'VoidTypeAnnotation',
@@ -372,11 +370,6 @@ export type NativeModulePromiseTypeAnnotation = $ReadOnly<{
372
370
  elementType: VoidTypeAnnotation | Nullable<NativeModuleBaseTypeAnnotation>,
373
371
  }>;
374
372
 
375
- export type UnionTypeAnnotationMemberType =
376
- | 'NumberTypeAnnotation'
377
- | 'ObjectTypeAnnotation'
378
- | 'StringTypeAnnotation';
379
-
380
373
  export type NativeModuleUnionTypeAnnotationMemberType =
381
374
  | NativeModuleObjectTypeAnnotation
382
375
  | StringLiteralTypeAnnotation
@@ -386,10 +379,8 @@ export type NativeModuleUnionTypeAnnotationMemberType =
386
379
  | StringTypeAnnotation
387
380
  | NumberTypeAnnotation;
388
381
 
389
- export type NativeModuleUnionTypeAnnotation = $ReadOnly<{
390
- type: 'UnionTypeAnnotation',
391
- memberType: UnionTypeAnnotationMemberType,
392
- }>;
382
+ export type NativeModuleUnionTypeAnnotation =
383
+ UnionTypeAnnotation<NativeModuleUnionTypeAnnotationMemberType>;
393
384
 
394
385
  export type NativeModuleMixedTypeAnnotation = $ReadOnly<{
395
386
  type: 'MixedTypeAnnotation',
@@ -473,6 +464,7 @@ export type CompleteTypeAnnotation =
473
464
  | UnsafeAnyTypeAnnotation
474
465
  | ArrayTypeAnnotation<CompleteTypeAnnotation>
475
466
  | ObjectTypeAnnotation<CompleteTypeAnnotation>
467
+ | NativeModuleUnionTypeAnnotationMemberType
476
468
  // Components
477
469
  | CommandTypeAnnotation
478
470
  | CompleteReservedTypeAnnotation;
@@ -14,8 +14,8 @@ const {FlowParser} = require('../../parsers/flow/parser');
14
14
  const {TypeScriptParser} = require('../../parsers/typescript/parser');
15
15
  const {filterJSFile} = require('./combine-utils');
16
16
  const fs = require('fs');
17
- const glob = require('glob');
18
17
  const path = require('path');
18
+ const {globSync} = require('tinyglobby');
19
19
  const flowParser = new FlowParser();
20
20
  const typescriptParser = new TypeScriptParser();
21
21
  function combineSchemas(files, libraryName) {
@@ -55,13 +55,11 @@ function expandDirectoriesIntoFiles(fileList, platform, exclude) {
55
55
  if (!fs.lstatSync(file).isDirectory()) {
56
56
  return [file];
57
57
  }
58
- const filePattern = path.sep === '\\' ? file.replace(/\\/g, '/') : file;
59
- return glob.sync(`${filePattern}/**/*{,.fb}.{js,ts,tsx}`, {
60
- nodir: true,
61
- // TODO: This will remove the need of slash substitution above for Windows,
62
- // but it requires glob@v9+; with the package currenlty relying on
63
- // glob@7.1.1; and flow-typed repo not having definitions for glob@9+.
64
- // windowsPathsNoEscape: true,
58
+ return globSync('**/*{,.fb}.{js,ts,tsx}', {
59
+ expandDirectories: false,
60
+ onlyFiles: true,
61
+ absolute: true,
62
+ cwd: file,
65
63
  });
66
64
  })
67
65
  .filter(element => filterJSFile(element, platform, exclude));
@@ -15,8 +15,8 @@ const {FlowParser} = require('../../parsers/flow/parser');
15
15
  const {TypeScriptParser} = require('../../parsers/typescript/parser');
16
16
  const {filterJSFile} = require('./combine-utils');
17
17
  const fs = require('fs');
18
- const glob = require('glob');
19
18
  const path = require('path');
19
+ const {globSync} = require('tinyglobby');
20
20
 
21
21
  const flowParser = new FlowParser();
22
22
  const typescriptParser = new TypeScriptParser();
@@ -66,13 +66,11 @@ function expandDirectoriesIntoFiles(
66
66
  if (!fs.lstatSync(file).isDirectory()) {
67
67
  return [file];
68
68
  }
69
- const filePattern = path.sep === '\\' ? file.replace(/\\/g, '/') : file;
70
- return glob.sync(`${filePattern}/**/*{,.fb}.{js,ts,tsx}`, {
71
- nodir: true,
72
- // TODO: This will remove the need of slash substitution above for Windows,
73
- // but it requires glob@v9+; with the package currenlty relying on
74
- // glob@7.1.1; and flow-typed repo not having definitions for glob@9+.
75
- // windowsPathsNoEscape: true,
69
+ return globSync('**/*{,.fb}.{js,ts,tsx}', {
70
+ expandDirectories: false,
71
+ onlyFiles: true,
72
+ absolute: true,
73
+ cwd: file,
76
74
  });
77
75
  })
78
76
  .filter(element => filterJSFile(element, platform, exclude));
@@ -38,10 +38,59 @@ function getEnumName(moduleName, origEnumName) {
38
38
  const uppercasedPropName = toSafeCppString(origEnumName);
39
39
  return `${moduleName}${uppercasedPropName}`;
40
40
  }
41
+ const NumberTypes = ['NumberTypeAnnotation', 'NumberLiteralTypeAnnotation'];
42
+ const StringTypes = ['StringTypeAnnotation', 'StringLiteralTypeAnnotation'];
43
+ const ObjectTypes = ['ObjectTypeAnnotation'];
44
+ const BooleanTypes = ['BooleanTypeAnnotation', 'BooleanLiteralTypeAnnotation'];
45
+ const ValidUnionTypes = [
46
+ ...NumberTypes,
47
+ ...ObjectTypes,
48
+ ...StringTypes,
49
+ ...BooleanTypes,
50
+ ];
51
+ class HeterogeneousUnionError extends Error {
52
+ constructor() {
53
+ super(`Non-homogenous union member types`);
54
+ }
55
+ }
56
+ function parseValidUnionType(annotation) {
57
+ const isUnionOfType = types => {
58
+ return annotation.types.every(memberTypeAnnotation =>
59
+ types.includes(memberTypeAnnotation.type),
60
+ );
61
+ };
62
+ if (isUnionOfType(BooleanTypes)) {
63
+ return 'boolean';
64
+ }
65
+ if (isUnionOfType(NumberTypes)) {
66
+ return 'number';
67
+ }
68
+ if (isUnionOfType(ObjectTypes)) {
69
+ return 'object';
70
+ }
71
+ if (isUnionOfType(StringTypes)) {
72
+ return 'string';
73
+ }
74
+ const invalidTypes = annotation.types.filter(member => {
75
+ return !ValidUnionTypes.includes(member.type);
76
+ });
77
+
78
+ // Check if union members are all supported but not homogeneous
79
+ // (e.g., mix of number and boolean)
80
+ if (invalidTypes.length === 0) {
81
+ throw new HeterogeneousUnionError();
82
+ } else {
83
+ throw new Error(
84
+ `Unsupported union member types: ${invalidTypes.join(', ')}"`,
85
+ );
86
+ }
87
+ }
41
88
  module.exports = {
42
89
  capitalize,
43
90
  indent,
91
+ parseValidUnionType,
44
92
  toPascalCase,
45
93
  toSafeCppString,
46
94
  getEnumName,
95
+ HeterogeneousUnionError,
47
96
  };
@@ -10,6 +10,8 @@
10
10
 
11
11
  'use strict';
12
12
 
13
+ import type {NativeModuleUnionTypeAnnotation} from '../CodegenSchema';
14
+
13
15
  function capitalize(string: string): string {
14
16
  return string.charAt(0).toUpperCase() + string.slice(1);
15
17
  }
@@ -44,10 +46,66 @@ function getEnumName(moduleName: string, origEnumName: string): string {
44
46
  return `${moduleName}${uppercasedPropName}`;
45
47
  }
46
48
 
49
+ type ValidUnionType = 'boolean' | 'number' | 'object' | 'string';
50
+ const NumberTypes = ['NumberTypeAnnotation', 'NumberLiteralTypeAnnotation'];
51
+ const StringTypes = ['StringTypeAnnotation', 'StringLiteralTypeAnnotation'];
52
+ const ObjectTypes = ['ObjectTypeAnnotation'];
53
+ const BooleanTypes = ['BooleanTypeAnnotation', 'BooleanLiteralTypeAnnotation'];
54
+ const ValidUnionTypes = [
55
+ ...NumberTypes,
56
+ ...ObjectTypes,
57
+ ...StringTypes,
58
+ ...BooleanTypes,
59
+ ];
60
+
61
+ class HeterogeneousUnionError extends Error {
62
+ constructor() {
63
+ super(`Non-homogenous union member types`);
64
+ }
65
+ }
66
+
67
+ function parseValidUnionType(
68
+ annotation: NativeModuleUnionTypeAnnotation,
69
+ ): ValidUnionType {
70
+ const isUnionOfType = (types: $ReadOnlyArray<string>): boolean => {
71
+ return annotation.types.every(memberTypeAnnotation =>
72
+ types.includes(memberTypeAnnotation.type),
73
+ );
74
+ };
75
+ if (isUnionOfType(BooleanTypes)) {
76
+ return 'boolean';
77
+ }
78
+ if (isUnionOfType(NumberTypes)) {
79
+ return 'number';
80
+ }
81
+ if (isUnionOfType(ObjectTypes)) {
82
+ return 'object';
83
+ }
84
+ if (isUnionOfType(StringTypes)) {
85
+ return 'string';
86
+ }
87
+
88
+ const invalidTypes = annotation.types.filter(member => {
89
+ return !ValidUnionTypes.includes(member.type);
90
+ });
91
+
92
+ // Check if union members are all supported but not homogeneous
93
+ // (e.g., mix of number and boolean)
94
+ if (invalidTypes.length === 0) {
95
+ throw new HeterogeneousUnionError();
96
+ } else {
97
+ throw new Error(
98
+ `Unsupported union member types: ${invalidTypes.join(', ')}"`,
99
+ );
100
+ }
101
+ }
102
+
47
103
  module.exports = {
48
104
  capitalize,
49
105
  indent,
106
+ parseValidUnionType,
50
107
  toPascalCase,
51
108
  toSafeCppString,
52
109
  getEnumName,
110
+ HeterogeneousUnionError,
53
111
  };
@@ -10,7 +10,7 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- const {getEnumName, toSafeCppString} = require('../Utils');
13
+ const {getEnumName, parseValidUnionType, toSafeCppString} = require('../Utils');
14
14
  function toIntEnumValueName(propName, value) {
15
15
  return `${toSafeCppString(propName)}${value}`;
16
16
  }
@@ -42,7 +42,40 @@ function getCppArrayTypeForAnnotation(typeElement, structParts) {
42
42
  case 'Int32TypeAnnotation':
43
43
  case 'MixedTypeAnnotation':
44
44
  return `std::vector<${getCppTypeForAnnotation(typeElement.type)}>`;
45
- case 'StringLiteralUnionTypeAnnotation':
45
+ case 'UnionTypeAnnotation':
46
+ const validUnionType = parseValidUnionType(typeElement);
47
+ switch (validUnionType) {
48
+ case 'boolean':
49
+ return `std::vector<${getCppTypeForAnnotation('BooleanTypeAnnotation')}>`;
50
+ case 'number':
51
+ // Have type-upgraded Number to Double in this case to match the Int32TypeAnnotation, FloatTypeAnnotation & DoubleTypeAnnotation
52
+ return `std::vector<${getCppTypeForAnnotation('DoubleTypeAnnotation')}>`;
53
+ case 'object':
54
+ if (!structParts) {
55
+ throw new Error(
56
+ `Trying to generate the event emitter for an Array of ${typeElement.type} without informations to generate the generic type`,
57
+ );
58
+ }
59
+ return `std::vector<${generateEventStructName(structParts)}>`;
60
+ case 'string':
61
+ if (
62
+ typeElement.types.every(
63
+ ({type}) => type === 'StringLiteralTypeAnnotation',
64
+ )
65
+ ) {
66
+ if (!structParts) {
67
+ throw new Error(
68
+ `Trying to generate the event emitter for an Array of ${typeElement.type} without informations to generate the generic type`,
69
+ );
70
+ }
71
+ return `std::vector<${generateEventStructName(structParts)}>`;
72
+ }
73
+ // Unions of strings and string literals are treated as just strings
74
+ return `std::vector<${getCppTypeForAnnotation('StringTypeAnnotation')}>`;
75
+ default:
76
+ validUnionType;
77
+ throw new Error(`Unsupported union member type`);
78
+ }
46
79
  case 'ObjectTypeAnnotation':
47
80
  if (!structParts) {
48
81
  throw new Error(
@@ -15,7 +15,7 @@ import type {
15
15
  PropTypeAnnotation,
16
16
  } from '../../CodegenSchema';
17
17
 
18
- const {getEnumName, toSafeCppString} = require('../Utils');
18
+ const {getEnumName, parseValidUnionType, toSafeCppString} = require('../Utils');
19
19
 
20
20
  function toIntEnumValueName(propName: string, value: number): string {
21
21
  return `${toSafeCppString(propName)}${value}`;
@@ -61,7 +61,40 @@ function getCppArrayTypeForAnnotation(
61
61
  case 'Int32TypeAnnotation':
62
62
  case 'MixedTypeAnnotation':
63
63
  return `std::vector<${getCppTypeForAnnotation(typeElement.type)}>`;
64
- case 'StringLiteralUnionTypeAnnotation':
64
+ case 'UnionTypeAnnotation':
65
+ const validUnionType = parseValidUnionType(typeElement);
66
+ switch (validUnionType) {
67
+ case 'boolean':
68
+ return `std::vector<${getCppTypeForAnnotation('BooleanTypeAnnotation')}>`;
69
+ case 'number':
70
+ // Have type-upgraded Number to Double in this case to match the Int32TypeAnnotation, FloatTypeAnnotation & DoubleTypeAnnotation
71
+ return `std::vector<${getCppTypeForAnnotation('DoubleTypeAnnotation')}>`;
72
+ case 'object':
73
+ if (!structParts) {
74
+ throw new Error(
75
+ `Trying to generate the event emitter for an Array of ${typeElement.type} without informations to generate the generic type`,
76
+ );
77
+ }
78
+ return `std::vector<${generateEventStructName(structParts)}>`;
79
+ case 'string':
80
+ if (
81
+ typeElement.types.every(
82
+ ({type}) => type === 'StringLiteralTypeAnnotation',
83
+ )
84
+ ) {
85
+ if (!structParts) {
86
+ throw new Error(
87
+ `Trying to generate the event emitter for an Array of ${typeElement.type} without informations to generate the generic type`,
88
+ );
89
+ }
90
+ return `std::vector<${generateEventStructName(structParts)}>`;
91
+ }
92
+ // Unions of strings and string literals are treated as just strings
93
+ return `std::vector<${getCppTypeForAnnotation('StringTypeAnnotation')}>`;
94
+ default:
95
+ (validUnionType: empty);
96
+ throw new Error(`Unsupported union member type`);
97
+ }
65
98
  case 'ObjectTypeAnnotation':
66
99
  if (!structParts) {
67
100
  throw new Error(
@@ -11,6 +11,7 @@
11
11
  'use strict';
12
12
 
13
13
  const {indent} = require('../Utils');
14
+ const {parseValidUnionType} = require('../Utils');
14
15
  const {IncludeTemplate, generateEventStructName} = require('./CppHelpers');
15
16
 
16
17
  // File path -> contents
@@ -146,7 +147,11 @@ function handleArrayElementType(
146
147
  loopLocalVariable,
147
148
  val => `jsi::valueFromDynamic(runtime, ${val})`,
148
149
  );
149
- case 'StringLiteralUnionTypeAnnotation':
150
+ case 'UnionTypeAnnotation':
151
+ const validUnionType = parseValidUnionType(elementType);
152
+ if (validUnionType !== 'string') {
153
+ throw new Error('Invalid since it is a union of non strings');
154
+ }
150
155
  return setValueAtIndex(
151
156
  propertyName,
152
157
  indexVariable,
@@ -242,7 +247,11 @@ function generateSetters(
242
247
  usingEvent,
243
248
  prop => `jsi::valueFromDynamic(runtime, ${prop})`,
244
249
  );
245
- case 'StringLiteralUnionTypeAnnotation':
250
+ case 'UnionTypeAnnotation':
251
+ const validUnionType = parseValidUnionType(typeAnnotation);
252
+ if (validUnionType !== 'string') {
253
+ throw new Error('Invalid since it is a union of non strings');
254
+ }
246
255
  return generateSetter(
247
256
  parentPropertyName,
248
257
  eventProperty.name,
@@ -19,6 +19,7 @@ import type {
19
19
  } from '../../CodegenSchema';
20
20
 
21
21
  const {indent} = require('../Utils');
22
+ const {parseValidUnionType} = require('../Utils');
22
23
  const {IncludeTemplate, generateEventStructName} = require('./CppHelpers');
23
24
 
24
25
  // File path -> contents
@@ -207,7 +208,11 @@ function handleArrayElementType(
207
208
  loopLocalVariable,
208
209
  val => `jsi::valueFromDynamic(runtime, ${val})`,
209
210
  );
210
- case 'StringLiteralUnionTypeAnnotation':
211
+ case 'UnionTypeAnnotation':
212
+ const validUnionType = parseValidUnionType(elementType);
213
+ if (validUnionType !== 'string') {
214
+ throw new Error('Invalid since it is a union of non strings');
215
+ }
211
216
  return setValueAtIndex(
212
217
  propertyName,
213
218
  indexVariable,
@@ -320,7 +325,11 @@ function generateSetters(
320
325
  usingEvent,
321
326
  prop => `jsi::valueFromDynamic(runtime, ${prop})`,
322
327
  );
323
- case 'StringLiteralUnionTypeAnnotation':
328
+ case 'UnionTypeAnnotation':
329
+ const validUnionType = parseValidUnionType(typeAnnotation);
330
+ if (validUnionType !== 'string') {
331
+ throw new Error('Invalid since it is a union of non strings');
332
+ }
324
333
  return generateSetter(
325
334
  parentPropertyName,
326
335
  eventProperty.name,
@@ -10,7 +10,7 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- const {indent, toSafeCppString} = require('../Utils');
13
+ const {indent, parseValidUnionType, toSafeCppString} = require('../Utils');
14
14
  const {
15
15
  generateEventStructName,
16
16
  getCppArrayTypeForAnnotation,
@@ -67,16 +67,21 @@ static char const *toString(const ${enumName} value) {
67
67
  }
68
68
  `.trim();
69
69
  function getNativeTypeFromAnnotation(componentName, eventProperty, nameParts) {
70
- const {type} = eventProperty.typeAnnotation;
71
- switch (type) {
70
+ const {typeAnnotation} = eventProperty;
71
+ switch (typeAnnotation.type) {
72
72
  case 'BooleanTypeAnnotation':
73
73
  case 'StringTypeAnnotation':
74
74
  case 'Int32TypeAnnotation':
75
75
  case 'DoubleTypeAnnotation':
76
76
  case 'FloatTypeAnnotation':
77
77
  case 'MixedTypeAnnotation':
78
- return getCppTypeForAnnotation(type);
79
- case 'StringLiteralUnionTypeAnnotation':
78
+ return getCppTypeForAnnotation(typeAnnotation.type);
79
+ case 'UnionTypeAnnotation':
80
+ const validUnionType = parseValidUnionType(typeAnnotation);
81
+ if (validUnionType !== 'string') {
82
+ throw new Error('Invalid since it is a union of non strings');
83
+ }
84
+ return generateEventStructName([...nameParts, eventProperty.name]);
80
85
  case 'ObjectTypeAnnotation':
81
86
  return generateEventStructName([...nameParts, eventProperty.name]);
82
87
  case 'ArrayTypeAnnotation':
@@ -91,8 +96,10 @@ function getNativeTypeFromAnnotation(componentName, eventProperty, nameParts) {
91
96
  eventProperty.name,
92
97
  ]);
93
98
  default:
94
- type;
95
- throw new Error(`Received invalid event property type ${type}`);
99
+ typeAnnotation.type;
100
+ throw new Error(
101
+ `Received invalid event property type ${typeAnnotation.type}`,
102
+ );
96
103
  }
97
104
  }
98
105
  function generateEnum(structs, options, nameParts) {
@@ -129,7 +136,11 @@ function handleGenerateStructForArray(
129
136
  nameParts.concat([name]),
130
137
  nullthrows(elementType.properties),
131
138
  );
132
- } else if (elementType.type === 'StringLiteralUnionTypeAnnotation') {
139
+ } else if (elementType.type === 'UnionTypeAnnotation') {
140
+ const validUnionType = parseValidUnionType(elementType);
141
+ if (validUnionType !== 'string') {
142
+ throw new Error('Invalid since it is a union of non strings');
143
+ }
133
144
  generateEnum(
134
145
  structs,
135
146
  elementType.types.map(literal => literal.value),
@@ -180,7 +191,11 @@ function generateStruct(structs, componentName, nameParts, properties) {
180
191
  nullthrows(typeAnnotation.properties),
181
192
  );
182
193
  return;
183
- case 'StringLiteralUnionTypeAnnotation':
194
+ case 'UnionTypeAnnotation':
195
+ const validUnionType = parseValidUnionType(typeAnnotation);
196
+ if (validUnionType !== 'string') {
197
+ throw new Error('Invalid since it is a union of non strings');
198
+ }
184
199
  generateEnum(
185
200
  structs,
186
201
  typeAnnotation.types.map(literal => literal.value),
@@ -18,7 +18,7 @@ import type {
18
18
  SchemaType,
19
19
  } from '../../CodegenSchema';
20
20
 
21
- const {indent, toSafeCppString} = require('../Utils');
21
+ const {indent, parseValidUnionType, toSafeCppString} = require('../Utils');
22
22
  const {
23
23
  generateEventStructName,
24
24
  getCppArrayTypeForAnnotation,
@@ -118,17 +118,21 @@ function getNativeTypeFromAnnotation(
118
118
  eventProperty: NamedShape<EventTypeAnnotation>,
119
119
  nameParts: $ReadOnlyArray<string>,
120
120
  ): string {
121
- const {type} = eventProperty.typeAnnotation;
122
-
123
- switch (type) {
121
+ const {typeAnnotation} = eventProperty;
122
+ switch (typeAnnotation.type) {
124
123
  case 'BooleanTypeAnnotation':
125
124
  case 'StringTypeAnnotation':
126
125
  case 'Int32TypeAnnotation':
127
126
  case 'DoubleTypeAnnotation':
128
127
  case 'FloatTypeAnnotation':
129
128
  case 'MixedTypeAnnotation':
130
- return getCppTypeForAnnotation(type);
131
- case 'StringLiteralUnionTypeAnnotation':
129
+ return getCppTypeForAnnotation(typeAnnotation.type);
130
+ case 'UnionTypeAnnotation':
131
+ const validUnionType = parseValidUnionType(typeAnnotation);
132
+ if (validUnionType !== 'string') {
133
+ throw new Error('Invalid since it is a union of non strings');
134
+ }
135
+ return generateEventStructName([...nameParts, eventProperty.name]);
132
136
  case 'ObjectTypeAnnotation':
133
137
  return generateEventStructName([...nameParts, eventProperty.name]);
134
138
  case 'ArrayTypeAnnotation':
@@ -143,8 +147,10 @@ function getNativeTypeFromAnnotation(
143
147
  eventProperty.name,
144
148
  ]);
145
149
  default:
146
- (type: empty);
147
- throw new Error(`Received invalid event property type ${type}`);
150
+ (typeAnnotation.type: empty);
151
+ throw new Error(
152
+ `Received invalid event property type ${typeAnnotation.type}`,
153
+ );
148
154
  }
149
155
  }
150
156
  function generateEnum(
@@ -188,7 +194,11 @@ function handleGenerateStructForArray(
188
194
  nameParts.concat([name]),
189
195
  nullthrows(elementType.properties),
190
196
  );
191
- } else if (elementType.type === 'StringLiteralUnionTypeAnnotation') {
197
+ } else if (elementType.type === 'UnionTypeAnnotation') {
198
+ const validUnionType = parseValidUnionType(elementType);
199
+ if (validUnionType !== 'string') {
200
+ throw new Error('Invalid since it is a union of non strings');
201
+ }
192
202
  generateEnum(
193
203
  structs,
194
204
  elementType.types.map(literal => literal.value),
@@ -251,7 +261,11 @@ function generateStruct(
251
261
  nullthrows(typeAnnotation.properties),
252
262
  );
253
263
  return;
254
- case 'StringLiteralUnionTypeAnnotation':
264
+ case 'UnionTypeAnnotation':
265
+ const validUnionType = parseValidUnionType(typeAnnotation);
266
+ if (validUnionType !== 'string') {
267
+ throw new Error('Invalid since it is a union of non strings');
268
+ }
255
269
  generateEnum(
256
270
  structs,
257
271
  typeAnnotation.types.map(literal => literal.value),