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 1b16f4a12e91..a2159154ab9d 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'; @@ -467,7 +526,7 @@ describe('throwIfUntypedModule', () => { }); }); -describe('throwIfMoreThanOneModuleRegistryCalls', () => { +describe('throwIfModuleTypeIsUnsupported', () => { const {throwIfModuleTypeIsUnsupported} = require('../error-utils.js'); const {UnsupportedModulePropertyParserError} = require('../errors.js'); const hasteModuleName = 'moduleName'; diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js index 2c3c237328d9..b3aeb2b0d91e 100644 --- a/packages/react-native-codegen/src/parsers/error-utils.js +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -13,6 +13,7 @@ import type {ParserType} from './errors'; const { + UnsupportedFunctionReturnTypeAnnotationParserError, MisnamedModuleInterfaceParserError, ModuleInterfaceNotFoundParserError, MoreThanOneModuleRegistryCallsParserError, @@ -139,6 +140,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, @@ -189,6 +208,7 @@ function throwIfModuleTypeIsUnsupported( } module.exports = { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError, throwIfModuleInterfaceIsMisnamed, throwIfModuleInterfaceNotFound, throwIfMoreThanOneModuleRegistryCalls, 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 d9e6b13abe01..790b0969dcdf 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -59,7 +59,6 @@ const { UnsupportedGenericParserError, UnsupportedTypeAnnotationParserError, UnsupportedFunctionParamTypeAnnotationParserError, - UnsupportedFunctionReturnTypeAnnotationParserError, UnsupportedEnumDeclarationParserError, UnsupportedUnionTypeAnnotationParserError, UnsupportedObjectPropertyTypeAnnotationParserError, @@ -69,6 +68,7 @@ const { const {verifyPlatforms} = require('../../utils'); const { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError, throwIfModuleInterfaceNotFound, throwIfModuleInterfaceIsMisnamed, throwIfUnusedModuleInterfaceParserError, @@ -511,14 +511,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 90b04f3c38de..4b3d19ae081f 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -59,7 +59,6 @@ const { UnsupportedGenericParserError, UnsupportedTypeAnnotationParserError, UnsupportedFunctionParamTypeAnnotationParserError, - UnsupportedFunctionReturnTypeAnnotationParserError, UnsupportedEnumDeclarationParserError, UnsupportedUnionTypeAnnotationParserError, UnsupportedObjectPropertyTypeAnnotationParserError, @@ -76,6 +75,7 @@ const { throwIfModuleInterfaceIsMisnamed, throwIfWrongNumberOfCallExpressionArgs, throwIfIncorrectModuleRegistryCallTypeParameterParserError, + throwIfUnsupportedFunctionReturnTypeAnnotationParserError, } = require('../../error-utils'); const language = 'TypeScript'; @@ -527,14 +527,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',