From 4a15f90208cc614d1ce39ea991b88e00acc76e2e Mon Sep 17 00:00:00 2001 From: Kyaw Thura <44088165+kyawthura-gg@users.noreply.github.com> Date: Wed, 22 Mar 2023 13:51:39 -0700 Subject: [PATCH] Convert the emitCommonTypes implementation from a switch based implementation to a dictionary based one (#36549) Summary: Task from https://github.com/facebook/react-native/issues/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: https://github.com/facebook/react-native/pull/36549 Test Plan: `yarn test react-native-codegen` Reviewed By: NickGerleman Differential Revision: D44244901 Pulled By: rshest fbshipit-source-id: 50712724c72aad3bd1dae3e7c381242c4913a236 --- .../__tests__/parsers-primitives-test.js | 13 +++- .../src/parsers/parsers-primitives.js | 62 ++++++++----------- 2 files changed, 39 insertions(+), 36 deletions(-) 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 2574005f71e6b9..b04e46aa96ae2b 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 ef2a3b605cd314..879376bda3d174 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 = {