Extract MisnamedModuleInterfaceParserError from Flow and Typescript into error-utils.js (#34916)

Summary:
This PR is part of https://github.com/facebook/react-native/issues/34872
This PR extracts MisnamedModuleFlowInterfaceParserError exception to a separate function 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] - Extract MisnamedModuleInterfaceParserError to a seperate function inside error-utils.js

Pull Request resolved: https://github.com/facebook/react-native/pull/34916

Test Plan:
yarn jest react-native-codegen
Added unit case in `error-utils-test.js` file

<img width="980" alt="Extract MisnamedModuleInterfaceParserError test Screenshot" src="https://user-images.githubusercontent.com/86604753/194853899-22c1ce05-fe55-4102-a83b-15c707a20000.png">

Reviewed By: cipolleschi

Differential Revision: D40226541

Pulled By: motiz88

fbshipit-source-id: 6698ceff192c592383aa3419ac31de524c605919
This commit is contained in:
Mohit Charkha
2022-10-17 06:47:13 -07:00
committed by Facebook GitHub Bot
parent cd8319433b
commit 9fb3700d35
5 changed files with 43 additions and 17 deletions
@@ -14,6 +14,7 @@
const {
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
throwIfModuleInterfaceIsMisnamed,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
@@ -21,11 +22,33 @@ const {
const {
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleRegistryCallsParserError,
MisnamedModuleInterfaceParserError,
UnusedModuleInterfaceParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
} = require('../errors');
describe('throwIfModuleInterfaceIsMisnamed', () => {
it("don't throw error if module interface name is Spec", () => {
const nativeModuleName = 'moduleName';
const specId = {name: 'Spec'};
const parserType = 'Flow';
expect(() => {
throwIfModuleInterfaceIsMisnamed(nativeModuleName, specId, parserType);
}).not.toThrow(MisnamedModuleInterfaceParserError);
});
it('throw error if module interface is misnamed', () => {
const nativeModuleName = 'moduleName';
const specId = {name: 'Name'};
const parserType = 'TypeScript';
expect(() => {
throwIfModuleInterfaceIsMisnamed(nativeModuleName, specId, parserType);
}).toThrow(MisnamedModuleInterfaceParserError);
});
});
describe('throwIfModuleInterfaceNotFound', () => {
it('throw error if there are zero module specs', () => {
const nativeModuleName = 'moduleName';
@@ -13,6 +13,7 @@
import type {ParserType} from './errors';
const {
MisnamedModuleInterfaceParserError,
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleRegistryCallsParserError,
UnusedModuleInterfaceParserError,
@@ -22,6 +23,20 @@ const {
UnsupportedModulePropertyParserError,
} = require('./errors.js');
function throwIfModuleInterfaceIsMisnamed(
nativeModuleName: string,
moduleSpecId: $FlowFixMe,
parserType: ParserType,
) {
if (moduleSpecId.name !== 'Spec') {
throw new MisnamedModuleInterfaceParserError(
nativeModuleName,
moduleSpecId,
parserType,
);
}
}
function throwIfModuleInterfaceNotFound(
numberOfModuleSpecs: number,
nativeModuleName: string,
@@ -174,6 +189,7 @@ function throwIfModuleTypeIsUnsupported(
}
module.exports = {
throwIfModuleInterfaceIsMisnamed,
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
throwIfUnusedModuleInterfaceParserError,
-1
View File
@@ -34,7 +34,6 @@ class ParserError extends Error {
Error.captureStackTrace(this, this.constructor);
}
}
class MisnamedModuleInterfaceParserError extends ParserError {
constructor(nativeModuleName: string, id: $FlowFixMe, language: ParserType) {
super(
@@ -53,7 +53,6 @@ const {
typeAliasResolution,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
MoreThanOneModuleInterfaceParserError,
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
@@ -70,6 +69,7 @@ const {
const {
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
@@ -613,13 +613,7 @@ function buildModuleSchema(
const [moduleSpec] = moduleSpecs;
if (moduleSpec.id.name !== 'Spec') {
throw new MisnamedModuleInterfaceParserError(
hasteModuleName,
moduleSpec.id,
language,
);
}
throwIfModuleInterfaceIsMisnamed(hasteModuleName, moduleSpec.id, language);
// Parse Module Names
const moduleName = tryParse((): string => {
@@ -53,7 +53,6 @@ const {
typeAliasResolution,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
MoreThanOneModuleInterfaceParserError,
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
@@ -73,6 +72,7 @@ const {
throwIfModuleTypeIsUnsupported,
throwIfUnusedModuleInterfaceParserError,
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
} = require('../../error-utils');
@@ -627,13 +627,7 @@ function buildModuleSchema(
const [moduleSpec] = moduleSpecs;
if (moduleSpec.id.name !== 'Spec') {
throw new MisnamedModuleInterfaceParserError(
hasteModuleName,
moduleSpec.id,
language,
);
}
throwIfModuleInterfaceIsMisnamed(hasteModuleName, moduleSpec.id, language);
// Parse Module Names
const moduleName = tryParse((): string => {