Skip to content

Commit 8c69b6c

Browse files
Antoine Doubovetzkyfacebook-github-bot
authored andcommitted
Extract throwIfUnsupportedFunctionParamTypeAnnotationParserError function (#35057)
Summary: This PR is a task from #34872: > Extract the UnsupportedFunctionParamTypeAnnotationParserError in its own throwing function (if it does not exists already) and reuse that function passing a proper type. Then, refactor the code using a dictionary and avoiding the three different ifs in both parsers. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal] [Changed] - Extract the UnsupportedFunctionParamTypeAnnotationParserError in its own throwing function in error-utils Pull Request resolved: #35057 Reviewed By: lunaleaps Differential Revision: D40721099 Pulled By: cipolleschi fbshipit-source-id: af5e4cd275d0049047d35660559b94a27e660e40
1 parent 44e8462 commit 8c69b6c

File tree

5 files changed

+53
-32
lines changed

5 files changed

+53
-32
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
throwIfMoreThanOneModuleInterfaceParserError,
2323
throwIfModuleTypeIsUnsupported,
2424
throwIfUntypedModule,
25+
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
2526
} = require('../error-utils');
2627
const {
2728
UnsupportedModulePropertyParserError,
@@ -34,6 +35,7 @@ const {
3435
UnsupportedFunctionReturnTypeAnnotationParserError,
3536
UntypedModuleRegistryCallParserError,
3637
MoreThanOneModuleInterfaceParserError,
38+
UnsupportedFunctionParamTypeAnnotationParserError,
3739
} = require('../errors');
3840

3941
describe('throwIfModuleInterfaceIsMisnamed', () => {
@@ -633,3 +635,20 @@ describe('throwIfMoreThanOneModuleInterfaceParserError', () => {
633635
}).toThrow(MoreThanOneModuleInterfaceParserError);
634636
});
635637
});
638+
639+
describe('throwIfUnsupportedFunctionParamTypeAnnotationParserError', () => {
640+
const nativeModuleName = 'moduleName';
641+
const languageParamTypeAnnotation = {type: 'VoidTypeAnnotation'};
642+
const paramName = 'paramName';
643+
it('throws an UnsupportedFunctionParamTypeAnnotationParserError', () => {
644+
const paramTypeAnnotationType = 'VoidTypeAnnotation';
645+
expect(() => {
646+
throwIfUnsupportedFunctionParamTypeAnnotationParserError(
647+
nativeModuleName,
648+
languageParamTypeAnnotation,
649+
paramName,
650+
paramTypeAnnotationType,
651+
);
652+
}).toThrow(UnsupportedFunctionParamTypeAnnotationParserError);
653+
});
654+
});

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
'use strict';
1212

13+
import type {NativeModuleTypeAnnotation} from '../CodegenSchema';
1314
import type {ParserType} from './errors';
1415

1516
const {
@@ -24,6 +25,7 @@ const {
2425
UntypedModuleRegistryCallParserError,
2526
UnsupportedModulePropertyParserError,
2627
MoreThanOneModuleInterfaceParserError,
28+
UnsupportedFunctionParamTypeAnnotationParserError,
2729
} = require('./errors.js');
2830

2931
function throwIfModuleInterfaceIsMisnamed(
@@ -249,6 +251,20 @@ function throwIfMoreThanOneModuleInterfaceParserError(
249251
}
250252
}
251253

254+
function throwIfUnsupportedFunctionParamTypeAnnotationParserError(
255+
nativeModuleName: string,
256+
languageParamTypeAnnotation: $FlowFixMe,
257+
paramName: string,
258+
paramTypeAnnotationType: NativeModuleTypeAnnotation['type'],
259+
) {
260+
throw new UnsupportedFunctionParamTypeAnnotationParserError(
261+
nativeModuleName,
262+
languageParamTypeAnnotation,
263+
paramName,
264+
paramTypeAnnotationType,
265+
);
266+
}
267+
252268
module.exports = {
253269
throwIfModuleInterfaceIsMisnamed,
254270
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
@@ -261,4 +277,5 @@ module.exports = {
261277
throwIfUntypedModule,
262278
throwIfModuleTypeIsUnsupported,
263279
throwIfMoreThanOneModuleInterfaceParserError,
280+
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
264281
};

packages/react-native-codegen/src/parsers/errors.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ class UnsupportedFunctionParamTypeAnnotationParserError extends ParserError {
258258
flowParamTypeAnnotation: $FlowFixMe,
259259
paramName: string,
260260
invalidParamType: string,
261-
language: ParserType,
262261
) {
263262
super(
264263
nativeModuleName,

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ const {
6060
UnsupportedArrayElementTypeAnnotationParserError,
6161
UnsupportedGenericParserError,
6262
UnsupportedTypeAnnotationParserError,
63-
UnsupportedFunctionParamTypeAnnotationParserError,
6463
UnsupportedEnumDeclarationParserError,
6564
UnsupportedUnionTypeAnnotationParserError,
6665
UnsupportedObjectPropertyTypeAnnotationParserError,
@@ -80,6 +79,7 @@ const {
8079
throwIfUntypedModule,
8180
throwIfModuleTypeIsUnsupported,
8281
throwIfMoreThanOneModuleInterfaceParserError,
82+
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
8383
} = require('../../error-utils');
8484

8585
const {FlowParser} = require('../parser.js');
@@ -436,23 +436,15 @@ function translateFunctionTypeAnnotation(
436436
),
437437
);
438438

439-
if (paramTypeAnnotation.type === 'VoidTypeAnnotation') {
440-
throw new UnsupportedFunctionParamTypeAnnotationParserError(
439+
if (
440+
paramTypeAnnotation.type === 'VoidTypeAnnotation' ||
441+
paramTypeAnnotation.type === 'PromiseTypeAnnotation'
442+
) {
443+
return throwIfUnsupportedFunctionParamTypeAnnotationParserError(
441444
hasteModuleName,
442445
flowParam.typeAnnotation,
443446
paramName,
444-
'void',
445-
language,
446-
);
447-
}
448-
449-
if (paramTypeAnnotation.type === 'PromiseTypeAnnotation') {
450-
throw new UnsupportedFunctionParamTypeAnnotationParserError(
451-
hasteModuleName,
452-
flowParam.typeAnnotation,
453-
paramName,
454-
'Promise',
455-
language,
447+
paramTypeAnnotation.type,
456448
);
457449
}
458450

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
2626
import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js';
2727
const {nullGuard} = require('../../parsers-utils');
2828

29-
const {throwIfMoreThanOneModuleRegistryCalls} = require('../../error-utils');
29+
const {
30+
throwIfMoreThanOneModuleRegistryCalls,
31+
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
32+
} = require('../../error-utils');
3033
const {visit} = require('../../utils');
3134
const {
3235
resolveTypeAnnotation,
@@ -59,7 +62,6 @@ const {
5962
UnsupportedArrayElementTypeAnnotationParserError,
6063
UnsupportedGenericParserError,
6164
UnsupportedTypeAnnotationParserError,
62-
UnsupportedFunctionParamTypeAnnotationParserError,
6365
UnsupportedEnumDeclarationParserError,
6466
UnsupportedObjectPropertyTypeAnnotationParserError,
6567
IncorrectModuleRegistryCallArgumentTypeParserError,
@@ -450,23 +452,15 @@ function translateFunctionTypeAnnotation(
450452
),
451453
);
452454

453-
if (paramTypeAnnotation.type === 'VoidTypeAnnotation') {
454-
throw new UnsupportedFunctionParamTypeAnnotationParserError(
455-
hasteModuleName,
456-
typeScriptParam.typeAnnotation,
457-
paramName,
458-
'void',
459-
language,
460-
);
461-
}
462-
463-
if (paramTypeAnnotation.type === 'PromiseTypeAnnotation') {
464-
throw new UnsupportedFunctionParamTypeAnnotationParserError(
455+
if (
456+
paramTypeAnnotation.type === 'VoidTypeAnnotation' ||
457+
paramTypeAnnotation.type === 'PromiseTypeAnnotation'
458+
) {
459+
return throwIfUnsupportedFunctionParamTypeAnnotationParserError(
465460
hasteModuleName,
466461
typeScriptParam.typeAnnotation,
467462
paramName,
468-
'Promise',
469-
language,
463+
paramTypeAnnotation.type,
470464
);
471465
}
472466

0 commit comments

Comments
 (0)