Skip to content

Commit a33f672

Browse files
vinayharwani13facebook-github-bot
authored andcommitted
Extracted UnusedModuleInterfaceParserError to throwIfUnusedModuleInterfaceParserError in error-utils.js (#34939)
Summary: This PR is part of #34872 This PR extracts UnusedModuleInterfaceParserError exception to throwIfUnusedModuleInterfaceParserError 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 UnusedModuleInterfaceParserError exception to throwIfUnusedModuleInterfaceParserError function Pull Request resolved: #34939 Test Plan: yarn jest react-native-codegen Added unit case in error-utils-test.js file <img width="1173" alt="Screenshot 2022-10-11 at 1 56 50 PM" src="https://user-images.githubusercontent.com/87412080/195039924-5a73283b-a8cf-41a5-bcc4-9f619d381240.png"> Reviewed By: dmytrorykun Differential Revision: D40296568 Pulled By: cipolleschi fbshipit-source-id: b5ee4520db9c70a57c0c666d26ff3f63f17e9a59
1 parent ab22e3a commit a33f672

File tree

4 files changed

+71
-19
lines changed

4 files changed

+71
-19
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
const {
1515
throwIfModuleInterfaceNotFound,
1616
throwIfMoreThanOneModuleRegistryCalls,
17+
throwIfUnusedModuleInterfaceParserError,
1718
} = require('../error-utils');
1819
const {
1920
ModuleInterfaceNotFoundParserError,
2021
MoreThanOneModuleRegistryCallsParserError,
22+
UnusedModuleInterfaceParserError,
2123
} = require('../errors');
2224

2325
describe('throwIfModuleInterfaceNotFound', () => {
@@ -75,3 +77,35 @@ describe('throwIfMoreThanOneModuleRegistryCalls', () => {
7577
}).not.toThrow(MoreThanOneModuleRegistryCallsParserError);
7678
});
7779
});
80+
81+
describe('throwIfUnusedModuleInterfaceParserError', () => {
82+
it('throw error if unused module', () => {
83+
const nativeModuleName = 'moduleName';
84+
const callExpressions = [];
85+
const spec = {name: 'Spec'};
86+
const parserType = 'Flow';
87+
expect(() => {
88+
throwIfUnusedModuleInterfaceParserError(
89+
nativeModuleName,
90+
spec,
91+
callExpressions,
92+
parserType,
93+
);
94+
}).toThrow(UnusedModuleInterfaceParserError);
95+
});
96+
97+
it("don't throw error if module is used", () => {
98+
const nativeModuleName = 'moduleName';
99+
const callExpressions = [{name: 'callExpression1'}];
100+
const spec = {name: 'Spec'};
101+
const parserType = 'TypeScript';
102+
expect(() => {
103+
throwIfUnusedModuleInterfaceParserError(
104+
nativeModuleName,
105+
spec,
106+
callExpressions,
107+
parserType,
108+
);
109+
}).not.toThrow(UnusedModuleInterfaceParserError);
110+
});
111+
});

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {ParserType} from './errors';
1515
const {
1616
ModuleInterfaceNotFoundParserError,
1717
MoreThanOneModuleRegistryCallsParserError,
18+
UnusedModuleInterfaceParserError,
1819
} = require('./errors.js');
1920

2021
function throwIfModuleInterfaceNotFound(
@@ -48,7 +49,23 @@ function throwIfMoreThanOneModuleRegistryCalls(
4849
}
4950
}
5051

52+
function throwIfUnusedModuleInterfaceParserError(
53+
nativeModuleName: string,
54+
moduleSpec: $FlowFixMe,
55+
callExpressions: $FlowFixMe,
56+
language: ParserType,
57+
) {
58+
if (callExpressions.length === 0) {
59+
throw new UnusedModuleInterfaceParserError(
60+
nativeModuleName,
61+
moduleSpec,
62+
language,
63+
);
64+
}
65+
}
66+
5167
module.exports = {
5268
throwIfModuleInterfaceNotFound,
5369
throwIfMoreThanOneModuleRegistryCalls,
70+
throwIfUnusedModuleInterfaceParserError,
5471
};

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,16 @@ const {
6363
UnsupportedModulePropertyParserError,
6464
UnsupportedObjectPropertyTypeAnnotationParserError,
6565
UnsupportedObjectPropertyValueTypeAnnotationParserError,
66-
UnusedModuleInterfaceParserError,
6766
UntypedModuleRegistryCallParserError,
6867
IncorrectModuleRegistryCallTypeParameterParserError,
6968
IncorrectModuleRegistryCallArityParserError,
7069
IncorrectModuleRegistryCallArgumentTypeParserError,
7170
} = require('../../errors.js');
7271

73-
const {throwIfModuleInterfaceNotFound} = require('../../error-utils');
74-
72+
const {
73+
throwIfModuleInterfaceNotFound,
74+
throwIfUnusedModuleInterfaceParserError,
75+
} = require('../../error-utils');
7576
const language = 'Flow';
7677

7778
function nullGuard<T>(fn: () => T): ?T {
@@ -616,13 +617,12 @@ function buildModuleSchema(
616617
},
617618
});
618619

619-
if (callExpressions.length === 0) {
620-
throw new UnusedModuleInterfaceParserError(
621-
hasteModuleName,
622-
moduleSpec,
623-
language,
624-
);
625-
}
620+
throwIfUnusedModuleInterfaceParserError(
621+
hasteModuleName,
622+
moduleSpec,
623+
callExpressions,
624+
language,
625+
);
626626

627627
throwIfMoreThanOneModuleRegistryCalls(
628628
hasteModuleName,

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ const {
6363
UnsupportedModulePropertyParserError,
6464
UnsupportedObjectPropertyTypeAnnotationParserError,
6565
UnsupportedObjectPropertyValueTypeAnnotationParserError,
66-
UnusedModuleInterfaceParserError,
6766
UntypedModuleRegistryCallParserError,
6867
IncorrectModuleRegistryCallTypeParameterParserError,
6968
IncorrectModuleRegistryCallArityParserError,
7069
IncorrectModuleRegistryCallArgumentTypeParserError,
7170
} = require('../../errors.js');
72-
const {throwIfModuleInterfaceNotFound} = require('../../error-utils');
7371

72+
const {
73+
throwIfUnusedModuleInterfaceParserError,
74+
throwIfModuleInterfaceNotFound,
75+
} = require('../../error-utils');
7476
const language = 'TypeScript';
7577

7678
function nullGuard<T>(fn: () => T): ?T {
@@ -649,13 +651,12 @@ function buildModuleSchema(
649651
},
650652
});
651653

652-
if (callExpressions.length === 0) {
653-
throw new UnusedModuleInterfaceParserError(
654-
hasteModuleName,
655-
moduleSpec,
656-
language,
657-
);
658-
}
654+
throwIfUnusedModuleInterfaceParserError(
655+
hasteModuleName,
656+
moduleSpec,
657+
callExpressions,
658+
language,
659+
);
659660

660661
throwIfMoreThanOneModuleRegistryCalls(
661662
hasteModuleName,

0 commit comments

Comments
 (0)