Skip to content

Commit

Permalink
chore: Extract codegen ModuleInterfaceNotFoundParserError in a separa…
Browse files Browse the repository at this point in the history
…te function (#34922)

Summary:
This PR extracts the codegen `ModuleInterfaceNotFoundParserError` exception into a separate function inside `error-utils.js` so that it can be used by both Flow and Typescript parsers as requested on #34872. This also adds unit tests to the new `throwIfModuleInterfaceNotFound` function.

## Changelog

[Internal] [Changed] - Extract codegen `ModuleInterfaceNotFoundParserError` in a separate function that can de used by Flow and TypeScript parsers

Pull Request resolved: #34922

Test Plan:
Run `yarn jest react-native-codegen` and ensure CI is green

![image](https://user-images.githubusercontent.com/11707729/194876017-8e98e3d2-4518-4cbe-b7f0-b77a54060b1e.png)

Reviewed By: dmytrorykun

Differential Revision: D40296533

Pulled By: cipolleschi

fbshipit-source-id: 205469b15337f6c3847936a6ef5427b5a9e08665
  • Loading branch information
gabrieldonadel authored and facebook-github-bot committed Oct 14, 2022
1 parent 8422557 commit c9338c4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 16 deletions.
@@ -0,0 +1,37 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
* @oncall react_native
*/

'use strict';

const {throwIfModuleInterfaceNotFound} = require('../error-utils');
const {ModuleInterfaceNotFoundParserError} = require('../errors');

describe('throwIfModuleInterfaceNotFound', () => {
it('throw error if there are zero module specs', () => {
const nativeModuleName = 'moduleName';
const specId = {name: 'Name'};
const parserType = 'TypeScript';

expect(() => {
throwIfModuleInterfaceNotFound(0, nativeModuleName, specId, parserType);
}).toThrow(ModuleInterfaceNotFoundParserError);
});

it("don't throw error if there is at least one module spec", () => {
const nativeModuleName = 'moduleName';
const specId = {name: 'Spec'};
const parserType = 'Flow';

expect(() => {
throwIfModuleInterfaceNotFound(1, nativeModuleName, specId, parserType);
}).not.toThrow(ModuleInterfaceNotFoundParserError);
});
});
33 changes: 33 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
@@ -0,0 +1,33 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

import type {ParserType} from './errors';
const {ModuleInterfaceNotFoundParserError} = require('./errors.js');

function throwIfModuleInterfaceNotFound(
numberOfModuleSpecs: number,
nativeModuleName: string,
ast: $FlowFixMe,
parserType: ParserType,
) {
if (numberOfModuleSpecs === 0) {
throw new ModuleInterfaceNotFoundParserError(
nativeModuleName,
ast,
parserType,
);
}
}

module.exports = {
throwIfModuleInterfaceNotFound,
};
16 changes: 8 additions & 8 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Expand Up @@ -50,7 +50,6 @@ const {
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleInterfaceParserError,
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
Expand All @@ -71,6 +70,8 @@ const {
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

const {throwIfModuleInterfaceNotFound} = require('../../error-utils');

const language = 'Flow';

function nullGuard<T>(fn: () => T): ?T {
Expand Down Expand Up @@ -578,13 +579,12 @@ function buildModuleSchema(
isModuleInterface,
);

if (moduleSpecs.length === 0) {
throw new ModuleInterfaceNotFoundParserError(
hasteModuleName,
ast,
language,
);
}
throwIfModuleInterfaceNotFound(
moduleSpecs.length,
hasteModuleName,
ast,
language,
);

if (moduleSpecs.length > 1) {
throw new MoreThanOneModuleInterfaceParserError(
Expand Down
Expand Up @@ -50,7 +50,6 @@ const {
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleInterfaceParserError,
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
Expand All @@ -70,6 +69,7 @@ const {
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
const {throwIfModuleInterfaceNotFound} = require('../../error-utils');

const language = 'TypeScript';

Expand Down Expand Up @@ -612,13 +612,12 @@ function buildModuleSchema(
isModuleInterface,
);

if (moduleSpecs.length === 0) {
throw new ModuleInterfaceNotFoundParserError(
hasteModuleName,
ast,
language,
);
}
throwIfModuleInterfaceNotFound(
moduleSpecs.length,
hasteModuleName,
ast,
language,
);

if (moduleSpecs.length > 1) {
throw new MoreThanOneModuleInterfaceParserError(
Expand Down

0 comments on commit c9338c4

Please sign in to comment.