Refactor: Extract findComponentConfig(...) to parsers-commons.js (#37547)

Summary:
This PR extracts the `findComponentConfig(...)` Flow and TS from the `index.js`'s files to the `parser-commons.js` file.

bypass-github-export-checks

## Changelog:

[INTERNAL][CHANGED] - Refactor: Extract `findComponentConfig(...)` from Flow & TS to `parsers-commons.js`

Pull Request resolved: https://github.com/facebook/react-native/pull/37547

Test Plan: - `yarn flow && yarn test packages/react-native-codegen`  → should be green.

Reviewed By: cortinico

Differential Revision: D46143481

Pulled By: cipolleschi

fbshipit-source-id: f9a456b1d58312422b17463ed2b60ee5fda16462
This commit is contained in:
Pranav Yadav
2023-05-25 02:59:23 -07:00
committed by Facebook GitHub Bot
parent cf8184d5ab
commit e240879057
3 changed files with 38 additions and 76 deletions
@@ -15,49 +15,12 @@ import type {ComponentSchemaBuilderConfig} from '../../schema.js';
const {getCommands} = require('./commands');
const {getEvents} = require('./events');
const {getProperties} = require('./componentsUtils.js');
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
const {
createComponentConfig,
findNativeComponentType,
propertyNames,
getCommandOptions,
getOptions,
getCommandTypeNameAndOptionsExpression,
findComponentConfig,
} = 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) {
const foundConfigs: Array<{[string]: string}> = [];
const defaultExports = ast.body.filter(
node => node.type === 'ExportDefaultDeclaration',
);
defaultExports.forEach(statement => {
findNativeComponentType(statement, foundConfigs, parser);
});
throwIfConfigNotfound(foundConfigs);
throwIfMoreThanOneConfig(foundConfigs);
const foundConfig = foundConfigs[0];
const namedExports = ast.body.filter(
node => node.type === 'ExportNamedDeclaration',
);
const commandsTypeNames = namedExports
.map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
.filter(Boolean);
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
return createComponentConfig(foundConfig, commandsTypeNames);
}
function getCommandProperties(ast: $FlowFixMe, parser: Parser) {
const {commandTypeName, commandOptionsExpression} = findComponentConfig(
@@ -41,6 +41,7 @@ const {
isModuleRegistryCall,
verifyPlatforms,
} = require('./utils');
const {
throwIfPropertyValueTypeIsUnsupported,
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
@@ -55,6 +56,9 @@ const {
throwIfModuleInterfaceNotFound,
throwIfMoreThanOneModuleInterfaceParserError,
throwIfModuleInterfaceIsMisnamed,
throwIfMoreThanOneCodegenNativecommands,
throwIfConfigNotfound,
throwIfMoreThanOneConfig,
} = require('./error-utils');
const {
@@ -898,6 +902,37 @@ function getEventArgument(
};
}
/* $FlowFixMe[signature-verification-failure] there's no flowtype for AST.
* TODO(T108222691): Use flow-types for @babel/parser */
function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
const foundConfigs: Array<{[string]: string}> = [];
const defaultExports = ast.body.filter(
node => node.type === 'ExportDefaultDeclaration',
);
defaultExports.forEach(statement => {
findNativeComponentType(statement, foundConfigs, parser);
});
throwIfConfigNotfound(foundConfigs);
throwIfMoreThanOneConfig(foundConfigs);
const foundConfig = foundConfigs[0];
const namedExports = ast.body.filter(
node => node.type === 'ExportNamedDeclaration',
);
const commandsTypeNames = namedExports
.map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
.filter(Boolean);
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
return createComponentConfig(foundConfig, commandsTypeNames);
}
module.exports = {
wrapModuleSchema,
unwrapNullable,
@@ -920,4 +955,5 @@ module.exports = {
extendsForProp,
buildPropSchema,
getEventArgument,
findComponentConfig,
};
@@ -16,49 +16,12 @@ const {getCommands} = require('./commands');
const {getEvents} = require('./events');
const {categorizeProps} = require('./extends');
const {getProperties} = require('./componentsUtils.js');
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
const {
createComponentConfig,
findNativeComponentType,
propertyNames,
getCommandOptions,
getOptions,
getCommandTypeNameAndOptionsExpression,
findComponentConfig,
} = 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) {
const foundConfigs: Array<{[string]: string}> = [];
const defaultExports = ast.body.filter(
node => node.type === 'ExportDefaultDeclaration',
);
defaultExports.forEach(statement =>
findNativeComponentType(statement, foundConfigs, parser),
);
throwIfConfigNotfound(foundConfigs);
throwIfMoreThanOneConfig(foundConfigs);
const foundConfig = foundConfigs[0];
const namedExports = ast.body.filter(
node => node.type === 'ExportNamedDeclaration',
);
const commandsTypeNames = namedExports
.map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
.filter(Boolean);
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
return createComponentConfig(foundConfig, commandsTypeNames);
}
function getCommandProperties(ast: $FlowFixMe, parser: Parser) {
const {commandTypeName, commandOptionsExpression} = findComponentConfig(