Skip to content

Commit 1e5e7db

Browse files
Modifying the Parser logic to handle Unions of different types and remove the special handling of StringLiteralUnionType (facebook#54596)
Summary: - Getting rid of emitStringLiteralUnion logic in parser - Modifying the emitUnion logic in parser Changelog: [Internal] Differential Revision: D87412493
1 parent bb8041a commit 1e5e7db

File tree

3 files changed

+46
-75
lines changed

3 files changed

+46
-75
lines changed

packages/react-native-codegen/src/parsers/parser.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import type {
1818
NativeModuleEnumMember,
1919
NativeModuleEnumMemberType,
2020
NativeModuleParamTypeAnnotation,
21+
NativeModuleUnionTypeAnnotationMemberType,
2122
Nullable,
2223
PropTypeAnnotation,
2324
SchemaType,
24-
UnionTypeAnnotationMemberType,
2525
} from '../CodegenSchema';
2626
import type {ParserType} from './errors';
2727
import type {
@@ -146,15 +146,7 @@ export interface Parser {
146146
*/
147147
remapUnionTypeAnnotationMemberNames(
148148
types: $FlowFixMe,
149-
): UnionTypeAnnotationMemberType[];
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[];
158150
/**
159151
* Given the name of a file, it returns a Schema.
160152
* @parameter filename: the name of the file.

packages/react-native-codegen/src/parsers/parserMock.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import type {
1818
NativeModuleEnumMember,
1919
NativeModuleEnumMemberType,
2020
NativeModuleParamTypeAnnotation,
21+
NativeModuleUnionTypeAnnotationMemberType,
2122
Nullable,
2223
PropTypeAnnotation,
2324
SchemaType,
24-
UnionTypeAnnotationMemberType,
2525
} from '../CodegenSchema';
2626
import type {ParserType} from './errors';
2727
import type {
@@ -105,13 +105,7 @@ export class MockedParser implements Parser {
105105

106106
remapUnionTypeAnnotationMemberNames(
107107
membersTypes: $FlowFixMe[],
108-
): UnionTypeAnnotationMemberType[] {
109-
return [];
110-
}
111-
112-
getStringLiteralUnionTypeAnnotationStringLiterals(
113-
membersTypes: $FlowFixMe[],
114-
): string[] {
108+
): NativeModuleUnionTypeAnnotationMemberType[] {
115109
return [];
116110
}
117111

packages/react-native-codegen/src/parsers/parsers-primitives.js

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ import type {
3030
NativeModuleTypeAliasTypeAnnotation,
3131
NativeModuleTypeAnnotation,
3232
NativeModuleUnionTypeAnnotation,
33+
NativeModuleUnionTypeAnnotationMemberType,
3334
Nullable,
3435
NumberLiteralTypeAnnotation,
3536
ObjectTypeAnnotation,
3637
ReservedTypeAnnotation,
3738
StringLiteralTypeAnnotation,
38-
StringLiteralUnionTypeAnnotation,
3939
StringTypeAnnotation,
4040
VoidTypeAnnotation,
4141
} from '../CodegenSchema';
@@ -51,11 +51,7 @@ const {
5151
throwIfPartialNotAnnotatingTypeParameter,
5252
throwIfPartialWithMoreParameter,
5353
} = require('./error-utils');
54-
const {
55-
ParserError,
56-
UnsupportedTypeAnnotationParserError,
57-
UnsupportedUnionTypeAnnotationParserError,
58-
} = require('./errors');
54+
const {ParserError, UnsupportedTypeAnnotationParserError} = require('./errors');
5955
const {
6056
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
6157
translateFunctionTypeAnnotation,
@@ -420,61 +416,50 @@ function emitUnion(
420416
nullable: boolean,
421417
hasteModuleName: string,
422418
typeAnnotation: $FlowFixMe,
419+
types: TypeDeclarationMap,
420+
aliasMap: {...NativeModuleAliasMap},
421+
enumMap: {...NativeModuleEnumMap},
422+
tryParse: ParserErrorCapturer,
423+
cxxOnly: boolean,
424+
translateTypeAnnotation: $FlowFixMe,
423425
parser: Parser,
424-
): Nullable<
425-
NativeModuleUnionTypeAnnotation | StringLiteralUnionTypeAnnotation,
426-
> {
427-
// Get all the literals by type
428-
// Verify they are all the same
429-
// If string, persist as StringLiteralUnionType
430-
// If number, persist as NumberTypeAnnotation (TODO: Number literal)
431-
432-
const unionTypes = parser.remapUnionTypeAnnotationMemberNames(
433-
typeAnnotation.types,
426+
): Nullable<NativeModuleUnionTypeAnnotation> {
427+
const unparsedMemberTypes: $ReadOnlyArray<$FlowFixMe> =
428+
(typeAnnotation.types: $ReadOnlyArray<$FlowFixMe>);
429+
430+
const memberTypes = unparsedMemberTypes.map(
431+
(memberType: $FlowFixMe): NativeModuleUnionTypeAnnotationMemberType => {
432+
switch (memberType.type) {
433+
case 'NumberLiteralTypeAnnotation':
434+
case 'StringLiteralTypeAnnotation':
435+
case 'BooleanLiteralTypeAnnotation':
436+
case 'TSLiteralType':
437+
case 'GenericTypeAnnotation':
438+
case 'TSTypeLiteral':
439+
case 'ObjectTypeAnnotation':
440+
return translateTypeAnnotation(
441+
hasteModuleName,
442+
memberType,
443+
types,
444+
aliasMap,
445+
enumMap,
446+
tryParse,
447+
cxxOnly,
448+
parser,
449+
);
450+
default:
451+
throw new UnsupportedTypeAnnotationParserError(
452+
hasteModuleName,
453+
memberType,
454+
parser.language(),
455+
);
456+
}
457+
},
434458
);
435459

436-
// Only support unionTypes of the same kind
437-
if (unionTypes.length > 1) {
438-
throw new UnsupportedUnionTypeAnnotationParserError(
439-
hasteModuleName,
440-
typeAnnotation,
441-
unionTypes,
442-
);
443-
}
444-
445-
if (unionTypes[0] === 'StringTypeAnnotation') {
446-
// Reprocess as a string literal union
447-
return emitStringLiteralUnion(
448-
nullable,
449-
hasteModuleName,
450-
typeAnnotation,
451-
parser,
452-
);
453-
}
454-
455460
return wrapNullable(nullable, {
456461
type: 'UnionTypeAnnotation',
457-
memberType: unionTypes[0],
458-
});
459-
}
460-
461-
function emitStringLiteralUnion(
462-
nullable: boolean,
463-
hasteModuleName: string,
464-
typeAnnotation: $FlowFixMe,
465-
parser: Parser,
466-
): Nullable<StringLiteralUnionTypeAnnotation> {
467-
const stringLiterals =
468-
parser.getStringLiteralUnionTypeAnnotationStringLiterals(
469-
typeAnnotation.types,
470-
);
471-
472-
return wrapNullable(nullable, {
473-
type: 'StringLiteralUnionTypeAnnotation',
474-
types: stringLiterals.map(stringLiteral => ({
475-
type: 'StringLiteralTypeAnnotation',
476-
value: stringLiteral,
477-
})),
462+
types: memberTypes,
478463
});
479464
}
480465

@@ -752,7 +737,7 @@ function emitUnionProp(
752737
name,
753738
optional,
754739
typeAnnotation: {
755-
type: 'StringLiteralUnionTypeAnnotation',
740+
type: 'UnionTypeAnnotation',
756741
types: typeAnnotation.types.map(option => ({
757742
type: 'StringLiteralTypeAnnotation',
758743
value: parser.getLiteralValue(option),

0 commit comments

Comments
 (0)