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
This commit is contained in:
Marco Caldera
2023-03-14 05:30:39 -07:00
committed by Facebook GitHub Bot
parent dc7941d732
commit 320e51f4c4
4 changed files with 68 additions and 22 deletions
@@ -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);
});
});
});
@@ -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(
@@ -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,
};
@@ -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(