Skip to content

Commit 347d6f8

Browse files
tarunrajputfacebook-github-bot
authored andcommitted
Extract throwIfMoreThanOneCodegenNativecommands error in error-utils (#36407)
Summary: Part of Codegen Umbrella Issue: #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: #36407 Test Plan: `yarn jest react-native-codegen` Reviewed By: christophpurrer Differential Revision: D43912403 Pulled By: cipolleschi fbshipit-source-id: 1c51fc01465a1baa5f06ccea09c3342584bb82cc
1 parent 6a395cb commit 347d6f8

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const {
2727
throwIfArrayElementTypeAnnotationIsUnsupported,
2828
throwIfPartialNotAnnotatingTypeParameter,
2929
throwIfPartialWithMoreParameter,
30+
throwIfMoreThanOneCodegenNativecommands,
3031
} = require('../error-utils');
3132
const {
3233
UnsupportedModulePropertyParserError,
@@ -818,3 +819,33 @@ describe('throwIfPartialWithMoreParameter', () => {
818819
}).not.toThrowError();
819820
});
820821
});
822+
823+
describe('throwIfMoreThanOneCodegenNativecommands', () => {
824+
it('throws an error if given more than one codegenNativeCommands', () => {
825+
const commandsTypeNames = [
826+
{
827+
commandTypeName: '',
828+
commandOptionsExpression: {},
829+
},
830+
{
831+
commandTypeName: '',
832+
commandOptionsExpression: {},
833+
},
834+
];
835+
expect(() => {
836+
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
837+
}).toThrowError('codegenNativeCommands may only be called once in a file');
838+
});
839+
840+
it('does not throw an error if given exactly one codegenNativeCommand', () => {
841+
const commandsTypeNames = [
842+
{
843+
commandTypeName: '',
844+
commandOptionsExpression: {},
845+
},
846+
];
847+
expect(() => {
848+
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
849+
}).not.toThrow();
850+
});
851+
});

packages/react-native-codegen/src/parsers/error-utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@ function throwIfPartialWithMoreParameter(typeAnnotation: $FlowFixMe) {
290290
}
291291
}
292292

293+
function throwIfMoreThanOneCodegenNativecommands(
294+
commandsTypeNames: $ReadOnlyArray<$FlowFixMe>,
295+
) {
296+
if (commandsTypeNames.length > 1) {
297+
throw new Error('codegenNativeCommands may only be called once in a file');
298+
}
299+
}
300+
293301
module.exports = {
294302
throwIfModuleInterfaceIsMisnamed,
295303
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
@@ -307,4 +315,5 @@ module.exports = {
307315
throwIfIncorrectModuleRegistryCallArgument,
308316
throwIfPartialNotAnnotatingTypeParameter,
309317
throwIfPartialWithMoreParameter,
318+
throwIfMoreThanOneCodegenNativecommands,
310319
};

packages/react-native-codegen/src/parsers/flow/components/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const {getCommandOptions, getOptions} = require('./options');
2121
const {getProps} = require('./props');
2222
const {getProperties} = require('./componentsUtils.js');
2323
const {createComponentConfig} = require('../../parsers-commons');
24+
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
2425

2526
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
2627
* LTI update could not be added via codemod */
@@ -109,9 +110,7 @@ function findComponentConfig(ast) {
109110
})
110111
.filter(Boolean);
111112

112-
if (commandsTypeNames.length > 1) {
113-
throw new Error('codegenNativeCommands may only be called once in a file');
114-
}
113+
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
115114

116115
return createComponentConfig(foundConfig, commandsTypeNames);
117116
}

packages/react-native-codegen/src/parsers/typescript/components/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {getCommandOptions, getOptions} = require('./options');
2222
const {getProps} = require('./props');
2323
const {getProperties} = require('./componentsUtils.js');
2424
const {createComponentConfig} = require('../../parsers-commons');
25+
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
2526

2627
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
2728
* LTI update could not be added via codemod */
@@ -110,9 +111,7 @@ function findComponentConfig(ast) {
110111
})
111112
.filter(Boolean);
112113

113-
if (commandsTypeNames.length > 1) {
114-
throw new Error('codegenNativeCommands may only be called once in a file');
115-
}
114+
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
116115

117116
return createComponentConfig(foundConfig, commandsTypeNames);
118117
}

0 commit comments

Comments
 (0)