Skip to content

Commit 3247436

Browse files
harshsiriahfacebook-github-bot
authored andcommitted
Extracted UnsupportedFunctionReturnTypeAnnotationParserError to throwIfUnsupportedFunctionReturnTypeAnnotationParserError (#34965)
Summary: This PR is part of #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: #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
1 parent eda90e5 commit 3247436

File tree

4 files changed

+97
-18
lines changed

4 files changed

+97
-18
lines changed

packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
throwIfUnusedModuleInterfaceParserError,
1919
throwIfWrongNumberOfCallExpressionArgs,
2020
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
21+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
2122
} = require('../error-utils');
2223
const {
2324
ModuleInterfaceNotFoundParserError,
@@ -26,6 +27,7 @@ const {
2627
UnusedModuleInterfaceParserError,
2728
IncorrectModuleRegistryCallArityParserError,
2829
IncorrectModuleRegistryCallTypeParameterParserError,
30+
UnsupportedFunctionReturnTypeAnnotationParserError,
2931
} = require('../errors');
3032

3133
describe('throwIfModuleInterfaceIsMisnamed', () => {
@@ -173,6 +175,63 @@ describe('throwErrorIfWrongNumberOfCallExpressionArgs', () => {
173175
});
174176
});
175177

178+
describe('throwIfUnsupportedFunctionReturnTypeAnnotationParserError', () => {
179+
const returnTypeAnnotation = {
180+
returnType: '',
181+
},
182+
nativeModuleName = 'moduleName',
183+
invalidReturnType = 'FunctionTypeAnnotation',
184+
language = 'Flow';
185+
186+
it('do not throw error if cxxOnly is true', () => {
187+
const cxxOnly = true,
188+
returnType = 'FunctionTypeAnnotation';
189+
190+
expect(() => {
191+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
192+
nativeModuleName,
193+
returnTypeAnnotation,
194+
invalidReturnType,
195+
language,
196+
cxxOnly,
197+
returnType,
198+
);
199+
}).not.toThrow(UnsupportedFunctionReturnTypeAnnotationParserError);
200+
});
201+
202+
it('do not throw error if returnTypeAnnotation type is not FunctionTypeAnnotation', () => {
203+
const cxxOnly = false,
204+
returnType = '';
205+
206+
expect(() => {
207+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
208+
nativeModuleName,
209+
returnTypeAnnotation,
210+
invalidReturnType,
211+
language,
212+
cxxOnly,
213+
returnType,
214+
);
215+
}).not.toThrow(UnsupportedFunctionReturnTypeAnnotationParserError);
216+
});
217+
218+
it('throw error if cxxOnly is false and returnTypeAnnotation type is FunctionTypeAnnotation', () => {
219+
const cxxOnly = false,
220+
returnType = 'FunctionTypeAnnotation';
221+
222+
expect(() => {
223+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
224+
nativeModuleName,
225+
returnTypeAnnotation,
226+
invalidReturnType,
227+
language,
228+
cxxOnly,
229+
returnType,
230+
);
231+
}).toThrow(UnsupportedFunctionReturnTypeAnnotationParserError);
232+
});
233+
});
234+
176235
describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => {
177236
const nativeModuleName = 'moduleName';
178237
const methodName = 'methodName';

packages/react-native-codegen/src/parsers/error-utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {ParserType} from './errors';
1414

1515
const {
1616
MisnamedModuleInterfaceParserError,
17+
UnsupportedFunctionReturnTypeAnnotationParserError,
1718
ModuleInterfaceNotFoundParserError,
1819
MoreThanOneModuleRegistryCallsParserError,
1920
UnusedModuleInterfaceParserError,
@@ -140,6 +141,24 @@ function throwIfIncorrectModuleRegistryCallTypeParameterParserError(
140141
}
141142
}
142143

144+
function throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
145+
nativeModuleName: string,
146+
returnTypeAnnotation: $FlowFixMe,
147+
invalidReturnType: string,
148+
language: ParserType,
149+
cxxOnly: boolean,
150+
returnType: string,
151+
) {
152+
if (!cxxOnly && returnType === 'FunctionTypeAnnotation') {
153+
throw new UnsupportedFunctionReturnTypeAnnotationParserError(
154+
nativeModuleName,
155+
returnTypeAnnotation.returnType,
156+
'FunctionTypeAnnotation',
157+
language,
158+
);
159+
}
160+
}
161+
143162
function throwIfUntypedModule(
144163
typeArguments: $FlowFixMe,
145164
hasteModuleName: string,
@@ -216,6 +235,7 @@ function throwIfPropertyValueTypeIsUnsupported(
216235

217236
module.exports = {
218237
throwIfModuleInterfaceIsMisnamed,
238+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
219239
throwIfModuleInterfaceNotFound,
220240
throwIfMoreThanOneModuleRegistryCalls,
221241
throwIfPropertyValueTypeIsUnsupported,

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ const {
6060
UnsupportedGenericParserError,
6161
UnsupportedTypeAnnotationParserError,
6262
UnsupportedFunctionParamTypeAnnotationParserError,
63-
UnsupportedFunctionReturnTypeAnnotationParserError,
6463
UnsupportedEnumDeclarationParserError,
6564
UnsupportedUnionTypeAnnotationParserError,
6665
UnsupportedObjectPropertyTypeAnnotationParserError,
@@ -69,6 +68,7 @@ const {
6968
const {verifyPlatforms} = require('../../utils');
7069

7170
const {
71+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
7272
throwIfModuleInterfaceNotFound,
7373
throwIfModuleInterfaceIsMisnamed,
7474
throwIfPropertyValueTypeIsUnsupported,
@@ -494,14 +494,14 @@ function translateFunctionTypeAnnotation(
494494
),
495495
);
496496

497-
if (!cxxOnly && returnTypeAnnotation.type === 'FunctionTypeAnnotation') {
498-
throw new UnsupportedFunctionReturnTypeAnnotationParserError(
499-
hasteModuleName,
500-
flowFunctionTypeAnnotation.returnType,
501-
'FunctionTypeAnnotation',
502-
language,
503-
);
504-
}
497+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
498+
hasteModuleName,
499+
flowFunctionTypeAnnotation,
500+
'FunctionTypeAnnotation',
501+
language,
502+
cxxOnly,
503+
returnTypeAnnotation.type,
504+
);
505505

506506
return {
507507
type: 'FunctionTypeAnnotation',

packages/react-native-codegen/src/parsers/typescript/modules/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ const {
6060
UnsupportedGenericParserError,
6161
UnsupportedTypeAnnotationParserError,
6262
UnsupportedFunctionParamTypeAnnotationParserError,
63-
UnsupportedFunctionReturnTypeAnnotationParserError,
6463
UnsupportedEnumDeclarationParserError,
6564
UnsupportedUnionTypeAnnotationParserError,
6665
UnsupportedObjectPropertyTypeAnnotationParserError,
@@ -77,6 +76,7 @@ const {
7776
throwIfModuleInterfaceIsMisnamed,
7877
throwIfWrongNumberOfCallExpressionArgs,
7978
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
79+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
8080
} = require('../../error-utils');
8181

8282
const language = 'TypeScript';
@@ -510,14 +510,14 @@ function translateFunctionTypeAnnotation(
510510
),
511511
);
512512

513-
if (!cxxOnly && returnTypeAnnotation.type === 'FunctionTypeAnnotation') {
514-
throw new UnsupportedFunctionReturnTypeAnnotationParserError(
515-
hasteModuleName,
516-
typescriptFunctionTypeAnnotation.returnType,
517-
'FunctionTypeAnnotation',
518-
language,
519-
);
520-
}
513+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
514+
hasteModuleName,
515+
typescriptFunctionTypeAnnotation,
516+
'FunctionTypeAnnotation',
517+
language,
518+
cxxOnly,
519+
returnTypeAnnotation.type,
520+
);
521521

522522
return {
523523
type: 'FunctionTypeAnnotation',

0 commit comments

Comments
 (0)