Skip to content

Commit 8fbcfce

Browse files
Antoine Doubovetzkyfacebook-github-bot
authored andcommitted
Extract throwIfConfigNotfound and throwIfMoreThanOneConfig from findComponentConfig function (#36719)
Summary: This PR contains tasks 96 and 97 from #34872: >[Codegen 96 - assigned to AntoineDoubovetzky] Create a throwIfConfigNotfound 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#L61-L63) and [TS](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/components/index.js#L62-L64) [Codegen 97 - assigned to AntoineDoubovetzky] Create a throwIfMoreThanOneConfig 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#L64-L66) and [TS](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/components/index.js#L65-L67) ## 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 throwIfConfigNotfound and throwIfMoreThanOneConfig from findComponentConfig function bypass-github-export-checks Pull Request resolved: #36719 Test Plan: I tested using Jest and Flow commands. Reviewed By: rshest Differential Revision: D44539681 Pulled By: cipolleschi fbshipit-source-id: c778ad1620d1c3f60b10c25c51efcb11173b3037
1 parent edc5ea1 commit 8fbcfce

File tree

4 files changed

+82
-12
lines changed

4 files changed

+82
-12
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
'use strict';
1313

14+
const {
15+
throwIfConfigNotfound,
16+
throwIfMoreThanOneConfig,
17+
} = require('../error-utils');
18+
1419
const {
1520
throwIfModuleInterfaceNotFound,
1621
throwIfMoreThanOneModuleRegistryCalls,
@@ -849,3 +854,54 @@ describe('throwIfMoreThanOneCodegenNativecommands', () => {
849854
}).not.toThrow();
850855
});
851856
});
857+
858+
describe('throwIfConfigNotfound', () => {
859+
it('throws an error if config is not found', () => {
860+
const configs: Array<{[string]: string}> = [];
861+
expect(() => {
862+
throwIfConfigNotfound(configs);
863+
}).toThrowError('Could not find component config for native component');
864+
});
865+
866+
it('does not throw an error if config contains some elements', () => {
867+
const configs: Array<{[string]: string}> = [
868+
{
869+
propsTypeName: 'testPropsTypeName',
870+
componentName: 'testComponentName',
871+
},
872+
];
873+
expect(() => {
874+
throwIfConfigNotfound(configs);
875+
}).not.toThrow();
876+
});
877+
});
878+
879+
describe('throwIfMoreThanOneConfig', () => {
880+
it('throws an error if config is not found', () => {
881+
const configs: Array<{[string]: string}> = [
882+
{
883+
propsTypeName: 'testPropsTypeName1',
884+
componentName: 'testComponentName1',
885+
},
886+
{
887+
propsTypeName: 'testPropsTypeName2',
888+
componentName: 'testComponentName2',
889+
},
890+
];
891+
expect(() => {
892+
throwIfMoreThanOneConfig(configs);
893+
}).toThrowError('Only one component is supported per file');
894+
});
895+
896+
it('does not throw an error if config contains some elements', () => {
897+
const configs: Array<{[string]: string}> = [
898+
{
899+
propsTypeName: 'testPropsTypeName',
900+
componentName: 'testComponentName',
901+
},
902+
];
903+
expect(() => {
904+
throwIfMoreThanOneConfig(configs);
905+
}).not.toThrow();
906+
});
907+
});

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,18 @@ function throwIfMoreThanOneCodegenNativecommands(
298298
}
299299
}
300300

301+
function throwIfConfigNotfound(foundConfigs: Array<{[string]: string}>) {
302+
if (foundConfigs.length === 0) {
303+
throw new Error('Could not find component config for native component');
304+
}
305+
}
306+
307+
function throwIfMoreThanOneConfig(foundConfigs: Array<{[string]: string}>) {
308+
if (foundConfigs.length > 1) {
309+
throw new Error('Only one component is supported per file');
310+
}
311+
}
312+
301313
module.exports = {
302314
throwIfModuleInterfaceIsMisnamed,
303315
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
@@ -316,4 +328,6 @@ module.exports = {
316328
throwIfPartialNotAnnotatingTypeParameter,
317329
throwIfPartialWithMoreParameter,
318330
throwIfMoreThanOneCodegenNativecommands,
331+
throwIfConfigNotfound,
332+
throwIfMoreThanOneConfig,
319333
};

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const {
2626
getOptions,
2727
getCommandTypeNameAndOptionsExpression,
2828
} = require('../../parsers-commons');
29+
const {
30+
throwIfConfigNotfound,
31+
throwIfMoreThanOneConfig,
32+
} = require('../../error-utils');
2933

3034
// $FlowFixMe[signature-verification-failure] there's no flowtype for AST
3135
function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
@@ -39,12 +43,8 @@ function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
3943
findNativeComponentType(statement, foundConfigs, parser);
4044
});
4145

42-
if (foundConfigs.length === 0) {
43-
throw new Error('Could not find component config for native component');
44-
}
45-
if (foundConfigs.length > 1) {
46-
throw new Error('Only one component is supported per file');
47-
}
46+
throwIfConfigNotfound(foundConfigs);
47+
throwIfMoreThanOneConfig(foundConfigs);
4848

4949
const foundConfig = foundConfigs[0];
5050

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const {
2727
getOptions,
2828
getCommandTypeNameAndOptionsExpression,
2929
} = require('../../parsers-commons');
30+
const {
31+
throwIfConfigNotfound,
32+
throwIfMoreThanOneConfig,
33+
} = require('../../error-utils');
3034

3135
// $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
3236
function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
@@ -40,12 +44,8 @@ function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
4044
findNativeComponentType(statement, foundConfigs, parser),
4145
);
4246

43-
if (foundConfigs.length === 0) {
44-
throw new Error('Could not find component config for native component');
45-
}
46-
if (foundConfigs.length > 1) {
47-
throw new Error('Only one component is supported per file');
48-
}
47+
throwIfConfigNotfound(foundConfigs);
48+
throwIfMoreThanOneConfig(foundConfigs);
4949

5050
const foundConfig = foundConfigs[0];
5151

0 commit comments

Comments
 (0)