diff --git a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js index 0629a947cd..887fb852d6 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js @@ -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(); + }); +}); diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js index 12beb81450..d6d286ea3f 100644 --- a/packages/react-native-codegen/src/parsers/error-utils.js +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -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, }; diff --git a/packages/react-native-codegen/src/parsers/flow/components/index.js b/packages/react-native-codegen/src/parsers/flow/components/index.js index 286ab2ee97..28db0c3b0e 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/index.js +++ b/packages/react-native-codegen/src/parsers/flow/components/index.js @@ -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); } diff --git a/packages/react-native-codegen/src/parsers/typescript/components/index.js b/packages/react-native-codegen/src/parsers/typescript/components/index.js index f4a40add22..65539f7cfd 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/index.js @@ -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); }