Skip to content

Commit ab22e3a

Browse files
mohitcharkhafacebook-github-bot
authored andcommitted
Extracted MoreThanOneModuleRegistryCallsParserError exception to throwIfMoreThanOneModuleRegistryCalls inside error-utils.js (#34921)
Summary: This PR is part of #34872 This PR extracts MoreThanOneModuleRegistryCallsParserError exception to throwIfMoreThanOneModuleRegistryCalls inside an error-utils.js file ## 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] - Extracted MoreThanOneModuleRegistryCallsParserError exception to throwIfMoreThanOneModuleRegistryCalls function Pull Request resolved: #34921 Test Plan: yarn jest react-native-codegen Added unit case in the `error-utils-test.js` file <img width="936" alt="Extracted MoreThanOneModuleRegistryCallsParserError Test" src="https://user-images.githubusercontent.com/86604753/194876724-8beec16d-4b88-4685-b8a7-a2c89208378a.png"> Reviewed By: NickGerleman Differential Revision: D40296527 Pulled By: cipolleschi fbshipit-source-id: 8c0d2854f42918b61ce8569cbfbaee5e90b21398
1 parent c9338c4 commit ab22e3a

File tree

4 files changed

+78
-21
lines changed

4 files changed

+78
-21
lines changed

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@
1111

1212
'use strict';
1313

14-
const {throwIfModuleInterfaceNotFound} = require('../error-utils');
15-
const {ModuleInterfaceNotFoundParserError} = require('../errors');
14+
const {
15+
throwIfModuleInterfaceNotFound,
16+
throwIfMoreThanOneModuleRegistryCalls,
17+
} = require('../error-utils');
18+
const {
19+
ModuleInterfaceNotFoundParserError,
20+
MoreThanOneModuleRegistryCallsParserError,
21+
} = require('../errors');
1622

1723
describe('throwIfModuleInterfaceNotFound', () => {
1824
it('throw error if there are zero module specs', () => {
@@ -35,3 +41,37 @@ describe('throwIfModuleInterfaceNotFound', () => {
3541
}).not.toThrow(ModuleInterfaceNotFoundParserError);
3642
});
3743
});
44+
45+
describe('throwIfMoreThanOneModuleRegistryCalls', () => {
46+
it('throw error if module registry calls more than one', () => {
47+
const nativeModuleName = 'moduleName';
48+
const callExpressions = [
49+
{name: 'callExpression1'},
50+
{name: 'callExpression2'},
51+
];
52+
const parserType = 'Flow';
53+
54+
expect(() => {
55+
throwIfMoreThanOneModuleRegistryCalls(
56+
nativeModuleName,
57+
callExpressions,
58+
callExpressions.length,
59+
parserType,
60+
);
61+
}).toThrow(MoreThanOneModuleRegistryCallsParserError);
62+
});
63+
it("don't throw error if single module registry call", () => {
64+
const nativeModuleName = 'moduleName';
65+
const callExpressions = [{name: 'callExpression1'}];
66+
const parserType = 'TypeScript';
67+
68+
expect(() => {
69+
throwIfMoreThanOneModuleRegistryCalls(
70+
nativeModuleName,
71+
callExpressions,
72+
callExpressions.length,
73+
parserType,
74+
);
75+
}).not.toThrow(MoreThanOneModuleRegistryCallsParserError);
76+
});
77+
});

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
'use strict';
1212

1313
import type {ParserType} from './errors';
14-
const {ModuleInterfaceNotFoundParserError} = require('./errors.js');
14+
15+
const {
16+
ModuleInterfaceNotFoundParserError,
17+
MoreThanOneModuleRegistryCallsParserError,
18+
} = require('./errors.js');
1519

1620
function throwIfModuleInterfaceNotFound(
1721
numberOfModuleSpecs: number,
@@ -28,6 +32,23 @@ function throwIfModuleInterfaceNotFound(
2832
}
2933
}
3034

35+
function throwIfMoreThanOneModuleRegistryCalls(
36+
hasteModuleName: string,
37+
callExpressions: $FlowFixMe,
38+
callExpressionsLength: number,
39+
language: ParserType,
40+
) {
41+
if (callExpressions.length > 1) {
42+
throw new MoreThanOneModuleRegistryCallsParserError(
43+
hasteModuleName,
44+
callExpressions,
45+
callExpressionsLength,
46+
language,
47+
);
48+
}
49+
}
50+
3151
module.exports = {
3252
throwIfModuleInterfaceNotFound,
53+
throwIfMoreThanOneModuleRegistryCalls,
3354
};

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type {
2525
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
2626
import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js';
2727

28+
const {throwIfMoreThanOneModuleRegistryCalls} = require('../../error-utils');
2829
const {
2930
resolveTypeAnnotation,
3031
getTypes,
@@ -63,7 +64,6 @@ const {
6364
UnsupportedObjectPropertyTypeAnnotationParserError,
6465
UnsupportedObjectPropertyValueTypeAnnotationParserError,
6566
UnusedModuleInterfaceParserError,
66-
MoreThanOneModuleRegistryCallsParserError,
6767
UntypedModuleRegistryCallParserError,
6868
IncorrectModuleRegistryCallTypeParameterParserError,
6969
IncorrectModuleRegistryCallArityParserError,
@@ -624,14 +624,12 @@ function buildModuleSchema(
624624
);
625625
}
626626

627-
if (callExpressions.length > 1) {
628-
throw new MoreThanOneModuleRegistryCallsParserError(
629-
hasteModuleName,
630-
callExpressions,
631-
callExpressions.length,
632-
language,
633-
);
634-
}
627+
throwIfMoreThanOneModuleRegistryCalls(
628+
hasteModuleName,
629+
callExpressions,
630+
callExpressions.length,
631+
language,
632+
);
635633

636634
const [callExpression] = callExpressions;
637635
const {typeArguments} = callExpression;

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type {
2525
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
2626
import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js';
2727

28+
const {throwIfMoreThanOneModuleRegistryCalls} = require('../../error-utils');
2829
const {
2930
resolveTypeAnnotation,
3031
getTypes,
@@ -63,7 +64,6 @@ const {
6364
UnsupportedObjectPropertyTypeAnnotationParserError,
6465
UnsupportedObjectPropertyValueTypeAnnotationParserError,
6566
UnusedModuleInterfaceParserError,
66-
MoreThanOneModuleRegistryCallsParserError,
6767
UntypedModuleRegistryCallParserError,
6868
IncorrectModuleRegistryCallTypeParameterParserError,
6969
IncorrectModuleRegistryCallArityParserError,
@@ -657,14 +657,12 @@ function buildModuleSchema(
657657
);
658658
}
659659

660-
if (callExpressions.length > 1) {
661-
throw new MoreThanOneModuleRegistryCallsParserError(
662-
hasteModuleName,
663-
callExpressions,
664-
callExpressions.length,
665-
language,
666-
);
667-
}
660+
throwIfMoreThanOneModuleRegistryCalls(
661+
hasteModuleName,
662+
callExpressions,
663+
callExpressions.length,
664+
language,
665+
);
668666

669667
const [callExpression] = callExpressions;
670668
const {typeParameters} = callExpression;

0 commit comments

Comments
 (0)