Skip to content

Commit 094ae08

Browse files
mohitcharkhaOlimpiaZurek
authored andcommitted
Extract MisnamedModuleInterfaceParserError from Flow and Typescript into error-utils.js (facebook#34916)
Summary: This PR is part of facebook#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: facebook#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
1 parent 73ba60b commit 094ae08

File tree

5 files changed

+43
-17
lines changed

5 files changed

+43
-17
lines changed

packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js

+23
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,41 @@
1414
const {
1515
throwIfModuleInterfaceNotFound,
1616
throwIfMoreThanOneModuleRegistryCalls,
17+
throwIfModuleInterfaceIsMisnamed,
1718
throwIfUnusedModuleInterfaceParserError,
1819
throwIfWrongNumberOfCallExpressionArgs,
1920
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
2021
} = require('../error-utils');
2122
const {
2223
ModuleInterfaceNotFoundParserError,
2324
MoreThanOneModuleRegistryCallsParserError,
25+
MisnamedModuleInterfaceParserError,
2426
UnusedModuleInterfaceParserError,
2527
IncorrectModuleRegistryCallArityParserError,
2628
IncorrectModuleRegistryCallTypeParameterParserError,
2729
} = require('../errors');
2830

31+
describe('throwIfModuleInterfaceIsMisnamed', () => {
32+
it("don't throw error if module interface name is Spec", () => {
33+
const nativeModuleName = 'moduleName';
34+
const specId = {name: 'Spec'};
35+
const parserType = 'Flow';
36+
37+
expect(() => {
38+
throwIfModuleInterfaceIsMisnamed(nativeModuleName, specId, parserType);
39+
}).not.toThrow(MisnamedModuleInterfaceParserError);
40+
});
41+
it('throw error if module interface is misnamed', () => {
42+
const nativeModuleName = 'moduleName';
43+
const specId = {name: 'Name'};
44+
const parserType = 'TypeScript';
45+
46+
expect(() => {
47+
throwIfModuleInterfaceIsMisnamed(nativeModuleName, specId, parserType);
48+
}).toThrow(MisnamedModuleInterfaceParserError);
49+
});
50+
});
51+
2952
describe('throwIfModuleInterfaceNotFound', () => {
3053
it('throw error if there are zero module specs', () => {
3154
const nativeModuleName = 'moduleName';

packages/react-native-codegen/src/parsers/error-utils.js

+16
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import type {ParserType} from './errors';
1414

1515
const {
16+
MisnamedModuleInterfaceParserError,
1617
ModuleInterfaceNotFoundParserError,
1718
MoreThanOneModuleRegistryCallsParserError,
1819
UnusedModuleInterfaceParserError,
@@ -22,6 +23,20 @@ const {
2223
UnsupportedModulePropertyParserError,
2324
} = require('./errors.js');
2425

26+
function throwIfModuleInterfaceIsMisnamed(
27+
nativeModuleName: string,
28+
moduleSpecId: $FlowFixMe,
29+
parserType: ParserType,
30+
) {
31+
if (moduleSpecId.name !== 'Spec') {
32+
throw new MisnamedModuleInterfaceParserError(
33+
nativeModuleName,
34+
moduleSpecId,
35+
parserType,
36+
);
37+
}
38+
}
39+
2540
function throwIfModuleInterfaceNotFound(
2641
numberOfModuleSpecs: number,
2742
nativeModuleName: string,
@@ -174,6 +189,7 @@ function throwIfModuleTypeIsUnsupported(
174189
}
175190

176191
module.exports = {
192+
throwIfModuleInterfaceIsMisnamed,
177193
throwIfModuleInterfaceNotFound,
178194
throwIfMoreThanOneModuleRegistryCalls,
179195
throwIfUnusedModuleInterfaceParserError,

packages/react-native-codegen/src/parsers/errors.js

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class ParserError extends Error {
3434
Error.captureStackTrace(this, this.constructor);
3535
}
3636
}
37-
3837
class MisnamedModuleInterfaceParserError extends ParserError {
3938
constructor(nativeModuleName: string, id: $FlowFixMe, language: ParserType) {
4039
super(

packages/react-native-codegen/src/parsers/flow/modules/index.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ const {
5353
typeAliasResolution,
5454
} = require('../../parsers-primitives');
5555
const {
56-
MisnamedModuleInterfaceParserError,
5756
MoreThanOneModuleInterfaceParserError,
5857
UnnamedFunctionParamParserError,
5958
UnsupportedArrayElementTypeAnnotationParserError,
@@ -70,6 +69,7 @@ const {
7069

7170
const {
7271
throwIfModuleInterfaceNotFound,
72+
throwIfModuleInterfaceIsMisnamed,
7373
throwIfUnusedModuleInterfaceParserError,
7474
throwIfWrongNumberOfCallExpressionArgs,
7575
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
@@ -613,13 +613,7 @@ function buildModuleSchema(
613613

614614
const [moduleSpec] = moduleSpecs;
615615

616-
if (moduleSpec.id.name !== 'Spec') {
617-
throw new MisnamedModuleInterfaceParserError(
618-
hasteModuleName,
619-
moduleSpec.id,
620-
language,
621-
);
622-
}
616+
throwIfModuleInterfaceIsMisnamed(hasteModuleName, moduleSpec.id, language);
623617

624618
// Parse Module Names
625619
const moduleName = tryParse((): string => {

packages/react-native-codegen/src/parsers/typescript/modules/index.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ const {
5353
typeAliasResolution,
5454
} = require('../../parsers-primitives');
5555
const {
56-
MisnamedModuleInterfaceParserError,
5756
MoreThanOneModuleInterfaceParserError,
5857
UnnamedFunctionParamParserError,
5958
UnsupportedArrayElementTypeAnnotationParserError,
@@ -73,6 +72,7 @@ const {
7372
throwIfModuleTypeIsUnsupported,
7473
throwIfUnusedModuleInterfaceParserError,
7574
throwIfModuleInterfaceNotFound,
75+
throwIfModuleInterfaceIsMisnamed,
7676
throwIfWrongNumberOfCallExpressionArgs,
7777
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
7878
} = require('../../error-utils');
@@ -627,13 +627,7 @@ function buildModuleSchema(
627627

628628
const [moduleSpec] = moduleSpecs;
629629

630-
if (moduleSpec.id.name !== 'Spec') {
631-
throw new MisnamedModuleInterfaceParserError(
632-
hasteModuleName,
633-
moduleSpec.id,
634-
language,
635-
);
636-
}
630+
throwIfModuleInterfaceIsMisnamed(hasteModuleName, moduleSpec.id, language);
637631

638632
// Parse Module Names
639633
const moduleName = tryParse((): string => {

0 commit comments

Comments
 (0)