@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.
- package/lib/CodegenSchema.d.ts +4 -13
- package/lib/CodegenSchema.js.flow +5 -13
- package/lib/generators/Utils.js +49 -0
- package/lib/generators/Utils.js.flow +58 -0
- package/lib/generators/components/CppHelpers.js +35 -2
- package/lib/generators/components/CppHelpers.js.flow +35 -2
- package/lib/generators/components/GenerateEventEmitterCpp.js +11 -2
- package/lib/generators/components/GenerateEventEmitterCpp.js.flow +11 -2
- package/lib/generators/components/GenerateEventEmitterH.js +24 -9
- package/lib/generators/components/GenerateEventEmitterH.js.flow +24 -10
- package/lib/generators/modules/GenerateModuleH.js +24 -17
- package/lib/generators/modules/GenerateModuleH.js.flow +24 -17
- package/lib/generators/modules/GenerateModuleJavaSpec.js +48 -34
- package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +48 -34
- package/lib/generators/modules/GenerateModuleJniCpp.js +29 -28
- package/lib/generators/modules/GenerateModuleJniCpp.js.flow +29 -28
- package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js +36 -22
- package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js.flow +39 -24
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js +4 -3
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js.flow +4 -3
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js +7 -4
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js.flow +7 -4
- package/lib/generators/modules/GenerateModuleObjCpp/serializeEventEmitter.js +19 -6
- package/lib/generators/modules/GenerateModuleObjCpp/serializeEventEmitter.js.flow +19 -6
- package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js +22 -23
- package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js.flow +22 -23
- package/lib/parsers/error-utils.js +1 -1
- package/lib/parsers/error-utils.js.flow +1 -1
- package/lib/parsers/errors.js +0 -15
- package/lib/parsers/errors.js.flow +0 -22
- package/lib/parsers/flow/components/events.js +1 -1
- package/lib/parsers/flow/components/events.js.flow +1 -1
- package/lib/parsers/flow/modules/index.js +12 -1
- package/lib/parsers/flow/modules/index.js.flow +12 -1
- package/lib/parsers/flow/parser.js +0 -3
- package/lib/parsers/flow/parser.js.flow +2 -8
- package/lib/parsers/parser.js.flow +2 -10
- package/lib/parsers/parserMock.js +0 -3
- package/lib/parsers/parserMock.js.flow +2 -8
- package/lib/parsers/parsers-primitives.js +41 -47
- package/lib/parsers/parsers-primitives.js.flow +45 -56
- package/lib/parsers/typescript/components/events.js +1 -1
- package/lib/parsers/typescript/components/events.js.flow +1 -1
- package/lib/parsers/typescript/modules/index.js +12 -1
- package/lib/parsers/typescript/modules/index.js.flow +12 -1
- package/lib/parsers/typescript/parser.js +0 -3
- package/lib/parsers/typescript/parser.js.flow +2 -8
- package/package.json +1 -1
package/lib/CodegenSchema.d.ts
CHANGED
|
@@ -318,10 +318,8 @@ export interface StringLiteralTypeAnnotation {
|
|
|
318
318
|
readonly value: string;
|
|
319
319
|
}
|
|
320
320
|
|
|
321
|
-
export
|
|
322
|
-
|
|
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
|
|
401
|
-
|
|
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 =
|
|
65
|
-
|
|
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 =
|
|
390
|
-
|
|
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;
|
package/lib/generators/Utils.js
CHANGED
|
@@ -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 '
|
|
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 '
|
|
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 '
|
|
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 '
|
|
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 '
|
|
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 '
|
|
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 {
|
|
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 '
|
|
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(
|
|
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 === '
|
|
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 '
|
|
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 {
|
|
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 '
|
|
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(
|
|
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 === '
|
|
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 '
|
|
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),
|
|
@@ -12,7 +12,12 @@
|
|
|
12
12
|
|
|
13
13
|
const {unwrapNullable} = require('../../parsers/parsers-commons');
|
|
14
14
|
const {wrapOptional} = require('../TypeUtils/Cxx');
|
|
15
|
-
const {
|
|
15
|
+
const {
|
|
16
|
+
getEnumName,
|
|
17
|
+
parseValidUnionType,
|
|
18
|
+
toPascalCase,
|
|
19
|
+
toSafeCppString,
|
|
20
|
+
} = require('../Utils');
|
|
16
21
|
const {
|
|
17
22
|
createAliasResolver,
|
|
18
23
|
getModules,
|
|
@@ -60,8 +65,6 @@ function serializeArg(moduleName, arg, index, resolveAlias, enumMap) {
|
|
|
60
65
|
return wrap(val => `${val}.asString(rt)`);
|
|
61
66
|
case 'StringLiteralTypeAnnotation':
|
|
62
67
|
return wrap(val => `${val}.asString(rt)`);
|
|
63
|
-
case 'StringLiteralUnionTypeAnnotation':
|
|
64
|
-
return wrap(val => `${val}.asString(rt)`);
|
|
65
68
|
case 'BooleanTypeAnnotation':
|
|
66
69
|
return wrap(val => `${val}.asBool()`);
|
|
67
70
|
case 'BooleanLiteralTypeAnnotation':
|
|
@@ -94,17 +97,19 @@ function serializeArg(moduleName, arg, index, resolveAlias, enumMap) {
|
|
|
94
97
|
case 'GenericObjectTypeAnnotation':
|
|
95
98
|
return wrap(val => `${val}.asObject(rt)`);
|
|
96
99
|
case 'UnionTypeAnnotation':
|
|
97
|
-
|
|
98
|
-
|
|
100
|
+
const validUnionType = parseValidUnionType(realTypeAnnotation);
|
|
101
|
+
switch (validUnionType) {
|
|
102
|
+
case 'boolean':
|
|
103
|
+
return wrap(val => `${val}.asBool()`);
|
|
104
|
+
case 'number':
|
|
99
105
|
return wrap(val => `${val}.asNumber()`);
|
|
100
|
-
case '
|
|
106
|
+
case 'object':
|
|
101
107
|
return wrap(val => `${val}.asObject(rt)`);
|
|
102
|
-
case '
|
|
108
|
+
case 'string':
|
|
103
109
|
return wrap(val => `${val}.asString(rt)`);
|
|
104
110
|
default:
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
);
|
|
111
|
+
validUnionType;
|
|
112
|
+
throw new Error(`Unsupported union member type`);
|
|
108
113
|
}
|
|
109
114
|
case 'ObjectTypeAnnotation':
|
|
110
115
|
return wrap(val => `${val}.asObject(rt)`);
|
|
@@ -203,8 +208,6 @@ function translatePrimitiveJSTypeToCpp(
|
|
|
203
208
|
return wrapOptional('jsi::String', isRequired);
|
|
204
209
|
case 'StringLiteralTypeAnnotation':
|
|
205
210
|
return wrapOptional('jsi::String', isRequired);
|
|
206
|
-
case 'StringLiteralUnionTypeAnnotation':
|
|
207
|
-
return wrapOptional('jsi::String', isRequired);
|
|
208
211
|
case 'NumberTypeAnnotation':
|
|
209
212
|
return wrapOptional('double', isRequired);
|
|
210
213
|
case 'NumberLiteralTypeAnnotation':
|
|
@@ -231,15 +234,19 @@ function translatePrimitiveJSTypeToCpp(
|
|
|
231
234
|
case 'GenericObjectTypeAnnotation':
|
|
232
235
|
return wrapOptional('jsi::Object', isRequired);
|
|
233
236
|
case 'UnionTypeAnnotation':
|
|
234
|
-
|
|
235
|
-
|
|
237
|
+
const validUnionType = parseValidUnionType(realTypeAnnotation);
|
|
238
|
+
switch (validUnionType) {
|
|
239
|
+
case 'boolean':
|
|
240
|
+
return wrapOptional('bool', isRequired);
|
|
241
|
+
case 'number':
|
|
236
242
|
return wrapOptional('double', isRequired);
|
|
237
|
-
case '
|
|
243
|
+
case 'object':
|
|
238
244
|
return wrapOptional('jsi::Object', isRequired);
|
|
239
|
-
case '
|
|
245
|
+
case 'string':
|
|
240
246
|
return wrapOptional('jsi::String', isRequired);
|
|
241
247
|
default:
|
|
242
|
-
|
|
248
|
+
validUnionType;
|
|
249
|
+
throw new Error(`Unsupported union member type`);
|
|
243
250
|
}
|
|
244
251
|
case 'ObjectTypeAnnotation':
|
|
245
252
|
return wrapOptional('jsi::Object', isRequired);
|