Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Codegen 96-97] Extract throwIfConfigNotfound and throwIfMoreThanOneConfig from findComponentConfig function #36719

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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