Skip to content

Commit

Permalink
Extract MisnamedModuleInterfaceParserError from Flow and Typescript i…
Browse files Browse the repository at this point in the history
…nto error-utils.js (#34916)

Summary:
This PR is part of #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: #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
  • Loading branch information
mohitcharkha authored and facebook-github-bot committed Oct 17, 2022
1 parent cd83194 commit 9fb3700
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,41 @@
const {
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
throwIfModuleInterfaceIsMisnamed,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
} = require('../error-utils');
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';
Expand Down
16 changes: 16 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import type {ParserType} from './errors';

const {
MisnamedModuleInterfaceParserError,
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleRegistryCallsParserError,
UnusedModuleInterfaceParserError,
Expand All @@ -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,
Expand Down Expand Up @@ -174,6 +189,7 @@ function throwIfModuleTypeIsUnsupported(
}

module.exports = {
throwIfModuleInterfaceIsMisnamed,
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
throwIfUnusedModuleInterfaceParserError,
Expand Down
1 change: 0 additions & 1 deletion packages/react-native-codegen/src/parsers/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 2 additions & 8 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const {
typeAliasResolution,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
MoreThanOneModuleInterfaceParserError,
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
Expand All @@ -70,6 +69,7 @@ const {

const {
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
Expand Down Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const {
typeAliasResolution,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
MoreThanOneModuleInterfaceParserError,
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
Expand All @@ -73,6 +72,7 @@ const {
throwIfModuleTypeIsUnsupported,
throwIfUnusedModuleInterfaceParserError,
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
} = require('../../error-utils');
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 9fb3700

Please sign in to comment.