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,55 @@
/**
* 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';

describe('throwIfUntypedModule', () => {
const {throwIfUntypedModule} = require('../error-utils');
const {UntypedModuleRegistryCallParserError} = require('../errors');
const hasteModuleName = 'moduleName';
const methodName = 'methodName';
const moduleName = 'moduleName';
const callExpressions = [];

it('should throw error if module does not have a type', () => {
const typeArguments = null;
const language = 'Flow';
expect(() =>
throwIfUntypedModule(
typeArguments,
hasteModuleName,
callExpressions,
methodName,
moduleName,
language,
),
).toThrowError(UntypedModuleRegistryCallParserError);
});

it('should not throw error if module have a type', () => {
const typeArguments = {
type: 'TSTypeParameterInstantiations',
params: [],
};

const language = 'TypeScript';
expect(() =>
throwIfUntypedModule(
typeArguments,
hasteModuleName,
callExpressions,
methodName,
moduleName,
language,
),
).not.toThrowError(UntypedModuleRegistryCallParserError);
});
});
38 changes: 38 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,38 @@
/**
* 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 {UntypedModuleRegistryCallParserError} = require('./errors');

function throwIfUntypedModule(
typeArguments: $FlowFixMe,
hasteModuleName: string,
callExpression: $FlowFixMe,
methodName: string,
$moduleName: string,
language: ParserType,
) {
if (typeArguments == null) {
throw new UntypedModuleRegistryCallParserError(
hasteModuleName,
callExpression,
methodName,
$moduleName,
language,
);
}
}

module.exports = {
throwIfUntypedModule,
};
19 changes: 9 additions & 10 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ const {
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UnusedModuleInterfaceParserError,
MoreThanOneModuleRegistryCallsParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
const {throwIfUntypedModule} = require('../../error-utils');

const language = 'Flow';

Expand Down Expand Up @@ -663,15 +663,14 @@ function buildModuleSchema(

const $moduleName = callExpression.arguments[0].value;

if (typeArguments == null) {
throw new UntypedModuleRegistryCallParserError(
hasteModuleName,
callExpression,
methodName,
$moduleName,
language,
);
}
throwIfUntypedModule(
typeArguments,
hasteModuleName,
callExpression,
methodName,
$moduleName,
language,
);

if (
typeArguments.type !== 'TypeParameterInstantiation' ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ const {
UnsupportedObjectPropertyValueTypeAnnotationParserError,
UnusedModuleInterfaceParserError,
MoreThanOneModuleRegistryCallsParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
const {throwIfUntypedModule} = require('../../error-utils');

const language = 'TypeScript';

Expand Down Expand Up @@ -697,15 +697,14 @@ function buildModuleSchema(

const $moduleName = callExpression.arguments[0].value;

if (typeParameters == null) {
throw new UntypedModuleRegistryCallParserError(
hasteModuleName,
callExpression,
methodName,
$moduleName,
language,
);
}
throwIfUntypedModule(
typeParameters,
hasteModuleName,
callExpression,
methodName,
$moduleName,
language,
);

if (
typeParameters.type !== 'TSTypeParameterInstantiation' ||
Expand Down