Skip to content

Commit

Permalink
Extract throwIfConfigNotfound and throwIfMoreThanOneConfig from findC…
Browse files Browse the repository at this point in the history
…omponentConfig 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
  • Loading branch information
Antoine Doubovetzky authored and facebook-github-bot committed Apr 5, 2023
1 parent edc5ea1 commit 8fbcfce
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

'use strict';

const {
throwIfConfigNotfound,
throwIfMoreThanOneConfig,
} = require('../error-utils');

const {
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleRegistryCalls,
Expand Down Expand Up @@ -849,3 +854,54 @@ describe('throwIfMoreThanOneCodegenNativecommands', () => {
}).not.toThrow();
});
});

describe('throwIfConfigNotfound', () => {
it('throws an error if config is not found', () => {
const configs: Array<{[string]: string}> = [];
expect(() => {
throwIfConfigNotfound(configs);
}).toThrowError('Could not find component config for native component');
});

it('does not throw an error if config contains some elements', () => {
const configs: Array<{[string]: string}> = [
{
propsTypeName: 'testPropsTypeName',
componentName: 'testComponentName',
},
];
expect(() => {
throwIfConfigNotfound(configs);
}).not.toThrow();
});
});

describe('throwIfMoreThanOneConfig', () => {
it('throws an error if config is not found', () => {
const configs: Array<{[string]: string}> = [
{
propsTypeName: 'testPropsTypeName1',
componentName: 'testComponentName1',
},
{
propsTypeName: 'testPropsTypeName2',
componentName: 'testComponentName2',
},
];
expect(() => {
throwIfMoreThanOneConfig(configs);
}).toThrowError('Only one component is supported per file');
});

it('does not throw an error if config contains some elements', () => {
const configs: Array<{[string]: string}> = [
{
propsTypeName: 'testPropsTypeName',
componentName: 'testComponentName',
},
];
expect(() => {
throwIfMoreThanOneConfig(configs);
}).not.toThrow();
});
});
14 changes: 14 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ function throwIfMoreThanOneCodegenNativecommands(
}
}

function throwIfConfigNotfound(foundConfigs: Array<{[string]: string}>) {
if (foundConfigs.length === 0) {
throw new Error('Could not find component config for native component');
}
}

function throwIfMoreThanOneConfig(foundConfigs: Array<{[string]: string}>) {
if (foundConfigs.length > 1) {
throw new Error('Only one component is supported per file');
}
}

module.exports = {
throwIfModuleInterfaceIsMisnamed,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
Expand All @@ -316,4 +328,6 @@ module.exports = {
throwIfPartialNotAnnotatingTypeParameter,
throwIfPartialWithMoreParameter,
throwIfMoreThanOneCodegenNativecommands,
throwIfConfigNotfound,
throwIfMoreThanOneConfig,
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const {
getOptions,
getCommandTypeNameAndOptionsExpression,
} = require('../../parsers-commons');
const {
throwIfConfigNotfound,
throwIfMoreThanOneConfig,
} = require('../../error-utils');

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

if (foundConfigs.length === 0) {
throw new Error('Could not find component config for native component');
}
if (foundConfigs.length > 1) {
throw new Error('Only one component is supported per file');
}
throwIfConfigNotfound(foundConfigs);
throwIfMoreThanOneConfig(foundConfigs);

const foundConfig = foundConfigs[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const {
getOptions,
getCommandTypeNameAndOptionsExpression,
} = require('../../parsers-commons');
const {
throwIfConfigNotfound,
throwIfMoreThanOneConfig,
} = require('../../error-utils');

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

if (foundConfigs.length === 0) {
throw new Error('Could not find component config for native component');
}
if (foundConfigs.length > 1) {
throw new Error('Only one component is supported per file');
}
throwIfConfigNotfound(foundConfigs);
throwIfMoreThanOneConfig(foundConfigs);

const foundConfig = foundConfigs[0];

Expand Down

0 comments on commit 8fbcfce

Please sign in to comment.