diff --git a/packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js b/packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js index 2574005f71e..b04e46aa96a 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js @@ -1263,6 +1263,7 @@ describe('emitPartial', () => { nullable: boolean, ): $FlowFixMe { return emitPartial( + nullable, hasteModuleName, typeAnnotation, /* types: TypeDeclarationMap */ @@ -1278,7 +1279,6 @@ describe('emitPartial', () => { }, /* cxxOnly: boolean */ false, - nullable, parser, ); } @@ -1494,4 +1494,15 @@ describe('emitCommonTypes', () => { expect(result).toEqual(expected); }); }); + + describe('when typeAnnotation is invalid', () => { + const typeAnnotation = { + id: { + name: 'InvalidName', + }, + }; + it('returns null', () => { + expect(emitCommonTypesForUnitTest(typeAnnotation, false)).toBeNull(); + }); + }); }); diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index ef2a3b605cd..879376bda3d 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -476,6 +476,7 @@ function Visitor(infoMap: {isComponent: boolean, isModule: boolean}): { } function emitPartial( + nullable: boolean, hasteModuleName: string, typeAnnotation: $FlowFixMe, types: TypeDeclarationMap, @@ -483,7 +484,6 @@ function emitPartial( enumMap: {...NativeModuleEnumMap}, tryParse: ParserErrorCapturer, cxxOnly: boolean, - nullable: boolean, parser: Parser, ): Nullable { throwIfPartialWithMoreParameter(typeAnnotation); @@ -524,41 +524,33 @@ function emitCommonTypes( const genericTypeAnnotationName = parser.nameForGenericTypeAnnotation(typeAnnotation); - switch (genericTypeAnnotationName) { - case 'Stringish': { - return emitStringish(nullable); - } - case 'Int32': { - return emitInt32(nullable); - } - case 'Double': { - return emitDouble(nullable); - } - case 'Float': { - return emitFloat(nullable); - } - case 'UnsafeObject': - case 'Object': { - return emitGenericObject(nullable); - } - case '$Partial': - case 'Partial': { - return emitPartial( - hasteModuleName, - typeAnnotation, - types, - aliasMap, - enumMap, - tryParse, - cxxOnly, - nullable, - parser, - ); - } - default: { - return null; - } + const typeMap = { + Stringish: emitStringish, + Int32: emitInt32, + Double: emitDouble, + Float: emitFloat, + UnsafeObject: emitGenericObject, + Object: emitGenericObject, + $Partial: emitPartial, + Partial: emitPartial, + }; + + const emitter = typeMap[genericTypeAnnotationName]; + if (!emitter) { + return null; } + + return emitter( + nullable, + hasteModuleName, + typeAnnotation, + types, + aliasMap, + enumMap, + tryParse, + cxxOnly, + parser, + ); } module.exports = {