Skip to content

Commit 320e51f

Browse files
marcocalderafacebook-github-bot
authored andcommitted
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 #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: #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
1 parent dc7941d commit 320e51f

File tree

4 files changed

+68
-22
lines changed

4 files changed

+68
-22
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
buildSchemaFromConfigType,
2121
buildSchema,
2222
parseModuleName,
23+
createComponentConfig,
2324
} from '../parsers-commons';
2425
import type {ParserType} from '../errors';
2526

@@ -1231,3 +1232,48 @@ describe('buildModuleSchema', () => {
12311232
expect(schema).toEqual(schmeaMock);
12321233
});
12331234
});
1235+
1236+
describe('createComponentConfig', () => {
1237+
const foundConfig = {
1238+
propsTypeName: 'testPropsTypeName',
1239+
componentName: 'testComponentName',
1240+
};
1241+
1242+
describe('when commandTypeNames contains an object as first element', () => {
1243+
it('returns expected config', () => {
1244+
const commandsTypeNames = [
1245+
{
1246+
commandTypeName: 'testTypeName',
1247+
commandOptionsExpression: 'testOptionsExpression',
1248+
},
1249+
];
1250+
1251+
const expectedConfig = {
1252+
propsTypeName: 'testPropsTypeName',
1253+
componentName: 'testComponentName',
1254+
commandTypeName: 'testTypeName',
1255+
commandOptionsExpression: 'testOptionsExpression',
1256+
};
1257+
1258+
const configs = createComponentConfig(foundConfig, commandsTypeNames);
1259+
expect(configs).toEqual(expectedConfig);
1260+
});
1261+
});
1262+
1263+
describe('when commandTypeNames is an empty array', () => {
1264+
it('returns the foundConfig and null for the command parameters', () => {
1265+
// $FlowFixMe[missing-empty-array-annot]
1266+
const commandsTypeNames = [];
1267+
1268+
const expectedConfig = {
1269+
propsTypeName: 'testPropsTypeName',
1270+
componentName: 'testComponentName',
1271+
commandTypeName: null,
1272+
commandOptionsExpression: null,
1273+
};
1274+
1275+
const configs = createComponentConfig(foundConfig, commandsTypeNames);
1276+
expect(configs).toEqual(expectedConfig);
1277+
});
1278+
});
1279+
});

packages/react-native-codegen/src/parsers/flow/components/index.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {getExtendsProps, removeKnownExtends} = require('./extends');
2020
const {getCommandOptions, getOptions} = require('./options');
2121
const {getProps} = require('./props');
2222
const {getProperties} = require('./componentsUtils.js');
23+
const {createComponentConfig} = require('../../parsers-commons');
2324

2425
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
2526
* LTI update could not be added via codemod */
@@ -112,17 +113,7 @@ function findComponentConfig(ast) {
112113
throw new Error('codegenNativeCommands may only be called once in a file');
113114
}
114115

115-
return {
116-
...foundConfig,
117-
commandTypeName:
118-
commandsTypeNames[0] == null
119-
? null
120-
: commandsTypeNames[0].commandTypeName,
121-
commandOptionsExpression:
122-
commandsTypeNames[0] == null
123-
? null
124-
: commandsTypeNames[0].commandOptionsExpression,
125-
};
116+
return createComponentConfig(foundConfig, commandsTypeNames);
126117
}
127118

128119
function getCommandProperties(

packages/react-native-codegen/src/parsers/parsers-commons.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,23 @@ function buildSchema(
466466
);
467467
}
468468

469+
function createComponentConfig(
470+
foundConfig: $FlowFixMe,
471+
commandsTypeNames: $FlowFixMe,
472+
): $FlowFixMe {
473+
return {
474+
...foundConfig,
475+
commandTypeName:
476+
commandsTypeNames[0] == null
477+
? null
478+
: commandsTypeNames[0].commandTypeName,
479+
commandOptionsExpression:
480+
commandsTypeNames[0] == null
481+
? null
482+
: commandsTypeNames[0].commandOptionsExpression,
483+
};
484+
}
485+
469486
const parseModuleName = (
470487
hasteModuleName: string,
471488
moduleSpec: $FlowFixMe,
@@ -651,6 +668,7 @@ module.exports = {
651668
buildPropertySchema,
652669
buildSchemaFromConfigType,
653670
buildSchema,
671+
createComponentConfig,
654672
parseModuleName,
655673
buildModuleSchema,
656674
};

packages/react-native-codegen/src/parsers/typescript/components/index.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const {categorizeProps} = require('./extends');
2121
const {getCommandOptions, getOptions} = require('./options');
2222
const {getProps} = require('./props');
2323
const {getProperties} = require('./componentsUtils.js');
24+
const {createComponentConfig} = require('../../parsers-commons');
2425

2526
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
2627
* LTI update could not be added via codemod */
@@ -113,17 +114,7 @@ function findComponentConfig(ast) {
113114
throw new Error('codegenNativeCommands may only be called once in a file');
114115
}
115116

116-
return {
117-
...foundConfig,
118-
commandTypeName:
119-
commandsTypeNames[0] == null
120-
? null
121-
: commandsTypeNames[0].commandTypeName,
122-
commandOptionsExpression:
123-
commandsTypeNames[0] == null
124-
? null
125-
: commandsTypeNames[0].commandOptionsExpression,
126-
};
117+
return createComponentConfig(foundConfig, commandsTypeNames);
127118
}
128119

129120
function getCommandProperties(

0 commit comments

Comments
 (0)