Skip to content

Commit

Permalink
Extracted IncorrectModuleRegistryCallArityParserError as a seperate f…
Browse files Browse the repository at this point in the history
…unction 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
  • Loading branch information
dakshbhardwaj authored and facebook-github-bot committed Oct 14, 2022
1 parent a33f672 commit 76c5b6f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 20 deletions.
Expand Up @@ -15,11 +15,13 @@ const {
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
} = require('../error-utils');
const {
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleRegistryCallsParserError,
UnusedModuleInterfaceParserError,
IncorrectModuleRegistryCallArityParserError,
} = require('../errors');

describe('throwIfModuleInterfaceNotFound', () => {
Expand Down Expand Up @@ -109,3 +111,39 @@ describe('throwIfUnusedModuleInterfaceParserError', () => {
}).not.toThrow(UnusedModuleInterfaceParserError);
});
});

describe('throwErrorIfWrongNumberOfCallExpressionArgs', () => {
it('throw error if wrong number of call expression args is used', () => {
const nativeModuleName = 'moduleName';
const flowCallExpression = {argument: []};
const methodName = 'methodName';
const numberOfCallExpressionArgs = flowCallExpression.argument.length;
const language = 'Flow';
expect(() => {
throwIfWrongNumberOfCallExpressionArgs(
nativeModuleName,
flowCallExpression,
methodName,
numberOfCallExpressionArgs,
language,
);
}).toThrow(IncorrectModuleRegistryCallArityParserError);
});

it("don't throw error if correct number of call expression args is used", () => {
const nativeModuleName = 'moduleName';
const flowCallExpression = {argument: ['argument']};
const methodName = 'methodName';
const numberOfCallExpressionArgs = flowCallExpression.argument.length;
const language = 'Flow';
expect(() => {
throwIfWrongNumberOfCallExpressionArgs(
nativeModuleName,
flowCallExpression,
methodName,
numberOfCallExpressionArgs,
language,
);
}).not.toThrow(IncorrectModuleRegistryCallArityParserError);
});
});
20 changes: 20 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Expand Up @@ -16,6 +16,7 @@ const {
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleRegistryCallsParserError,
UnusedModuleInterfaceParserError,
IncorrectModuleRegistryCallArityParserError,
} = require('./errors.js');

function throwIfModuleInterfaceNotFound(
Expand Down Expand Up @@ -64,8 +65,27 @@ function throwIfUnusedModuleInterfaceParserError(
}
}

function throwIfWrongNumberOfCallExpressionArgs(
nativeModuleName: string,
flowCallExpression: $FlowFixMe,
methodName: string,
numberOfCallExpressionArgs: number,
language: ParserType,
) {
if (numberOfCallExpressionArgs !== 1) {
throw new IncorrectModuleRegistryCallArityParserError(
nativeModuleName,
flowCallExpression,
methodName,
numberOfCallExpressionArgs,
language,
);
}
}

module.exports = {
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
};
19 changes: 9 additions & 10 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Expand Up @@ -65,14 +65,15 @@ const {
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

const {
throwIfModuleInterfaceNotFound,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
} = require('../../error-utils');

const language = 'Flow';

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

if (callExpression.arguments.length !== 1) {
throw new IncorrectModuleRegistryCallArityParserError(
hasteModuleName,
callExpression,
methodName,
callExpression.arguments.length,
language,
);
}
throwIfWrongNumberOfCallExpressionArgs(
hasteModuleName,
callExpression,
methodName,
callExpression.arguments.length,
language,
);

if (callExpression.arguments[0].type !== 'Literal') {
const {type} = callExpression.arguments[0];
Expand Down
Expand Up @@ -65,14 +65,15 @@ const {
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

const {
throwIfUnusedModuleInterfaceParserError,
throwIfModuleInterfaceNotFound,
throwIfWrongNumberOfCallExpressionArgs,
} = require('../../error-utils');

const language = 'TypeScript';

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

if (callExpression.arguments.length !== 1) {
throw new IncorrectModuleRegistryCallArityParserError(
hasteModuleName,
callExpression,
methodName,
callExpression.arguments.length,
language,
);
}
throwIfWrongNumberOfCallExpressionArgs(
hasteModuleName,
callExpression,
methodName,
callExpression.arguments.length,
language,
);

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

0 comments on commit 76c5b6f

Please sign in to comment.