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

<img width="939" alt="Screenshot 2022-10-13 at 11 46 54 AM" src="https://user-images.githubusercontent.com/86605635/195517350-dcb7a26d-434c-4e45-a174-ce82931073e5.png">

Reviewed By: dmytrorykun

Differential Revision: D40338048

Pulled By: cipolleschi

fbshipit-source-id: baa41e0e96c9e17a35f316433c8d80c9bf88d334
This commit is contained in:
harshsiriah
2022-10-19 01:38:28 -07:00
committed by Facebook GitHub Bot
parent eda90e5181
commit 32474367c2
4 changed files with 97 additions and 18 deletions
@@ -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';
@@ -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,
@@ -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',
@@ -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',