From 32474367c2c3ca665665a4573c8b165cef0ed578 Mon Sep 17 00:00:00 2001 From: harshsiriah Date: Wed, 19 Oct 2022 01:38:28 -0700 Subject: [PATCH] Extracted UnsupportedFunctionReturnTypeAnnotationParserError to throwIfUnsupportedFunctionReturnTypeAnnotationParserError (#34965) Summary: This PR is part of https://github.com/facebook/react-native/issues/34872 This PR extracts `UnsupportedFunctionReturnTypeAnnotationParserError` exception to a separate function inside an `error-utils.js` file ## Changelog [Internal] [Changed] - Extract `UnsupportedFunctionReturnTypeAnnotationParserError` to a seperate function inside `error-utils.js` Pull Request resolved: https://github.com/facebook/react-native/pull/34965 Test Plan: ```sh yarn jest react-native-codegen ``` Added unit case in `error-utils-test.js` file Screenshot 2022-10-13 at 11 46 54 AM Reviewed By: dmytrorykun Differential Revision: D40338048 Pulled By: cipolleschi fbshipit-source-id: baa41e0e96c9e17a35f316433c8d80c9bf88d334 --- .../src/parsers/__tests__/error-utils-test.js | 59 +++++++++++++++++++ .../src/parsers/error-utils.js | 20 +++++++ .../src/parsers/flow/modules/index.js | 18 +++--- .../src/parsers/typescript/modules/index.js | 18 +++--- 4 files changed, 97 insertions(+), 18 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js index 900ce7f4f5d..717897c350c 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js @@ -18,6 +18,7 @@ const { throwIfUnusedModuleInterfaceParserError, throwIfWrongNumberOfCallExpressionArgs, throwIfIncorrectModuleRegistryCallTypeParameterParserError, + throwIfUnsupportedFunctionReturnTypeAnnotationParserError, } = require('../error-utils'); const { ModuleInterfaceNotFoundParserError, @@ -26,6 +27,7 @@ const { UnusedModuleInterfaceParserError, IncorrectModuleRegistryCallArityParserError, IncorrectModuleRegistryCallTypeParameterParserError, + UnsupportedFunctionReturnTypeAnnotationParserError, } = require('../errors'); describe('throwIfModuleInterfaceIsMisnamed', () => { @@ -173,6 +175,63 @@ describe('throwErrorIfWrongNumberOfCallExpressionArgs', () => { }); }); +describe('throwIfUnsupportedFunctionReturnTypeAnnotationParserError', () => { + const returnTypeAnnotation = { + returnType: '', + }, + nativeModuleName = 'moduleName', + invalidReturnType = 'FunctionTypeAnnotation', + language = 'Flow'; + + it('do not throw error if cxxOnly is true', () => { + const cxxOnly = true, + returnType = 'FunctionTypeAnnotation'; + + expect(() => { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + nativeModuleName, + returnTypeAnnotation, + invalidReturnType, + language, + cxxOnly, + returnType, + ); + }).not.toThrow(UnsupportedFunctionReturnTypeAnnotationParserError); + }); + + it('do not throw error if returnTypeAnnotation type is not FunctionTypeAnnotation', () => { + const cxxOnly = false, + returnType = ''; + + expect(() => { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + nativeModuleName, + returnTypeAnnotation, + invalidReturnType, + language, + cxxOnly, + returnType, + ); + }).not.toThrow(UnsupportedFunctionReturnTypeAnnotationParserError); + }); + + it('throw error if cxxOnly is false and returnTypeAnnotation type is FunctionTypeAnnotation', () => { + const cxxOnly = false, + returnType = 'FunctionTypeAnnotation'; + + expect(() => { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + nativeModuleName, + returnTypeAnnotation, + invalidReturnType, + language, + cxxOnly, + returnType, + ); + }).toThrow(UnsupportedFunctionReturnTypeAnnotationParserError); + }); +}); + describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => { const nativeModuleName = 'moduleName'; const methodName = 'methodName'; diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js index ea419933ec8..31d134345e0 100644 --- a/packages/react-native-codegen/src/parsers/error-utils.js +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -14,6 +14,7 @@ import type {ParserType} from './errors'; const { MisnamedModuleInterfaceParserError, + UnsupportedFunctionReturnTypeAnnotationParserError, ModuleInterfaceNotFoundParserError, MoreThanOneModuleRegistryCallsParserError, UnusedModuleInterfaceParserError, @@ -140,6 +141,24 @@ function throwIfIncorrectModuleRegistryCallTypeParameterParserError( } } +function throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + nativeModuleName: string, + returnTypeAnnotation: $FlowFixMe, + invalidReturnType: string, + language: ParserType, + cxxOnly: boolean, + returnType: string, +) { + if (!cxxOnly && returnType === 'FunctionTypeAnnotation') { + throw new UnsupportedFunctionReturnTypeAnnotationParserError( + nativeModuleName, + returnTypeAnnotation.returnType, + 'FunctionTypeAnnotation', + language, + ); + } +} + function throwIfUntypedModule( typeArguments: $FlowFixMe, hasteModuleName: string, @@ -216,6 +235,7 @@ function throwIfPropertyValueTypeIsUnsupported( module.exports = { throwIfModuleInterfaceIsMisnamed, + throwIfUnsupportedFunctionReturnTypeAnnotationParserError, throwIfModuleInterfaceNotFound, throwIfMoreThanOneModuleRegistryCalls, throwIfPropertyValueTypeIsUnsupported, 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 c9649217047..0a4ffe69b8a 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -60,7 +60,6 @@ const { UnsupportedGenericParserError, UnsupportedTypeAnnotationParserError, UnsupportedFunctionParamTypeAnnotationParserError, - UnsupportedFunctionReturnTypeAnnotationParserError, UnsupportedEnumDeclarationParserError, UnsupportedUnionTypeAnnotationParserError, UnsupportedObjectPropertyTypeAnnotationParserError, @@ -69,6 +68,7 @@ const { const {verifyPlatforms} = require('../../utils'); const { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError, throwIfModuleInterfaceNotFound, throwIfModuleInterfaceIsMisnamed, throwIfPropertyValueTypeIsUnsupported, @@ -494,14 +494,14 @@ function translateFunctionTypeAnnotation( ), ); - if (!cxxOnly && returnTypeAnnotation.type === 'FunctionTypeAnnotation') { - throw new UnsupportedFunctionReturnTypeAnnotationParserError( - hasteModuleName, - flowFunctionTypeAnnotation.returnType, - 'FunctionTypeAnnotation', - language, - ); - } + throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + hasteModuleName, + flowFunctionTypeAnnotation, + 'FunctionTypeAnnotation', + language, + cxxOnly, + returnTypeAnnotation.type, + ); return { type: 'FunctionTypeAnnotation', 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 d61b2a69e71..5b32ff29dc1 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -60,7 +60,6 @@ const { UnsupportedGenericParserError, UnsupportedTypeAnnotationParserError, UnsupportedFunctionParamTypeAnnotationParserError, - UnsupportedFunctionReturnTypeAnnotationParserError, UnsupportedEnumDeclarationParserError, UnsupportedUnionTypeAnnotationParserError, UnsupportedObjectPropertyTypeAnnotationParserError, @@ -77,6 +76,7 @@ const { throwIfModuleInterfaceIsMisnamed, throwIfWrongNumberOfCallExpressionArgs, throwIfIncorrectModuleRegistryCallTypeParameterParserError, + throwIfUnsupportedFunctionReturnTypeAnnotationParserError, } = require('../../error-utils'); const language = 'TypeScript'; @@ -510,14 +510,14 @@ function translateFunctionTypeAnnotation( ), ); - if (!cxxOnly && returnTypeAnnotation.type === 'FunctionTypeAnnotation') { - throw new UnsupportedFunctionReturnTypeAnnotationParserError( - hasteModuleName, - typescriptFunctionTypeAnnotation.returnType, - 'FunctionTypeAnnotation', - language, - ); - } + throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + hasteModuleName, + typescriptFunctionTypeAnnotation, + 'FunctionTypeAnnotation', + language, + cxxOnly, + returnTypeAnnotation.type, + ); return { type: 'FunctionTypeAnnotation',