From e2408790577064c2f4977ed35bf64cb593a730d7 Mon Sep 17 00:00:00 2001 From: Pranav Yadav Date: Thu, 25 May 2023 02:59:23 -0700 Subject: [PATCH] Refactor: Extract `findComponentConfig(...)` to `parsers-commons.js` (#37547) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../src/parsers/flow/components/index.js | 39 +------------------ .../src/parsers/parsers-commons.js | 36 +++++++++++++++++ .../parsers/typescript/components/index.js | 39 +------------------ 3 files changed, 38 insertions(+), 76 deletions(-) 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 b424a1c9338..d5a9b6ed12a 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/index.js +++ b/packages/react-native-codegen/src/parsers/flow/components/index.js @@ -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( diff --git a/packages/react-native-codegen/src/parsers/parsers-commons.js b/packages/react-native-codegen/src/parsers/parsers-commons.js index ab65836feb7..4ebe10fac4f 100644 --- a/packages/react-native-codegen/src/parsers/parsers-commons.js +++ b/packages/react-native-codegen/src/parsers/parsers-commons.js @@ -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, }; 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 6bf6e82c879..b889dfe2223 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/index.js @@ -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(