From c403cd4f112b529d292dee2b0bfc35cd96908502 Mon Sep 17 00:00:00 2001 From: mohitcharkha Date: Fri, 14 Oct 2022 03:09:51 -0700 Subject: [PATCH] Extracted content of the case FunctionTypeAnnotation into emitFunction in parsers-primitives file (#34950) Summary: This PR is part of https://github.com/facebook/react-native/issues/34872 This PR extracts the content of the case `FunctionTypeAnnotation` into a single `emitFunction` function inside the parsers-primitives.js file and uses it in both Flow and TypeScript parsers ## Changelog [Internal] [Changed] - Extract the content of the case `FunctionTypeAnnotation` into a single `emitFunction` function Pull Request resolved: https://github.com/facebook/react-native/pull/34950 Test Plan: Run yarn jest react-native-codegen and ensure CI is green Screenshot 2022-10-12 at 11 18 54 AM Reviewed By: christophpurrer Differential Revision: D40296823 Pulled By: cipolleschi fbshipit-source-id: 3cac407d260481bd6ae7c3e46642e4c16bba3376 --- .../src/parsers/flow/modules/index.js | 8 ++++---- .../src/parsers/parsers-primitives.js | 12 ++++++++++-- .../src/parsers/typescript/modules/index.js | 9 +++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index dd7c32d60f9..a514f7e608f 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -41,6 +41,7 @@ const { const { emitBoolean, emitDouble, + emitFunction, emitNumber, emitInt32, emitObject, @@ -372,8 +373,7 @@ function translateTypeAnnotation( }); } case 'FunctionTypeAnnotation': { - return wrapNullable( - nullable, + const translateFunctionTypeAnnotationValue: NativeModuleFunctionTypeAnnotation = translateFunctionTypeAnnotation( hasteModuleName, typeAnnotation, @@ -381,8 +381,8 @@ function translateTypeAnnotation( aliasMap, tryParse, cxxOnly, - ), - ); + ); + return emitFunction(nullable, translateFunctionTypeAnnotationValue); } case 'UnionTypeAnnotation': { if (cxxOnly) { diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index cdf0f1a10ab..c64f4049bdd 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -14,6 +14,7 @@ import type { Nullable, NativeModuleAliasMap, NativeModuleBaseTypeAnnotation, + NativeModuleFunctionTypeAnnotation, NativeModuleTypeAliasTypeAnnotation, NativeModuleNumberTypeAnnotation, BooleanTypeAnnotation, @@ -23,8 +24,8 @@ import type { ReservedTypeAnnotation, ObjectTypeAnnotation, NativeModulePromiseTypeAnnotation, - VoidTypeAnnotation, StringTypeAnnotation, + VoidTypeAnnotation, } from '../CodegenSchema'; import type {ParserType} from './errors'; import type {TypeAliasResolutionStatus} from './utils'; @@ -78,6 +79,12 @@ function emitStringish(nullable: boolean): Nullable { type: 'StringTypeAnnotation', }); } +function emitFunction( + nullable: boolean, + translateFunctionTypeAnnotationValue: NativeModuleFunctionTypeAnnotation, +): Nullable { + return wrapNullable(nullable, translateFunctionTypeAnnotationValue); +} function typeAliasResolution( typeAliasResolutionStatus: TypeAliasResolutionStatus, @@ -161,12 +168,13 @@ function emitObject( module.exports = { emitBoolean, emitDouble, + emitFunction, emitInt32, emitNumber, emitObject, emitPromise, emitRootTag, - emitVoid, emitStringish, + emitVoid, typeAliasResolution, }; diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index b092d7bd5df..f7c1c15e40d 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -41,6 +41,7 @@ const { const { emitBoolean, emitDouble, + emitFunction, emitNumber, emitInt32, emitObject, @@ -387,8 +388,7 @@ function translateTypeAnnotation( }); } case 'TSFunctionType': { - return wrapNullable( - nullable, + const translateFunctionTypeAnnotationValue: NativeModuleFunctionTypeAnnotation = translateFunctionTypeAnnotation( hasteModuleName, typeAnnotation, @@ -396,8 +396,9 @@ function translateTypeAnnotation( aliasMap, tryParse, cxxOnly, - ), - ); + ); + + return emitFunction(nullable, translateFunctionTypeAnnotationValue); } case 'TSUnionType': { if (cxxOnly) {