Skip to content

Commit

Permalink
Chore/extract codegen parser more than one module exception (#34920)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Marcoo09 authored and facebook-github-bot committed Oct 19, 2022
1 parent 27cb501 commit f628edc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ const {
throwIfWrongNumberOfCallExpressionArgs,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
throwIfMoreThanOneModuleInterfaceParserError,
throwIfModuleTypeIsUnsupported,
throwIfUntypedModule,
} = require('../error-utils');
const {
UnsupportedModulePropertyParserError,
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleRegistryCallsParserError,
MisnamedModuleInterfaceParserError,
UnusedModuleInterfaceParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
UnsupportedFunctionReturnTypeAnnotationParserError,
UntypedModuleRegistryCallParserError,
MoreThanOneModuleInterfaceParserError,
} = require('../errors');

describe('throwIfModuleInterfaceIsMisnamed', () => {
Expand Down Expand Up @@ -484,8 +490,6 @@ describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => {
});

describe('throwIfUntypedModule', () => {
const {throwIfUntypedModule} = require('../error-utils');
const {UntypedModuleRegistryCallParserError} = require('../errors');
const hasteModuleName = 'moduleName';
const methodName = 'methodName';
const moduleName = 'moduleName';
Expand Down Expand Up @@ -527,8 +531,6 @@ describe('throwIfUntypedModule', () => {
});

describe('throwIfModuleTypeIsUnsupported', () => {
const {throwIfModuleTypeIsUnsupported} = require('../error-utils.js');
const {UnsupportedModulePropertyParserError} = require('../errors.js');
const hasteModuleName = 'moduleName';
const property = {value: 'value', key: {name: 'name'}};
it("don't throw error if module type is FunctionTypeAnnotation in Flow", () => {
Expand Down Expand Up @@ -602,3 +604,32 @@ describe('throwIfModuleTypeIsUnsupported', () => {
}).toThrow(UnsupportedModulePropertyParserError);
});
});

describe('throwIfMoreThanOneModuleInterfaceParserError', () => {
it("don't throw error if module specs length is <= 1", () => {
const nativeModuleName = 'moduleName';
const moduleSpecs = [];
const parserType = 'Flow';

expect(() => {
throwIfMoreThanOneModuleInterfaceParserError(
nativeModuleName,
moduleSpecs,
parserType,
);
}).not.toThrow(MoreThanOneModuleInterfaceParserError);
});
it('throw error if module specs is > 1 ', () => {
const nativeModuleName = 'moduleName';
const moduleSpecs = [{id: {name: 'Name-1'}}, {id: {name: 'Name-2'}}];
const parserType = 'TypeScript';

expect(() => {
throwIfMoreThanOneModuleInterfaceParserError(
nativeModuleName,
moduleSpecs,
parserType,
);
}).toThrow(MoreThanOneModuleInterfaceParserError);
});
});
17 changes: 17 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UntypedModuleRegistryCallParserError,
UnsupportedModulePropertyParserError,
MoreThanOneModuleInterfaceParserError,
} = require('./errors.js');

function throwIfModuleInterfaceIsMisnamed(
Expand Down Expand Up @@ -233,6 +234,21 @@ function throwIfPropertyValueTypeIsUnsupported(
);
}

function throwIfMoreThanOneModuleInterfaceParserError(
nativeModuleName: string,
moduleSpecs: $ReadOnlyArray<$FlowFixMe>,
parserType: ParserType,
) {
if (moduleSpecs.length > 1) {
throw new MoreThanOneModuleInterfaceParserError(
nativeModuleName,
moduleSpecs,
moduleSpecs.map(node => node.id.name),
parserType,
);
}
}

module.exports = {
throwIfModuleInterfaceIsMisnamed,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
Expand All @@ -244,4 +260,5 @@ module.exports = {
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUntypedModule,
throwIfModuleTypeIsUnsupported,
throwIfMoreThanOneModuleInterfaceParserError,
};
17 changes: 8 additions & 9 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const {
emitStringish,
typeAliasResolution,
} = require('../../parsers-primitives');

const {
MoreThanOneModuleInterfaceParserError,
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
UnsupportedGenericParserError,
Expand All @@ -65,6 +65,7 @@ const {
UnsupportedObjectPropertyTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

const {verifyPlatforms} = require('../../utils');

const {
Expand All @@ -77,6 +78,7 @@ const {
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUntypedModule,
throwIfModuleTypeIsUnsupported,
throwIfMoreThanOneModuleInterfaceParserError,
} = require('../../error-utils');

const language = 'Flow';
Expand Down Expand Up @@ -586,14 +588,11 @@ function buildModuleSchema(
language,
);

if (moduleSpecs.length > 1) {
throw new MoreThanOneModuleInterfaceParserError(
hasteModuleName,
moduleSpecs,
moduleSpecs.map(node => node.id.name),
language,
);
}
throwIfMoreThanOneModuleInterfaceParserError(
hasteModuleName,
moduleSpecs,
language,
);

const [moduleSpec] = moduleSpecs;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const {
typeAliasResolution,
} = require('../../parsers-primitives');
const {
MoreThanOneModuleInterfaceParserError,
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
UnsupportedGenericParserError,
Expand All @@ -65,6 +64,7 @@ const {
UnsupportedObjectPropertyTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

const {verifyPlatforms} = require('../../utils');

const {
Expand All @@ -75,6 +75,7 @@ const {
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
throwIfWrongNumberOfCallExpressionArgs,
throwIfMoreThanOneModuleInterfaceParserError,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
} = require('../../error-utils');
Expand Down Expand Up @@ -600,14 +601,11 @@ function buildModuleSchema(
language,
);

if (moduleSpecs.length > 1) {
throw new MoreThanOneModuleInterfaceParserError(
hasteModuleName,
moduleSpecs,
moduleSpecs.map(node => node.id.name),
language,
);
}
throwIfMoreThanOneModuleInterfaceParserError(
hasteModuleName,
moduleSpecs,
language,
);

const [moduleSpec] = moduleSpecs;

Expand Down

0 comments on commit f628edc

Please sign in to comment.