Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
} = require('../error-utils');
const {
ModuleInterfaceNotFoundParserError,
Expand All @@ -26,6 +27,7 @@ const {
UnusedModuleInterfaceParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
UnsupportedFunctionReturnTypeAnnotationParserError,
} = require('../errors');

describe('throwIfModuleInterfaceIsMisnamed', () => {
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -467,7 +526,7 @@ describe('throwIfUntypedModule', () => {
});
});

describe('throwIfMoreThanOneModuleRegistryCalls', () => {
describe('throwIfModuleTypeIsUnsupported', () => {
const {throwIfModuleTypeIsUnsupported} = require('../error-utils.js');
const {UnsupportedModulePropertyParserError} = require('../errors.js');
const hasteModuleName = 'moduleName';
Expand Down
20 changes: 20 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import type {ParserType} from './errors';

const {
UnsupportedFunctionReturnTypeAnnotationParserError,
MisnamedModuleInterfaceParserError,
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleRegistryCallsParserError,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -189,6 +208,7 @@ function throwIfModuleTypeIsUnsupported(
}

module.exports = {
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
throwIfModuleInterfaceIsMisnamed,
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
Expand Down
18 changes: 9 additions & 9 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const {
UnsupportedGenericParserError,
UnsupportedTypeAnnotationParserError,
UnsupportedFunctionParamTypeAnnotationParserError,
UnsupportedFunctionReturnTypeAnnotationParserError,
UnsupportedEnumDeclarationParserError,
UnsupportedUnionTypeAnnotationParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
Expand All @@ -69,6 +68,7 @@ const {
const {verifyPlatforms} = require('../../utils');

const {
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
throwIfUnusedModuleInterfaceParserError,
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const {
UnsupportedGenericParserError,
UnsupportedTypeAnnotationParserError,
UnsupportedFunctionParamTypeAnnotationParserError,
UnsupportedFunctionReturnTypeAnnotationParserError,
UnsupportedEnumDeclarationParserError,
UnsupportedUnionTypeAnnotationParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
Expand All @@ -76,6 +75,7 @@ const {
throwIfModuleInterfaceIsMisnamed,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
} = require('../../error-utils');

const language = 'TypeScript';
Expand Down Expand Up @@ -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',
Expand Down