Skip to content

Commit 76c5b6f

Browse files
dakshbhardwajfacebook-github-bot
authored andcommitted
Extracted IncorrectModuleRegistryCallArityParserError as a seperate function in error-utils.js (#34940)
Summary: This PR is part of #34872 This PR extracts `IncorrectModuleRegistryCallArityParserError` exception to `throwIfIncorrectModuleRegistryCallArityParserError` inside an `error-utils.js` file. ## Changelog [Internal] [Changed] - Extracted IncorrectModuleRegistryCallArityParserError exception to throwIfIncorrectModuleRegistryCallArityParserError function Pull Request resolved: #34940 Test Plan: Added unit case in error-utils-test.js file yarn jest react-native-codegen <img width="1144" alt="Screenshot 2022-10-11 at 4 04 55 PM" src="https://user-images.githubusercontent.com/22423684/195070498-627d1bb8-53b1-43d3-b410-462e4f0da23a.png"> Reviewed By: dmytrorykun Differential Revision: D40296626 Pulled By: cipolleschi fbshipit-source-id: 33a299b1adf6334753c2390a81a54832c9096ec5
1 parent a33f672 commit 76c5b6f

File tree

4 files changed

+76
-20
lines changed

4 files changed

+76
-20
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ const {
1515
throwIfModuleInterfaceNotFound,
1616
throwIfMoreThanOneModuleRegistryCalls,
1717
throwIfUnusedModuleInterfaceParserError,
18+
throwIfWrongNumberOfCallExpressionArgs,
1819
} = require('../error-utils');
1920
const {
2021
ModuleInterfaceNotFoundParserError,
2122
MoreThanOneModuleRegistryCallsParserError,
2223
UnusedModuleInterfaceParserError,
24+
IncorrectModuleRegistryCallArityParserError,
2325
} = require('../errors');
2426

2527
describe('throwIfModuleInterfaceNotFound', () => {
@@ -109,3 +111,39 @@ describe('throwIfUnusedModuleInterfaceParserError', () => {
109111
}).not.toThrow(UnusedModuleInterfaceParserError);
110112
});
111113
});
114+
115+
describe('throwErrorIfWrongNumberOfCallExpressionArgs', () => {
116+
it('throw error if wrong number of call expression args is used', () => {
117+
const nativeModuleName = 'moduleName';
118+
const flowCallExpression = {argument: []};
119+
const methodName = 'methodName';
120+
const numberOfCallExpressionArgs = flowCallExpression.argument.length;
121+
const language = 'Flow';
122+
expect(() => {
123+
throwIfWrongNumberOfCallExpressionArgs(
124+
nativeModuleName,
125+
flowCallExpression,
126+
methodName,
127+
numberOfCallExpressionArgs,
128+
language,
129+
);
130+
}).toThrow(IncorrectModuleRegistryCallArityParserError);
131+
});
132+
133+
it("don't throw error if correct number of call expression args is used", () => {
134+
const nativeModuleName = 'moduleName';
135+
const flowCallExpression = {argument: ['argument']};
136+
const methodName = 'methodName';
137+
const numberOfCallExpressionArgs = flowCallExpression.argument.length;
138+
const language = 'Flow';
139+
expect(() => {
140+
throwIfWrongNumberOfCallExpressionArgs(
141+
nativeModuleName,
142+
flowCallExpression,
143+
methodName,
144+
numberOfCallExpressionArgs,
145+
language,
146+
);
147+
}).not.toThrow(IncorrectModuleRegistryCallArityParserError);
148+
});
149+
});

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
ModuleInterfaceNotFoundParserError,
1717
MoreThanOneModuleRegistryCallsParserError,
1818
UnusedModuleInterfaceParserError,
19+
IncorrectModuleRegistryCallArityParserError,
1920
} = require('./errors.js');
2021

2122
function throwIfModuleInterfaceNotFound(
@@ -64,8 +65,27 @@ function throwIfUnusedModuleInterfaceParserError(
6465
}
6566
}
6667

68+
function throwIfWrongNumberOfCallExpressionArgs(
69+
nativeModuleName: string,
70+
flowCallExpression: $FlowFixMe,
71+
methodName: string,
72+
numberOfCallExpressionArgs: number,
73+
language: ParserType,
74+
) {
75+
if (numberOfCallExpressionArgs !== 1) {
76+
throw new IncorrectModuleRegistryCallArityParserError(
77+
nativeModuleName,
78+
flowCallExpression,
79+
methodName,
80+
numberOfCallExpressionArgs,
81+
language,
82+
);
83+
}
84+
}
85+
6786
module.exports = {
6887
throwIfModuleInterfaceNotFound,
6988
throwIfMoreThanOneModuleRegistryCalls,
7089
throwIfUnusedModuleInterfaceParserError,
90+
throwIfWrongNumberOfCallExpressionArgs,
7191
};

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ const {
6565
UnsupportedObjectPropertyValueTypeAnnotationParserError,
6666
UntypedModuleRegistryCallParserError,
6767
IncorrectModuleRegistryCallTypeParameterParserError,
68-
IncorrectModuleRegistryCallArityParserError,
6968
IncorrectModuleRegistryCallArgumentTypeParserError,
7069
} = require('../../errors.js');
7170

7271
const {
7372
throwIfModuleInterfaceNotFound,
7473
throwIfUnusedModuleInterfaceParserError,
74+
throwIfWrongNumberOfCallExpressionArgs,
7575
} = require('../../error-utils');
76+
7677
const language = 'Flow';
7778

7879
function nullGuard<T>(fn: () => T): ?T {
@@ -635,15 +636,13 @@ function buildModuleSchema(
635636
const {typeArguments} = callExpression;
636637
const methodName = callExpression.callee.property.name;
637638

638-
if (callExpression.arguments.length !== 1) {
639-
throw new IncorrectModuleRegistryCallArityParserError(
640-
hasteModuleName,
641-
callExpression,
642-
methodName,
643-
callExpression.arguments.length,
644-
language,
645-
);
646-
}
639+
throwIfWrongNumberOfCallExpressionArgs(
640+
hasteModuleName,
641+
callExpression,
642+
methodName,
643+
callExpression.arguments.length,
644+
language,
645+
);
647646

648647
if (callExpression.arguments[0].type !== 'Literal') {
649648
const {type} = callExpression.arguments[0];

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ const {
6565
UnsupportedObjectPropertyValueTypeAnnotationParserError,
6666
UntypedModuleRegistryCallParserError,
6767
IncorrectModuleRegistryCallTypeParameterParserError,
68-
IncorrectModuleRegistryCallArityParserError,
6968
IncorrectModuleRegistryCallArgumentTypeParserError,
7069
} = require('../../errors.js');
7170

7271
const {
7372
throwIfUnusedModuleInterfaceParserError,
7473
throwIfModuleInterfaceNotFound,
74+
throwIfWrongNumberOfCallExpressionArgs,
7575
} = require('../../error-utils');
76+
7677
const language = 'TypeScript';
7778

7879
function nullGuard<T>(fn: () => T): ?T {
@@ -669,15 +670,13 @@ function buildModuleSchema(
669670
const {typeParameters} = callExpression;
670671
const methodName = callExpression.callee.property.name;
671672

672-
if (callExpression.arguments.length !== 1) {
673-
throw new IncorrectModuleRegistryCallArityParserError(
674-
hasteModuleName,
675-
callExpression,
676-
methodName,
677-
callExpression.arguments.length,
678-
language,
679-
);
680-
}
673+
throwIfWrongNumberOfCallExpressionArgs(
674+
hasteModuleName,
675+
callExpression,
676+
methodName,
677+
callExpression.arguments.length,
678+
language,
679+
);
681680

682681
if (callExpression.arguments[0].type !== 'StringLiteral') {
683682
const {type} = callExpression.arguments[0];

0 commit comments

Comments
 (0)