Skip to content

Commit

Permalink
Extracted MoreThanOneModuleRegistryCallsParserError exception to thro…
Browse files Browse the repository at this point in the history
…wIfMoreThanOneModuleRegistryCalls 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
  • Loading branch information
mohitcharkha authored and facebook-github-bot committed Oct 14, 2022
1 parent c9338c4 commit ab22e3a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 21 deletions.
Expand Up @@ -11,8 +11,14 @@

'use strict';

const {throwIfModuleInterfaceNotFound} = require('../error-utils');
const {ModuleInterfaceNotFoundParserError} = require('../errors');
const {
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
} = require('../error-utils');
const {
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleRegistryCallsParserError,
} = require('../errors');

describe('throwIfModuleInterfaceNotFound', () => {
it('throw error if there are zero module specs', () => {
Expand All @@ -35,3 +41,37 @@ describe('throwIfModuleInterfaceNotFound', () => {
}).not.toThrow(ModuleInterfaceNotFoundParserError);
});
});

describe('throwIfMoreThanOneModuleRegistryCalls', () => {
it('throw error if module registry calls more than one', () => {
const nativeModuleName = 'moduleName';
const callExpressions = [
{name: 'callExpression1'},
{name: 'callExpression2'},
];
const parserType = 'Flow';

expect(() => {
throwIfMoreThanOneModuleRegistryCalls(
nativeModuleName,
callExpressions,
callExpressions.length,
parserType,
);
}).toThrow(MoreThanOneModuleRegistryCallsParserError);
});
it("don't throw error if single module registry call", () => {
const nativeModuleName = 'moduleName';
const callExpressions = [{name: 'callExpression1'}];
const parserType = 'TypeScript';

expect(() => {
throwIfMoreThanOneModuleRegistryCalls(
nativeModuleName,
callExpressions,
callExpressions.length,
parserType,
);
}).not.toThrow(MoreThanOneModuleRegistryCallsParserError);
});
});
23 changes: 22 additions & 1 deletion packages/react-native-codegen/src/parsers/error-utils.js
Expand Up @@ -11,7 +11,11 @@
'use strict';

import type {ParserType} from './errors';
const {ModuleInterfaceNotFoundParserError} = require('./errors.js');

const {
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleRegistryCallsParserError,
} = require('./errors.js');

function throwIfModuleInterfaceNotFound(
numberOfModuleSpecs: number,
Expand All @@ -28,6 +32,23 @@ function throwIfModuleInterfaceNotFound(
}
}

function throwIfMoreThanOneModuleRegistryCalls(
hasteModuleName: string,
callExpressions: $FlowFixMe,
callExpressionsLength: number,
language: ParserType,
) {
if (callExpressions.length > 1) {
throw new MoreThanOneModuleRegistryCallsParserError(
hasteModuleName,
callExpressions,
callExpressionsLength,
language,
);
}
}

module.exports = {
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
};
16 changes: 7 additions & 9 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Expand Up @@ -25,6 +25,7 @@ import type {
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js';

const {throwIfMoreThanOneModuleRegistryCalls} = require('../../error-utils');
const {
resolveTypeAnnotation,
getTypes,
Expand Down Expand Up @@ -63,7 +64,6 @@ const {
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UnusedModuleInterfaceParserError,
MoreThanOneModuleRegistryCallsParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArityParserError,
Expand Down Expand Up @@ -624,14 +624,12 @@ function buildModuleSchema(
);
}

if (callExpressions.length > 1) {
throw new MoreThanOneModuleRegistryCallsParserError(
hasteModuleName,
callExpressions,
callExpressions.length,
language,
);
}
throwIfMoreThanOneModuleRegistryCalls(
hasteModuleName,
callExpressions,
callExpressions.length,
language,
);

const [callExpression] = callExpressions;
const {typeArguments} = callExpression;
Expand Down
Expand Up @@ -25,6 +25,7 @@ import type {
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js';

const {throwIfMoreThanOneModuleRegistryCalls} = require('../../error-utils');
const {
resolveTypeAnnotation,
getTypes,
Expand Down Expand Up @@ -63,7 +64,6 @@ const {
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UnusedModuleInterfaceParserError,
MoreThanOneModuleRegistryCallsParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArityParserError,
Expand Down Expand Up @@ -657,14 +657,12 @@ function buildModuleSchema(
);
}

if (callExpressions.length > 1) {
throw new MoreThanOneModuleRegistryCallsParserError(
hasteModuleName,
callExpressions,
callExpressions.length,
language,
);
}
throwIfMoreThanOneModuleRegistryCalls(
hasteModuleName,
callExpressions,
callExpressions.length,
language,
);

const [callExpression] = callExpressions;
const {typeParameters} = callExpression;
Expand Down

0 comments on commit ab22e3a

Please sign in to comment.