Skip to content

Commit f628edc

Browse files
Marcoo09facebook-github-bot
authored andcommitted
Chore/extract codegen parser more than one module exception (#34920)
Summary: This PR is part of #34872 This PR extracts MoreThanOneModuleInterfaceParserError exception to a separate function inside an error-utils.js file ## Changelog [Internal] [Changed] - Extract MoreThanOneModuleInterfaceParserError to a seperate function inside error-utils.js Pull Request resolved: #34920 Test Plan: <img width="297" alt="image" src="https://user-images.githubusercontent.com/18408823/194859284-7d3ff330-c644-472e-9ae0-3b9444bc12e8.png"> Reviewed By: cortinico Differential Revision: D40226575 Pulled By: cipolleschi fbshipit-source-id: 01e581abfae1ffe40e92bed8c9bedd6fe09e1aab
1 parent 27cb501 commit f628edc

File tree

4 files changed

+67
-22
lines changed

4 files changed

+67
-22
lines changed

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ const {
1919
throwIfWrongNumberOfCallExpressionArgs,
2020
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
2121
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
22+
throwIfMoreThanOneModuleInterfaceParserError,
23+
throwIfModuleTypeIsUnsupported,
24+
throwIfUntypedModule,
2225
} = require('../error-utils');
2326
const {
27+
UnsupportedModulePropertyParserError,
2428
ModuleInterfaceNotFoundParserError,
2529
MoreThanOneModuleRegistryCallsParserError,
2630
MisnamedModuleInterfaceParserError,
2731
UnusedModuleInterfaceParserError,
2832
IncorrectModuleRegistryCallArityParserError,
2933
IncorrectModuleRegistryCallTypeParameterParserError,
3034
UnsupportedFunctionReturnTypeAnnotationParserError,
35+
UntypedModuleRegistryCallParserError,
36+
MoreThanOneModuleInterfaceParserError,
3137
} = require('../errors');
3238

3339
describe('throwIfModuleInterfaceIsMisnamed', () => {
@@ -484,8 +490,6 @@ describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => {
484490
});
485491

486492
describe('throwIfUntypedModule', () => {
487-
const {throwIfUntypedModule} = require('../error-utils');
488-
const {UntypedModuleRegistryCallParserError} = require('../errors');
489493
const hasteModuleName = 'moduleName';
490494
const methodName = 'methodName';
491495
const moduleName = 'moduleName';
@@ -527,8 +531,6 @@ describe('throwIfUntypedModule', () => {
527531
});
528532

529533
describe('throwIfModuleTypeIsUnsupported', () => {
530-
const {throwIfModuleTypeIsUnsupported} = require('../error-utils.js');
531-
const {UnsupportedModulePropertyParserError} = require('../errors.js');
532534
const hasteModuleName = 'moduleName';
533535
const property = {value: 'value', key: {name: 'name'}};
534536
it("don't throw error if module type is FunctionTypeAnnotation in Flow", () => {
@@ -602,3 +604,32 @@ describe('throwIfModuleTypeIsUnsupported', () => {
602604
}).toThrow(UnsupportedModulePropertyParserError);
603605
});
604606
});
607+
608+
describe('throwIfMoreThanOneModuleInterfaceParserError', () => {
609+
it("don't throw error if module specs length is <= 1", () => {
610+
const nativeModuleName = 'moduleName';
611+
const moduleSpecs = [];
612+
const parserType = 'Flow';
613+
614+
expect(() => {
615+
throwIfMoreThanOneModuleInterfaceParserError(
616+
nativeModuleName,
617+
moduleSpecs,
618+
parserType,
619+
);
620+
}).not.toThrow(MoreThanOneModuleInterfaceParserError);
621+
});
622+
it('throw error if module specs is > 1 ', () => {
623+
const nativeModuleName = 'moduleName';
624+
const moduleSpecs = [{id: {name: 'Name-1'}}, {id: {name: 'Name-2'}}];
625+
const parserType = 'TypeScript';
626+
627+
expect(() => {
628+
throwIfMoreThanOneModuleInterfaceParserError(
629+
nativeModuleName,
630+
moduleSpecs,
631+
parserType,
632+
);
633+
}).toThrow(MoreThanOneModuleInterfaceParserError);
634+
});
635+
});

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const {
2323
UnsupportedObjectPropertyValueTypeAnnotationParserError,
2424
UntypedModuleRegistryCallParserError,
2525
UnsupportedModulePropertyParserError,
26+
MoreThanOneModuleInterfaceParserError,
2627
} = require('./errors.js');
2728

2829
function throwIfModuleInterfaceIsMisnamed(
@@ -233,6 +234,21 @@ function throwIfPropertyValueTypeIsUnsupported(
233234
);
234235
}
235236

237+
function throwIfMoreThanOneModuleInterfaceParserError(
238+
nativeModuleName: string,
239+
moduleSpecs: $ReadOnlyArray<$FlowFixMe>,
240+
parserType: ParserType,
241+
) {
242+
if (moduleSpecs.length > 1) {
243+
throw new MoreThanOneModuleInterfaceParserError(
244+
nativeModuleName,
245+
moduleSpecs,
246+
moduleSpecs.map(node => node.id.name),
247+
parserType,
248+
);
249+
}
250+
}
251+
236252
module.exports = {
237253
throwIfModuleInterfaceIsMisnamed,
238254
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
@@ -244,4 +260,5 @@ module.exports = {
244260
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
245261
throwIfUntypedModule,
246262
throwIfModuleTypeIsUnsupported,
263+
throwIfMoreThanOneModuleInterfaceParserError,
247264
};

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ const {
5353
emitStringish,
5454
typeAliasResolution,
5555
} = require('../../parsers-primitives');
56+
5657
const {
57-
MoreThanOneModuleInterfaceParserError,
5858
UnnamedFunctionParamParserError,
5959
UnsupportedArrayElementTypeAnnotationParserError,
6060
UnsupportedGenericParserError,
@@ -65,6 +65,7 @@ const {
6565
UnsupportedObjectPropertyTypeAnnotationParserError,
6666
IncorrectModuleRegistryCallArgumentTypeParserError,
6767
} = require('../../errors.js');
68+
6869
const {verifyPlatforms} = require('../../utils');
6970

7071
const {
@@ -77,6 +78,7 @@ const {
7778
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
7879
throwIfUntypedModule,
7980
throwIfModuleTypeIsUnsupported,
81+
throwIfMoreThanOneModuleInterfaceParserError,
8082
} = require('../../error-utils');
8183

8284
const language = 'Flow';
@@ -586,14 +588,11 @@ function buildModuleSchema(
586588
language,
587589
);
588590

589-
if (moduleSpecs.length > 1) {
590-
throw new MoreThanOneModuleInterfaceParserError(
591-
hasteModuleName,
592-
moduleSpecs,
593-
moduleSpecs.map(node => node.id.name),
594-
language,
595-
);
596-
}
591+
throwIfMoreThanOneModuleInterfaceParserError(
592+
hasteModuleName,
593+
moduleSpecs,
594+
language,
595+
);
597596

598597
const [moduleSpec] = moduleSpecs;
599598

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ const {
5454
typeAliasResolution,
5555
} = require('../../parsers-primitives');
5656
const {
57-
MoreThanOneModuleInterfaceParserError,
5857
UnnamedFunctionParamParserError,
5958
UnsupportedArrayElementTypeAnnotationParserError,
6059
UnsupportedGenericParserError,
@@ -65,6 +64,7 @@ const {
6564
UnsupportedObjectPropertyTypeAnnotationParserError,
6665
IncorrectModuleRegistryCallArgumentTypeParserError,
6766
} = require('../../errors.js');
67+
6868
const {verifyPlatforms} = require('../../utils');
6969

7070
const {
@@ -75,6 +75,7 @@ const {
7575
throwIfModuleInterfaceNotFound,
7676
throwIfModuleInterfaceIsMisnamed,
7777
throwIfWrongNumberOfCallExpressionArgs,
78+
throwIfMoreThanOneModuleInterfaceParserError,
7879
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
7980
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
8081
} = require('../../error-utils');
@@ -600,14 +601,11 @@ function buildModuleSchema(
600601
language,
601602
);
602603

603-
if (moduleSpecs.length > 1) {
604-
throw new MoreThanOneModuleInterfaceParserError(
605-
hasteModuleName,
606-
moduleSpecs,
607-
moduleSpecs.map(node => node.id.name),
608-
language,
609-
);
610-
}
604+
throwIfMoreThanOneModuleInterfaceParserError(
605+
hasteModuleName,
606+
moduleSpecs,
607+
language,
608+
);
611609

612610
const [moduleSpec] = moduleSpecs;
613611

0 commit comments

Comments
 (0)