Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Comment thread
cipolleschi marked this conversation as resolved.
Outdated
* @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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const {
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleInterfaceParserError,
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
Expand All @@ -69,6 +68,8 @@ const {
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

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

const language = 'Flow';

function nullGuard<T>(fn: () => T): ?T {
Expand Down Expand Up @@ -582,13 +583,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
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const {
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
ModuleInterfaceNotFoundParserError,
MoreThanOneModuleInterfaceParserError,
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
Expand All @@ -68,6 +67,7 @@ const {
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
const {throwIfModuleInterfaceNotFound} = require('../../error-utils');

const language = 'TypeScript';

Expand Down Expand Up @@ -616,13 +616,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