Skip to content

Commit

Permalink
extracted UntypedModuleRegistryCallParserError to a throwing functi…
Browse files Browse the repository at this point in the history
…on in error-utils.js (#34953)

Summary:
This PR is part of #34872.
As a part of this PR, `UntypedModuleRegistryCallParserError` is separated into its own throwing function in `error-utils.js` file and is used in both Flow and TypeScript parsers

## Changelog

[Internal] [Changed] - Extract `UntypedModuleRegistryCallParserError` to a separate function inside `error-utils.js` file.

Pull Request resolved: #34953

Test Plan:
Added unit case in error-utils-test.js file to test the new function.
Output of `yarn jest react-native-codegen` ensures all passed test cases.

<img width="450" alt="Screenshot 2022-10-12 at 12 44 36 PM" src="https://user-images.githubusercontent.com/32268377/195277708-97340db3-f3d8-48a3-9a59-95d2747c67b0.png">

Reviewed By: christophpurrer

Differential Revision: D40297017

Pulled By: cipolleschi

fbshipit-source-id: b02dcf0e110ab903a0d1831783194ae4a543075b
  • Loading branch information
dhruvtailor7 authored and facebook-github-bot committed Oct 14, 2022
1 parent b87d371 commit 1e15e21
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,46 @@ describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => {
}).not.toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
});

describe('throwIfUntypedModule', () => {
const {throwIfUntypedModule} = require('../error-utils');
const {UntypedModuleRegistryCallParserError} = require('../errors');
const hasteModuleName = 'moduleName';
const methodName = 'methodName';
const moduleName = 'moduleName';
const callExpressions = [];

it('should throw error if module does not have a type', () => {
const typeArguments = null;
const language = 'Flow';
expect(() =>
throwIfUntypedModule(
typeArguments,
hasteModuleName,
callExpressions,
methodName,
moduleName,
language,
),
).toThrowError(UntypedModuleRegistryCallParserError);
});

it('should not throw error if module have a type', () => {
const typeArguments = {
type: 'TSTypeParameterInstantiations',
params: [],
};

const language = 'TypeScript';
expect(() =>
throwIfUntypedModule(
typeArguments,
hasteModuleName,
callExpressions,
methodName,
moduleName,
language,
),
).not.toThrowError(UntypedModuleRegistryCallParserError);
});
});
21 changes: 21 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
UnusedModuleInterfaceParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
UntypedModuleRegistryCallParserError,
} = require('./errors.js');

function throwIfModuleInterfaceNotFound(
Expand Down Expand Up @@ -122,10 +123,30 @@ function throwIfIncorrectModuleRegistryCallTypeParameterParserError(
}
}

function throwIfUntypedModule(
typeArguments: $FlowFixMe,
hasteModuleName: string,
callExpression: $FlowFixMe,
methodName: string,
$moduleName: string,
language: ParserType,
) {
if (typeArguments == null) {
throw new UntypedModuleRegistryCallParserError(
hasteModuleName,
callExpression,
methodName,
$moduleName,
language,
);
}
}

module.exports = {
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUntypedModule,
};
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 @@ -66,9 +66,9 @@ const {
UnsupportedModulePropertyParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
const {throwIfUntypedModule} = require('../../error-utils');

const {
throwIfModuleInterfaceNotFound,
Expand Down Expand Up @@ -673,15 +673,14 @@ function buildModuleSchema(

const $moduleName = callExpression.arguments[0].value;

if (typeArguments == null) {
throw new UntypedModuleRegistryCallParserError(
hasteModuleName,
callExpression,
methodName,
$moduleName,
language,
);
}
throwIfUntypedModule(
typeArguments,
hasteModuleName,
callExpression,
methodName,
$moduleName,
language,
);

throwIfIncorrectModuleRegistryCallTypeParameterParserError(
hasteModuleName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ const {
UnsupportedModulePropertyParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
const {throwIfUntypedModule} = require('../../error-utils');

const {
throwIfUnusedModuleInterfaceParserError,
Expand Down Expand Up @@ -688,15 +688,14 @@ function buildModuleSchema(

const $moduleName = callExpression.arguments[0].value;

if (typeParameters == null) {
throw new UntypedModuleRegistryCallParserError(
hasteModuleName,
callExpression,
methodName,
$moduleName,
language,
);
}
throwIfUntypedModule(
typeParameters,
hasteModuleName,
callExpression,
methodName,
$moduleName,
language,
);

throwIfIncorrectModuleRegistryCallTypeParameterParserError(
hasteModuleName,
Expand Down

0 comments on commit 1e15e21

Please sign in to comment.