Extract throwIfMoreThanOneCodegenNativecommands error in error-utils (#36407)

Summary:
Part of Codegen Umbrella Issue: https://github.com/facebook/react-native/issues/34872
> [Codegen 99] Extract the throwIfMoreThanOneCodegenNativecommands error in the error-utils.js file and extract the error code from [Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/components/index.js#L111-L133) and [TS](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/components/index.js#L112-L114)

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->
[Internal] [Changed] - Extract throwIfMoreThanOneCodegenNativecommands error in error-utils

Pull Request resolved: https://github.com/facebook/react-native/pull/36407

Test Plan: `yarn jest react-native-codegen`

Reviewed By: christophpurrer

Differential Revision: D43912403

Pulled By: cipolleschi

fbshipit-source-id: 1c51fc01465a1baa5f06ccea09c3342584bb82cc
This commit is contained in:
Tarun Chauhan 2023-03-15 07:14:26 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 6a395cb2d7
Коммит 347d6f8d89
4 изменённых файлов: 44 добавлений и 6 удалений

Просмотреть файл

@ -27,6 +27,7 @@ const {
throwIfArrayElementTypeAnnotationIsUnsupported,
throwIfPartialNotAnnotatingTypeParameter,
throwIfPartialWithMoreParameter,
throwIfMoreThanOneCodegenNativecommands,
} = require('../error-utils');
const {
UnsupportedModulePropertyParserError,
@ -818,3 +819,33 @@ describe('throwIfPartialWithMoreParameter', () => {
}).not.toThrowError();
});
});
describe('throwIfMoreThanOneCodegenNativecommands', () => {
it('throws an error if given more than one codegenNativeCommands', () => {
const commandsTypeNames = [
{
commandTypeName: '',
commandOptionsExpression: {},
},
{
commandTypeName: '',
commandOptionsExpression: {},
},
];
expect(() => {
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
}).toThrowError('codegenNativeCommands may only be called once in a file');
});
it('does not throw an error if given exactly one codegenNativeCommand', () => {
const commandsTypeNames = [
{
commandTypeName: '',
commandOptionsExpression: {},
},
];
expect(() => {
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
}).not.toThrow();
});
});

Просмотреть файл

@ -290,6 +290,14 @@ function throwIfPartialWithMoreParameter(typeAnnotation: $FlowFixMe) {
}
}
function throwIfMoreThanOneCodegenNativecommands(
commandsTypeNames: $ReadOnlyArray<$FlowFixMe>,
) {
if (commandsTypeNames.length > 1) {
throw new Error('codegenNativeCommands may only be called once in a file');
}
}
module.exports = {
throwIfModuleInterfaceIsMisnamed,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
@ -307,4 +315,5 @@ module.exports = {
throwIfIncorrectModuleRegistryCallArgument,
throwIfPartialNotAnnotatingTypeParameter,
throwIfPartialWithMoreParameter,
throwIfMoreThanOneCodegenNativecommands,
};

Просмотреть файл

@ -21,6 +21,7 @@ const {getCommandOptions, getOptions} = require('./options');
const {getProps} = require('./props');
const {getProperties} = require('./componentsUtils.js');
const {createComponentConfig} = require('../../parsers-commons');
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
@ -109,9 +110,7 @@ function findComponentConfig(ast) {
})
.filter(Boolean);
if (commandsTypeNames.length > 1) {
throw new Error('codegenNativeCommands may only be called once in a file');
}
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
return createComponentConfig(foundConfig, commandsTypeNames);
}

Просмотреть файл

@ -22,6 +22,7 @@ const {getCommandOptions, getOptions} = require('./options');
const {getProps} = require('./props');
const {getProperties} = require('./componentsUtils.js');
const {createComponentConfig} = require('../../parsers-commons');
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
@ -110,9 +111,7 @@ function findComponentConfig(ast) {
})
.filter(Boolean);
if (commandsTypeNames.length > 1) {
throw new Error('codegenNativeCommands may only be called once in a file');
}
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
return createComponentConfig(foundConfig, commandsTypeNames);
}