From 320e51f4c41104f8fd612eb46a9c2ad26b3382c2 Mon Sep 17 00:00:00 2001 From: Marco Caldera Date: Tue, 14 Mar 2023 05:30:39 -0700 Subject: [PATCH] Unify findComponentConfig return statement (#36446) Summary: > [Codegen 100] Create a createComponentConfig function in the parser-commons.js file. It takes the foundConfig and the commandTypeNames as parameters and returns the component config object. Extract the return statements ([Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/components/index.js#L115-L126) [TS](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/components/index.js#L116-L127)) and use those implementations in that function. Part of Issue https://github.com/facebook/react-native/issues/34872 In case I should already add better typing I can update the PR while clarifying the type definitions. ## Changelog [INTERNAL] [CHANGED] - Move the return statement of `findComponentConfig` to `parsers-commons.js` merging Flow and TS implementation. Pull Request resolved: https://github.com/facebook/react-native/pull/36446 Test Plan: `yarn lint && yarn run flow && yarn test react-native-codegen` Reviewed By: cortinico Differential Revision: D44005899 Pulled By: rshest fbshipit-source-id: 19a4a05476156cbc2d824c9c32a7909c06a382ff --- .../parsers/__tests__/parsers-commons-test.js | 46 +++++++++++++++++++ .../src/parsers/flow/components/index.js | 13 +----- .../src/parsers/parsers-commons.js | 18 ++++++++ .../parsers/typescript/components/index.js | 13 +----- 4 files changed, 68 insertions(+), 22 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js b/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js index 7ab1c5d0787..872f1aa0624 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js @@ -20,6 +20,7 @@ import { buildSchemaFromConfigType, buildSchema, parseModuleName, + createComponentConfig, } from '../parsers-commons'; import type {ParserType} from '../errors'; @@ -1231,3 +1232,48 @@ describe('buildModuleSchema', () => { expect(schema).toEqual(schmeaMock); }); }); + +describe('createComponentConfig', () => { + const foundConfig = { + propsTypeName: 'testPropsTypeName', + componentName: 'testComponentName', + }; + + describe('when commandTypeNames contains an object as first element', () => { + it('returns expected config', () => { + const commandsTypeNames = [ + { + commandTypeName: 'testTypeName', + commandOptionsExpression: 'testOptionsExpression', + }, + ]; + + const expectedConfig = { + propsTypeName: 'testPropsTypeName', + componentName: 'testComponentName', + commandTypeName: 'testTypeName', + commandOptionsExpression: 'testOptionsExpression', + }; + + const configs = createComponentConfig(foundConfig, commandsTypeNames); + expect(configs).toEqual(expectedConfig); + }); + }); + + describe('when commandTypeNames is an empty array', () => { + it('returns the foundConfig and null for the command parameters', () => { + // $FlowFixMe[missing-empty-array-annot] + const commandsTypeNames = []; + + const expectedConfig = { + propsTypeName: 'testPropsTypeName', + componentName: 'testComponentName', + commandTypeName: null, + commandOptionsExpression: null, + }; + + const configs = createComponentConfig(foundConfig, commandsTypeNames); + expect(configs).toEqual(expectedConfig); + }); + }); +}); 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 e209ce4671f..286ab2ee97d 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/index.js +++ b/packages/react-native-codegen/src/parsers/flow/components/index.js @@ -20,6 +20,7 @@ const {getExtendsProps, removeKnownExtends} = require('./extends'); const {getCommandOptions, getOptions} = require('./options'); const {getProps} = require('./props'); const {getProperties} = require('./componentsUtils.js'); +const {createComponentConfig} = require('../../parsers-commons'); /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's * LTI update could not be added via codemod */ @@ -112,17 +113,7 @@ function findComponentConfig(ast) { throw new Error('codegenNativeCommands may only be called once in a file'); } - return { - ...foundConfig, - commandTypeName: - commandsTypeNames[0] == null - ? null - : commandsTypeNames[0].commandTypeName, - commandOptionsExpression: - commandsTypeNames[0] == null - ? null - : commandsTypeNames[0].commandOptionsExpression, - }; + return createComponentConfig(foundConfig, commandsTypeNames); } function getCommandProperties( diff --git a/packages/react-native-codegen/src/parsers/parsers-commons.js b/packages/react-native-codegen/src/parsers/parsers-commons.js index ae073d8c211..abef8705375 100644 --- a/packages/react-native-codegen/src/parsers/parsers-commons.js +++ b/packages/react-native-codegen/src/parsers/parsers-commons.js @@ -466,6 +466,23 @@ function buildSchema( ); } +function createComponentConfig( + foundConfig: $FlowFixMe, + commandsTypeNames: $FlowFixMe, +): $FlowFixMe { + return { + ...foundConfig, + commandTypeName: + commandsTypeNames[0] == null + ? null + : commandsTypeNames[0].commandTypeName, + commandOptionsExpression: + commandsTypeNames[0] == null + ? null + : commandsTypeNames[0].commandOptionsExpression, + }; +} + const parseModuleName = ( hasteModuleName: string, moduleSpec: $FlowFixMe, @@ -651,6 +668,7 @@ module.exports = { buildPropertySchema, buildSchemaFromConfigType, buildSchema, + createComponentConfig, parseModuleName, buildModuleSchema, }; 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 2adaddeceeb..f4a40add222 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/index.js @@ -21,6 +21,7 @@ const {categorizeProps} = require('./extends'); const {getCommandOptions, getOptions} = require('./options'); const {getProps} = require('./props'); const {getProperties} = require('./componentsUtils.js'); +const {createComponentConfig} = require('../../parsers-commons'); /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's * LTI update could not be added via codemod */ @@ -113,17 +114,7 @@ function findComponentConfig(ast) { throw new Error('codegenNativeCommands may only be called once in a file'); } - return { - ...foundConfig, - commandTypeName: - commandsTypeNames[0] == null - ? null - : commandsTypeNames[0].commandTypeName, - commandOptionsExpression: - commandsTypeNames[0] == null - ? null - : commandsTypeNames[0].commandOptionsExpression, - }; + return createComponentConfig(foundConfig, commandsTypeNames); } function getCommandProperties(