Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
* @oncall react_native
*/

'use strict';

const {throwIfWrongNumberOfCallExpressionArgs} = require('../error-utils');
const {IncorrectModuleRegistryCallArityParserError} = require('../errors');

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);
});
});
37 changes: 37 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

('use strict');

import type {ParserType} from './errors';

const {IncorrectModuleRegistryCallArityParserError} = require('./errors');

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 = {
throwIfWrongNumberOfCallExpressionArgs,
};
19 changes: 9 additions & 10 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ const {
MoreThanOneModuleRegistryCallsParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

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

const language = 'Flow';

function nullGuard<T>(fn: () => T): ?T {
Expand Down Expand Up @@ -641,15 +642,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
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ const {
MoreThanOneModuleRegistryCallsParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

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

const language = 'TypeScript';

function nullGuard<T>(fn: () => T): ?T {
Expand Down Expand Up @@ -675,15 +676,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