Skip to content

Commit 4a15f90

Browse files
kyawthura-ggfacebook-github-bot
authored andcommitted
Convert the emitCommonTypes implementation from a switch based implementation to a dictionary based one (#36549)
Summary: Task from #34872 > [Codegen 80] Convert the emitCommonTypes implementation from a switch based implementation to a dictionary based one ## Changelog: [Internal] [Changed] - Convert the emitCommonTypes implementation from a switch based implementation to a dictionary based one Pull Request resolved: #36549 Test Plan: `yarn test react-native-codegen` Reviewed By: NickGerleman Differential Revision: D44244901 Pulled By: rshest fbshipit-source-id: 50712724c72aad3bd1dae3e7c381242c4913a236
1 parent cf43f9c commit 4a15f90

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,7 @@ describe('emitPartial', () => {
12631263
nullable: boolean,
12641264
): $FlowFixMe {
12651265
return emitPartial(
1266+
nullable,
12661267
hasteModuleName,
12671268
typeAnnotation,
12681269
/* types: TypeDeclarationMap */
@@ -1278,7 +1279,6 @@ describe('emitPartial', () => {
12781279
},
12791280
/* cxxOnly: boolean */
12801281
false,
1281-
nullable,
12821282
parser,
12831283
);
12841284
}
@@ -1494,4 +1494,15 @@ describe('emitCommonTypes', () => {
14941494
expect(result).toEqual(expected);
14951495
});
14961496
});
1497+
1498+
describe('when typeAnnotation is invalid', () => {
1499+
const typeAnnotation = {
1500+
id: {
1501+
name: 'InvalidName',
1502+
},
1503+
};
1504+
it('returns null', () => {
1505+
expect(emitCommonTypesForUnitTest(typeAnnotation, false)).toBeNull();
1506+
});
1507+
});
14971508
});

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

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,14 @@ function Visitor(infoMap: {isComponent: boolean, isModule: boolean}): {
476476
}
477477

478478
function emitPartial(
479+
nullable: boolean,
479480
hasteModuleName: string,
480481
typeAnnotation: $FlowFixMe,
481482
types: TypeDeclarationMap,
482483
aliasMap: {...NativeModuleAliasMap},
483484
enumMap: {...NativeModuleEnumMap},
484485
tryParse: ParserErrorCapturer,
485486
cxxOnly: boolean,
486-
nullable: boolean,
487487
parser: Parser,
488488
): Nullable<NativeModuleTypeAnnotation> {
489489
throwIfPartialWithMoreParameter(typeAnnotation);
@@ -524,41 +524,33 @@ function emitCommonTypes(
524524
const genericTypeAnnotationName =
525525
parser.nameForGenericTypeAnnotation(typeAnnotation);
526526

527-
switch (genericTypeAnnotationName) {
528-
case 'Stringish': {
529-
return emitStringish(nullable);
530-
}
531-
case 'Int32': {
532-
return emitInt32(nullable);
533-
}
534-
case 'Double': {
535-
return emitDouble(nullable);
536-
}
537-
case 'Float': {
538-
return emitFloat(nullable);
539-
}
540-
case 'UnsafeObject':
541-
case 'Object': {
542-
return emitGenericObject(nullable);
543-
}
544-
case '$Partial':
545-
case 'Partial': {
546-
return emitPartial(
547-
hasteModuleName,
548-
typeAnnotation,
549-
types,
550-
aliasMap,
551-
enumMap,
552-
tryParse,
553-
cxxOnly,
554-
nullable,
555-
parser,
556-
);
557-
}
558-
default: {
559-
return null;
560-
}
527+
const typeMap = {
528+
Stringish: emitStringish,
529+
Int32: emitInt32,
530+
Double: emitDouble,
531+
Float: emitFloat,
532+
UnsafeObject: emitGenericObject,
533+
Object: emitGenericObject,
534+
$Partial: emitPartial,
535+
Partial: emitPartial,
536+
};
537+
538+
const emitter = typeMap[genericTypeAnnotationName];
539+
if (!emitter) {
540+
return null;
561541
}
542+
543+
return emitter(
544+
nullable,
545+
hasteModuleName,
546+
typeAnnotation,
547+
types,
548+
aliasMap,
549+
enumMap,
550+
tryParse,
551+
cxxOnly,
552+
parser,
553+
);
562554
}
563555

564556
module.exports = {

0 commit comments

Comments
 (0)