Skip to content

Commit

Permalink
Extract throwIfMoreThanOneCodegenNativecommands error in error-utils (f…
Browse files Browse the repository at this point in the history
…acebook#36407)

Summary:
Part of Codegen Umbrella Issue: facebook#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: facebook#36407

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

Reviewed By: christophpurrer

Differential Revision: D43912403

Pulled By: cipolleschi

fbshipit-source-id: 1c51fc01465a1baa5f06ccea09c3342584bb82cc
  • Loading branch information
tarunrajput authored and OlimpiaZurek committed May 22, 2023
1 parent 63ad13e commit 51d9542
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
throwIfArrayElementTypeAnnotationIsUnsupported,
throwIfPartialNotAnnotatingTypeParameter,
throwIfPartialWithMoreParameter,
throwIfMoreThanOneCodegenNativecommands,
} = require('../error-utils');
const {
UnsupportedModulePropertyParserError,
Expand Down Expand Up @@ -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();
});
});
9 changes: 9 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -307,4 +315,5 @@ module.exports = {
throwIfIncorrectModuleRegistryCallArgument,
throwIfPartialNotAnnotatingTypeParameter,
throwIfPartialWithMoreParameter,
throwIfMoreThanOneCodegenNativecommands,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 51d9542

Please sign in to comment.