@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.
- package/lib/CodegenSchema.d.ts +4 -13
- package/lib/CodegenSchema.js.flow +5 -13
- package/lib/cli/combine/combine-js-to-schema.js +6 -8
- package/lib/cli/combine/combine-js-to-schema.js.flow +6 -8
- 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 +2 -2
|
@@ -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 '
|
|
263
|
-
|
|
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
|
-
|
|
376
|
-
|
|
372
|
+
const validUnionType = parseValidUnionType(typeAnnotation);
|
|
373
|
+
switch (validUnionType) {
|
|
374
|
+
case 'boolean':
|
|
377
375
|
return wrapOptional('NSNumber *', isRequired);
|
|
378
|
-
case '
|
|
376
|
+
case 'number':
|
|
377
|
+
return wrapOptional('NSNumber *', isRequired);
|
|
378
|
+
case 'object':
|
|
379
379
|
return wrapOptional('NSDictionary *', isRequired);
|
|
380
|
-
case '
|
|
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
|
-
|
|
386
|
-
|
|
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
|
-
|
|
452
|
-
|
|
448
|
+
const validUnionType = parseValidUnionType(typeAnnotation);
|
|
449
|
+
switch (validUnionType) {
|
|
450
|
+
case 'boolean':
|
|
451
|
+
return 'BooleanKind';
|
|
452
|
+
case 'number':
|
|
453
453
|
return 'NumberKind';
|
|
454
|
-
case '
|
|
454
|
+
case 'object':
|
|
455
455
|
return 'ObjectKind';
|
|
456
|
-
case '
|
|
456
|
+
case 'string':
|
|
457
457
|
return 'StringKind';
|
|
458
458
|
default:
|
|
459
|
-
|
|
460
|
-
|
|
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) {
|
package/lib/parsers/errors.js
CHANGED
|
@@ -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: '
|
|
110
|
+
type: 'UnionTypeAnnotation',
|
|
111
111
|
types: typeAnnotation.types.map(option => ({
|
|
112
112
|
type: 'StringLiteralTypeAnnotation',
|
|
113
113
|
value: parser.getLiteralValue(option),
|
|
@@ -115,7 +115,7 @@ function extractArrayElementType(
|
|
|
115
115
|
};
|
|
116
116
|
case 'UnionTypeAnnotation':
|
|
117
117
|
return {
|
|
118
|
-
type: '
|
|
118
|
+
type: 'UnionTypeAnnotation',
|
|
119
119
|
types: typeAnnotation.types.map(option => ({
|
|
120
120
|
type: 'StringLiteralTypeAnnotation',
|
|
121
121
|
value: parser.getLiteralValue(option),
|
|
@@ -220,7 +220,18 @@ function translateTypeAnnotation(
|
|
|
220
220
|
);
|
|
221
221
|
}
|
|
222
222
|
case 'UnionTypeAnnotation': {
|
|
223
|
-
return emitUnion(
|
|
223
|
+
return emitUnion(
|
|
224
|
+
nullable,
|
|
225
|
+
hasteModuleName,
|
|
226
|
+
typeAnnotation,
|
|
227
|
+
types,
|
|
228
|
+
aliasMap,
|
|
229
|
+
enumMap,
|
|
230
|
+
tryParse,
|
|
231
|
+
cxxOnly,
|
|
232
|
+
translateTypeAnnotation,
|
|
233
|
+
parser,
|
|
234
|
+
);
|
|
224
235
|
}
|
|
225
236
|
case 'NumberLiteralTypeAnnotation': {
|
|
226
237
|
return emitNumberLiteral(nullable, typeAnnotation.value);
|
|
@@ -245,7 +245,18 @@ function translateTypeAnnotation(
|
|
|
245
245
|
);
|
|
246
246
|
}
|
|
247
247
|
case 'UnionTypeAnnotation': {
|
|
248
|
-
return emitUnion(
|
|
248
|
+
return emitUnion(
|
|
249
|
+
nullable,
|
|
250
|
+
hasteModuleName,
|
|
251
|
+
typeAnnotation,
|
|
252
|
+
types,
|
|
253
|
+
aliasMap,
|
|
254
|
+
enumMap,
|
|
255
|
+
tryParse,
|
|
256
|
+
cxxOnly,
|
|
257
|
+
translateTypeAnnotation,
|
|
258
|
+
parser,
|
|
259
|
+
);
|
|
249
260
|
}
|
|
250
261
|
case 'NumberLiteralTypeAnnotation': {
|
|
251
262
|
return emitNumberLiteral(nullable, typeAnnotation.value);
|
|
@@ -131,9 +131,6 @@ class FlowParser {
|
|
|
131
131
|
};
|
|
132
132
|
return [...new Set(membersTypes.map(remapLiteral))];
|
|
133
133
|
}
|
|
134
|
-
getStringLiteralUnionTypeAnnotationStringLiterals(membersTypes) {
|
|
135
|
-
return membersTypes.map(item => item.value);
|
|
136
|
-
}
|
|
137
134
|
parseFile(filename) {
|
|
138
135
|
const contents = fs.readFileSync(filename, 'utf8');
|
|
139
136
|
return this.parseString(contents, filename);
|
|
@@ -18,10 +18,10 @@ import type {
|
|
|
18
18
|
NativeModuleEnumMember,
|
|
19
19
|
NativeModuleEnumMemberType,
|
|
20
20
|
NativeModuleParamTypeAnnotation,
|
|
21
|
+
NativeModuleUnionTypeAnnotationMemberType,
|
|
21
22
|
Nullable,
|
|
22
23
|
PropTypeAnnotation,
|
|
23
24
|
SchemaType,
|
|
24
|
-
UnionTypeAnnotationMemberType,
|
|
25
25
|
} from '../../CodegenSchema';
|
|
26
26
|
import type {ParserType} from '../errors';
|
|
27
27
|
import type {
|
|
@@ -111,7 +111,7 @@ class FlowParser implements Parser {
|
|
|
111
111
|
|
|
112
112
|
remapUnionTypeAnnotationMemberNames(
|
|
113
113
|
membersTypes: $FlowFixMe[],
|
|
114
|
-
):
|
|
114
|
+
): NativeModuleUnionTypeAnnotationMemberType[] {
|
|
115
115
|
const remapLiteral = (item: $FlowFixMe) => {
|
|
116
116
|
return item.type
|
|
117
117
|
.replace('NumberLiteralTypeAnnotation', 'NumberTypeAnnotation')
|
|
@@ -121,12 +121,6 @@ class FlowParser implements Parser {
|
|
|
121
121
|
return [...new Set(membersTypes.map(remapLiteral))];
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
getStringLiteralUnionTypeAnnotationStringLiterals(
|
|
125
|
-
membersTypes: $FlowFixMe[],
|
|
126
|
-
): string[] {
|
|
127
|
-
return membersTypes.map((item: $FlowFixMe) => item.value);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
124
|
parseFile(filename: string): SchemaType {
|
|
131
125
|
const contents = fs.readFileSync(filename, 'utf8');
|
|
132
126
|
|
|
@@ -18,10 +18,10 @@ import type {
|
|
|
18
18
|
NativeModuleEnumMember,
|
|
19
19
|
NativeModuleEnumMemberType,
|
|
20
20
|
NativeModuleParamTypeAnnotation,
|
|
21
|
+
NativeModuleUnionTypeAnnotationMemberType,
|
|
21
22
|
Nullable,
|
|
22
23
|
PropTypeAnnotation,
|
|
23
24
|
SchemaType,
|
|
24
|
-
UnionTypeAnnotationMemberType,
|
|
25
25
|
} from '../CodegenSchema';
|
|
26
26
|
import type {ParserType} from './errors';
|
|
27
27
|
import type {
|
|
@@ -146,15 +146,7 @@ export interface Parser {
|
|
|
146
146
|
*/
|
|
147
147
|
remapUnionTypeAnnotationMemberNames(
|
|
148
148
|
types: $FlowFixMe,
|
|
149
|
-
):
|
|
150
|
-
/**
|
|
151
|
-
* Given a union annotation members types, it returns an array of string literals.
|
|
152
|
-
* @parameter membersTypes: union annotation members types
|
|
153
|
-
* @returns: an array of string literals.
|
|
154
|
-
*/
|
|
155
|
-
getStringLiteralUnionTypeAnnotationStringLiterals(
|
|
156
|
-
types: $FlowFixMe,
|
|
157
|
-
): string[];
|
|
149
|
+
): NativeModuleUnionTypeAnnotationMemberType[];
|
|
158
150
|
/**
|
|
159
151
|
* Given the name of a file, it returns a Schema.
|
|
160
152
|
* @parameter filename: the name of the file.
|
|
@@ -112,9 +112,6 @@ export class MockedParser {
|
|
|
112
112
|
remapUnionTypeAnnotationMemberNames(membersTypes) {
|
|
113
113
|
return [];
|
|
114
114
|
}
|
|
115
|
-
getStringLiteralUnionTypeAnnotationStringLiterals(membersTypes) {
|
|
116
|
-
return [];
|
|
117
|
-
}
|
|
118
115
|
parseFile(filename) {
|
|
119
116
|
/* $FlowFixMe[incompatible-type] Natural Inference rollout. See
|
|
120
117
|
* https://fburl.com/workplace/6291gfvu */
|
|
@@ -18,10 +18,10 @@ import type {
|
|
|
18
18
|
NativeModuleEnumMember,
|
|
19
19
|
NativeModuleEnumMemberType,
|
|
20
20
|
NativeModuleParamTypeAnnotation,
|
|
21
|
+
NativeModuleUnionTypeAnnotationMemberType,
|
|
21
22
|
Nullable,
|
|
22
23
|
PropTypeAnnotation,
|
|
23
24
|
SchemaType,
|
|
24
|
-
UnionTypeAnnotationMemberType,
|
|
25
25
|
} from '../CodegenSchema';
|
|
26
26
|
import type {ParserType} from './errors';
|
|
27
27
|
import type {
|
|
@@ -105,13 +105,7 @@ export class MockedParser implements Parser {
|
|
|
105
105
|
|
|
106
106
|
remapUnionTypeAnnotationMemberNames(
|
|
107
107
|
membersTypes: $FlowFixMe[],
|
|
108
|
-
):
|
|
109
|
-
return [];
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
getStringLiteralUnionTypeAnnotationStringLiterals(
|
|
113
|
-
membersTypes: $FlowFixMe[],
|
|
114
|
-
): string[] {
|
|
108
|
+
): NativeModuleUnionTypeAnnotationMemberType[] {
|
|
115
109
|
return [];
|
|
116
110
|
}
|
|
117
111
|
|
|
@@ -15,11 +15,7 @@ const {
|
|
|
15
15
|
throwIfPartialNotAnnotatingTypeParameter,
|
|
16
16
|
throwIfPartialWithMoreParameter,
|
|
17
17
|
} = require('./error-utils');
|
|
18
|
-
const {
|
|
19
|
-
ParserError,
|
|
20
|
-
UnsupportedTypeAnnotationParserError,
|
|
21
|
-
UnsupportedUnionTypeAnnotationParserError,
|
|
22
|
-
} = require('./errors');
|
|
18
|
+
const {ParserError, UnsupportedTypeAnnotationParserError} = require('./errors');
|
|
23
19
|
const {
|
|
24
20
|
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
|
|
25
21
|
translateFunctionTypeAnnotation,
|
|
@@ -320,54 +316,51 @@ function emitFloatProp(name, optional) {
|
|
|
320
316
|
},
|
|
321
317
|
};
|
|
322
318
|
}
|
|
323
|
-
function emitUnion(
|
|
324
|
-
// Get all the literals by type
|
|
325
|
-
// Verify they are all the same
|
|
326
|
-
// If string, persist as StringLiteralUnionType
|
|
327
|
-
// If number, persist as NumberTypeAnnotation (TODO: Number literal)
|
|
328
|
-
|
|
329
|
-
const unionTypes = parser.remapUnionTypeAnnotationMemberNames(
|
|
330
|
-
typeAnnotation.types,
|
|
331
|
-
);
|
|
332
|
-
|
|
333
|
-
// Only support unionTypes of the same kind
|
|
334
|
-
if (unionTypes.length > 1) {
|
|
335
|
-
throw new UnsupportedUnionTypeAnnotationParserError(
|
|
336
|
-
hasteModuleName,
|
|
337
|
-
typeAnnotation,
|
|
338
|
-
unionTypes,
|
|
339
|
-
);
|
|
340
|
-
}
|
|
341
|
-
if (unionTypes[0] === 'StringTypeAnnotation') {
|
|
342
|
-
// Reprocess as a string literal union
|
|
343
|
-
return emitStringLiteralUnion(
|
|
344
|
-
nullable,
|
|
345
|
-
hasteModuleName,
|
|
346
|
-
typeAnnotation,
|
|
347
|
-
parser,
|
|
348
|
-
);
|
|
349
|
-
}
|
|
350
|
-
return wrapNullable(nullable, {
|
|
351
|
-
type: 'UnionTypeAnnotation',
|
|
352
|
-
memberType: unionTypes[0],
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
function emitStringLiteralUnion(
|
|
319
|
+
function emitUnion(
|
|
356
320
|
nullable,
|
|
357
321
|
hasteModuleName,
|
|
358
322
|
typeAnnotation,
|
|
323
|
+
types,
|
|
324
|
+
aliasMap,
|
|
325
|
+
enumMap,
|
|
326
|
+
tryParse,
|
|
327
|
+
cxxOnly,
|
|
328
|
+
translateTypeAnnotation,
|
|
359
329
|
parser,
|
|
360
330
|
) {
|
|
361
|
-
const
|
|
362
|
-
|
|
363
|
-
|
|
331
|
+
const unparsedMemberTypes = typeAnnotation.types;
|
|
332
|
+
const memberTypes = unparsedMemberTypes.map(memberType => {
|
|
333
|
+
const memberTypeAnnotation = translateTypeAnnotation(
|
|
334
|
+
hasteModuleName,
|
|
335
|
+
memberType,
|
|
336
|
+
types,
|
|
337
|
+
aliasMap,
|
|
338
|
+
enumMap,
|
|
339
|
+
tryParse,
|
|
340
|
+
cxxOnly,
|
|
341
|
+
parser,
|
|
364
342
|
);
|
|
343
|
+
switch (memberTypeAnnotation.type) {
|
|
344
|
+
case 'StringTypeAnnotation':
|
|
345
|
+
case 'NumberTypeAnnotation':
|
|
346
|
+
case 'BooleanTypeAnnotation':
|
|
347
|
+
case 'NumberLiteralTypeAnnotation':
|
|
348
|
+
case 'StringLiteralTypeAnnotation':
|
|
349
|
+
case 'BooleanLiteralTypeAnnotation':
|
|
350
|
+
case 'ObjectTypeAnnotation':
|
|
351
|
+
case 'TypeAliasTypeAnnotation':
|
|
352
|
+
return memberTypeAnnotation;
|
|
353
|
+
default:
|
|
354
|
+
throw new UnsupportedTypeAnnotationParserError(
|
|
355
|
+
hasteModuleName,
|
|
356
|
+
memberType,
|
|
357
|
+
parser.language(),
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
});
|
|
365
361
|
return wrapNullable(nullable, {
|
|
366
|
-
type: '
|
|
367
|
-
types:
|
|
368
|
-
type: 'StringLiteralTypeAnnotation',
|
|
369
|
-
value: stringLiteral,
|
|
370
|
-
})),
|
|
362
|
+
type: 'UnionTypeAnnotation',
|
|
363
|
+
types: memberTypes,
|
|
371
364
|
});
|
|
372
365
|
}
|
|
373
366
|
function translateArrayTypeAnnotation(
|
|
@@ -542,6 +535,7 @@ function emitCommonTypes(
|
|
|
542
535
|
VoidTypeAnnotation: emitVoid,
|
|
543
536
|
StringTypeAnnotation: emitString,
|
|
544
537
|
MixedTypeAnnotation: cxxOnly ? emitMixed : emitGenericObject,
|
|
538
|
+
UnsafeMixed: cxxOnly ? emitMixed : emitGenericObject,
|
|
545
539
|
};
|
|
546
540
|
const typeAnnotationName = parser.convertKeywordToTypeAnnotation(
|
|
547
541
|
typeAnnotation.type,
|
|
@@ -608,7 +602,7 @@ function emitUnionProp(name, optional, parser, typeAnnotation) {
|
|
|
608
602
|
name,
|
|
609
603
|
optional,
|
|
610
604
|
typeAnnotation: {
|
|
611
|
-
type: '
|
|
605
|
+
type: 'UnionTypeAnnotation',
|
|
612
606
|
types: typeAnnotation.types.map(option => ({
|
|
613
607
|
type: 'StringLiteralTypeAnnotation',
|
|
614
608
|
value: parser.getLiteralValue(option),
|
|
@@ -31,12 +31,12 @@ import type {
|
|
|
31
31
|
NativeModuleTypeAliasTypeAnnotation,
|
|
32
32
|
NativeModuleTypeAnnotation,
|
|
33
33
|
NativeModuleUnionTypeAnnotation,
|
|
34
|
+
NativeModuleUnionTypeAnnotationMemberType,
|
|
34
35
|
Nullable,
|
|
35
36
|
NumberLiteralTypeAnnotation,
|
|
36
37
|
ObjectTypeAnnotation,
|
|
37
38
|
ReservedTypeAnnotation,
|
|
38
39
|
StringLiteralTypeAnnotation,
|
|
39
|
-
StringLiteralUnionTypeAnnotation,
|
|
40
40
|
StringTypeAnnotation,
|
|
41
41
|
VoidTypeAnnotation,
|
|
42
42
|
} from '../CodegenSchema';
|
|
@@ -52,11 +52,7 @@ const {
|
|
|
52
52
|
throwIfPartialNotAnnotatingTypeParameter,
|
|
53
53
|
throwIfPartialWithMoreParameter,
|
|
54
54
|
} = require('./error-utils');
|
|
55
|
-
const {
|
|
56
|
-
ParserError,
|
|
57
|
-
UnsupportedTypeAnnotationParserError,
|
|
58
|
-
UnsupportedUnionTypeAnnotationParserError,
|
|
59
|
-
} = require('./errors');
|
|
55
|
+
const {ParserError, UnsupportedTypeAnnotationParserError} = require('./errors');
|
|
60
56
|
const {
|
|
61
57
|
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
|
|
62
58
|
translateFunctionTypeAnnotation,
|
|
@@ -431,61 +427,53 @@ function emitUnion(
|
|
|
431
427
|
nullable: boolean,
|
|
432
428
|
hasteModuleName: string,
|
|
433
429
|
typeAnnotation: $FlowFixMe,
|
|
430
|
+
types: TypeDeclarationMap,
|
|
431
|
+
aliasMap: {...NativeModuleAliasMap},
|
|
432
|
+
enumMap: {...NativeModuleEnumMap},
|
|
433
|
+
tryParse: ParserErrorCapturer,
|
|
434
|
+
cxxOnly: boolean,
|
|
435
|
+
translateTypeAnnotation: $FlowFixMe,
|
|
434
436
|
parser: Parser,
|
|
435
|
-
): Nullable<
|
|
436
|
-
|
|
437
|
-
>
|
|
438
|
-
// Get all the literals by type
|
|
439
|
-
// Verify they are all the same
|
|
440
|
-
// If string, persist as StringLiteralUnionType
|
|
441
|
-
// If number, persist as NumberTypeAnnotation (TODO: Number literal)
|
|
442
|
-
|
|
443
|
-
const unionTypes = parser.remapUnionTypeAnnotationMemberNames(
|
|
444
|
-
typeAnnotation.types,
|
|
445
|
-
);
|
|
437
|
+
): Nullable<NativeModuleUnionTypeAnnotation> {
|
|
438
|
+
const unparsedMemberTypes: $ReadOnlyArray<$FlowFixMe> =
|
|
439
|
+
(typeAnnotation.types: $ReadOnlyArray<$FlowFixMe>);
|
|
446
440
|
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
441
|
+
const memberTypes = unparsedMemberTypes.map(
|
|
442
|
+
(memberType: $FlowFixMe): NativeModuleUnionTypeAnnotationMemberType => {
|
|
443
|
+
const memberTypeAnnotation = translateTypeAnnotation(
|
|
444
|
+
hasteModuleName,
|
|
445
|
+
memberType,
|
|
446
|
+
types,
|
|
447
|
+
aliasMap,
|
|
448
|
+
enumMap,
|
|
449
|
+
tryParse,
|
|
450
|
+
cxxOnly,
|
|
451
|
+
parser,
|
|
452
|
+
);
|
|
455
453
|
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
454
|
+
switch (memberTypeAnnotation.type) {
|
|
455
|
+
case 'StringTypeAnnotation':
|
|
456
|
+
case 'NumberTypeAnnotation':
|
|
457
|
+
case 'BooleanTypeAnnotation':
|
|
458
|
+
case 'NumberLiteralTypeAnnotation':
|
|
459
|
+
case 'StringLiteralTypeAnnotation':
|
|
460
|
+
case 'BooleanLiteralTypeAnnotation':
|
|
461
|
+
case 'ObjectTypeAnnotation':
|
|
462
|
+
case 'TypeAliasTypeAnnotation':
|
|
463
|
+
return memberTypeAnnotation;
|
|
464
|
+
default:
|
|
465
|
+
throw new UnsupportedTypeAnnotationParserError(
|
|
466
|
+
hasteModuleName,
|
|
467
|
+
memberType,
|
|
468
|
+
parser.language(),
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
},
|
|
472
|
+
);
|
|
465
473
|
|
|
466
474
|
return wrapNullable(nullable, {
|
|
467
475
|
type: 'UnionTypeAnnotation',
|
|
468
|
-
|
|
469
|
-
});
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
function emitStringLiteralUnion(
|
|
473
|
-
nullable: boolean,
|
|
474
|
-
hasteModuleName: string,
|
|
475
|
-
typeAnnotation: $FlowFixMe,
|
|
476
|
-
parser: Parser,
|
|
477
|
-
): Nullable<StringLiteralUnionTypeAnnotation> {
|
|
478
|
-
const stringLiterals =
|
|
479
|
-
parser.getStringLiteralUnionTypeAnnotationStringLiterals(
|
|
480
|
-
typeAnnotation.types,
|
|
481
|
-
);
|
|
482
|
-
|
|
483
|
-
return wrapNullable(nullable, {
|
|
484
|
-
type: 'StringLiteralUnionTypeAnnotation',
|
|
485
|
-
types: stringLiterals.map(stringLiteral => ({
|
|
486
|
-
type: 'StringLiteralTypeAnnotation',
|
|
487
|
-
value: stringLiteral,
|
|
488
|
-
})),
|
|
476
|
+
types: memberTypes,
|
|
489
477
|
});
|
|
490
478
|
}
|
|
491
479
|
|
|
@@ -675,6 +663,7 @@ function emitCommonTypes(
|
|
|
675
663
|
VoidTypeAnnotation: emitVoid,
|
|
676
664
|
StringTypeAnnotation: emitString,
|
|
677
665
|
MixedTypeAnnotation: cxxOnly ? emitMixed : emitGenericObject,
|
|
666
|
+
UnsafeMixed: cxxOnly ? emitMixed : emitGenericObject,
|
|
678
667
|
};
|
|
679
668
|
|
|
680
669
|
const typeAnnotationName = parser.convertKeywordToTypeAnnotation(
|
|
@@ -763,7 +752,7 @@ function emitUnionProp(
|
|
|
763
752
|
name,
|
|
764
753
|
optional,
|
|
765
754
|
typeAnnotation: {
|
|
766
|
-
type: '
|
|
755
|
+
type: 'UnionTypeAnnotation',
|
|
767
756
|
types: typeAnnotation.types.map(option => ({
|
|
768
757
|
type: 'StringLiteralTypeAnnotation',
|
|
769
758
|
value: parser.getLiteralValue(option),
|
|
@@ -115,7 +115,7 @@ function extractArrayElementType(typeAnnotation, name, parser) {
|
|
|
115
115
|
};
|
|
116
116
|
case 'TSUnionType':
|
|
117
117
|
return {
|
|
118
|
-
type: '
|
|
118
|
+
type: 'UnionTypeAnnotation',
|
|
119
119
|
types: typeAnnotation.types.map(option => ({
|
|
120
120
|
type: 'StringLiteralTypeAnnotation',
|
|
121
121
|
value: parser.getLiteralValue(option),
|
|
@@ -127,7 +127,7 @@ function extractArrayElementType(
|
|
|
127
127
|
};
|
|
128
128
|
case 'TSUnionType':
|
|
129
129
|
return {
|
|
130
|
-
type: '
|
|
130
|
+
type: 'UnionTypeAnnotation',
|
|
131
131
|
types: typeAnnotation.types.map(option => ({
|
|
132
132
|
type: 'StringLiteralTypeAnnotation',
|
|
133
133
|
value: parser.getLiteralValue(option),
|
|
@@ -380,7 +380,18 @@ function translateTypeAnnotation(
|
|
|
380
380
|
);
|
|
381
381
|
}
|
|
382
382
|
case 'TSUnionType': {
|
|
383
|
-
return emitUnion(
|
|
383
|
+
return emitUnion(
|
|
384
|
+
nullable,
|
|
385
|
+
hasteModuleName,
|
|
386
|
+
typeAnnotation,
|
|
387
|
+
types,
|
|
388
|
+
aliasMap,
|
|
389
|
+
enumMap,
|
|
390
|
+
tryParse,
|
|
391
|
+
cxxOnly,
|
|
392
|
+
translateTypeAnnotation,
|
|
393
|
+
parser,
|
|
394
|
+
);
|
|
384
395
|
}
|
|
385
396
|
case 'TSLiteralType': {
|
|
386
397
|
const literal = typeAnnotation.literal;
|