Skip to content

Commit e240879

Browse files
Pranav-yadavfacebook-github-bot
authored andcommitted
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: #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
1 parent cf8184d commit e240879

File tree

3 files changed

+38
-76
lines changed

3 files changed

+38
-76
lines changed

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

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,12 @@ import type {ComponentSchemaBuilderConfig} from '../../schema.js';
1515
const {getCommands} = require('./commands');
1616
const {getEvents} = require('./events');
1717
const {getProperties} = require('./componentsUtils.js');
18-
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
1918
const {
20-
createComponentConfig,
21-
findNativeComponentType,
2219
propertyNames,
2320
getCommandOptions,
2421
getOptions,
25-
getCommandTypeNameAndOptionsExpression,
22+
findComponentConfig,
2623
} = require('../../parsers-commons');
27-
const {
28-
throwIfConfigNotfound,
29-
throwIfMoreThanOneConfig,
30-
} = require('../../error-utils');
31-
32-
// $FlowFixMe[signature-verification-failure] there's no flowtype for AST
33-
function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
34-
const foundConfigs: Array<{[string]: string}> = [];
35-
36-
const defaultExports = ast.body.filter(
37-
node => node.type === 'ExportDefaultDeclaration',
38-
);
39-
40-
defaultExports.forEach(statement => {
41-
findNativeComponentType(statement, foundConfigs, parser);
42-
});
43-
44-
throwIfConfigNotfound(foundConfigs);
45-
throwIfMoreThanOneConfig(foundConfigs);
46-
47-
const foundConfig = foundConfigs[0];
48-
49-
const namedExports = ast.body.filter(
50-
node => node.type === 'ExportNamedDeclaration',
51-
);
52-
53-
const commandsTypeNames = namedExports
54-
.map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
55-
.filter(Boolean);
56-
57-
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
58-
59-
return createComponentConfig(foundConfig, commandsTypeNames);
60-
}
6124

6225
function getCommandProperties(ast: $FlowFixMe, parser: Parser) {
6326
const {commandTypeName, commandOptionsExpression} = findComponentConfig(

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const {
4141
isModuleRegistryCall,
4242
verifyPlatforms,
4343
} = require('./utils');
44+
4445
const {
4546
throwIfPropertyValueTypeIsUnsupported,
4647
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
@@ -55,6 +56,9 @@ const {
5556
throwIfModuleInterfaceNotFound,
5657
throwIfMoreThanOneModuleInterfaceParserError,
5758
throwIfModuleInterfaceIsMisnamed,
59+
throwIfMoreThanOneCodegenNativecommands,
60+
throwIfConfigNotfound,
61+
throwIfMoreThanOneConfig,
5862
} = require('./error-utils');
5963

6064
const {
@@ -898,6 +902,37 @@ function getEventArgument(
898902
};
899903
}
900904

905+
/* $FlowFixMe[signature-verification-failure] there's no flowtype for AST.
906+
* TODO(T108222691): Use flow-types for @babel/parser */
907+
function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
908+
const foundConfigs: Array<{[string]: string}> = [];
909+
910+
const defaultExports = ast.body.filter(
911+
node => node.type === 'ExportDefaultDeclaration',
912+
);
913+
914+
defaultExports.forEach(statement => {
915+
findNativeComponentType(statement, foundConfigs, parser);
916+
});
917+
918+
throwIfConfigNotfound(foundConfigs);
919+
throwIfMoreThanOneConfig(foundConfigs);
920+
921+
const foundConfig = foundConfigs[0];
922+
923+
const namedExports = ast.body.filter(
924+
node => node.type === 'ExportNamedDeclaration',
925+
);
926+
927+
const commandsTypeNames = namedExports
928+
.map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
929+
.filter(Boolean);
930+
931+
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
932+
933+
return createComponentConfig(foundConfig, commandsTypeNames);
934+
}
935+
901936
module.exports = {
902937
wrapModuleSchema,
903938
unwrapNullable,
@@ -920,4 +955,5 @@ module.exports = {
920955
extendsForProp,
921956
buildPropSchema,
922957
getEventArgument,
958+
findComponentConfig,
923959
};

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

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,12 @@ const {getCommands} = require('./commands');
1616
const {getEvents} = require('./events');
1717
const {categorizeProps} = require('./extends');
1818
const {getProperties} = require('./componentsUtils.js');
19-
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
2019
const {
21-
createComponentConfig,
22-
findNativeComponentType,
2320
propertyNames,
2421
getCommandOptions,
2522
getOptions,
26-
getCommandTypeNameAndOptionsExpression,
23+
findComponentConfig,
2724
} = require('../../parsers-commons');
28-
const {
29-
throwIfConfigNotfound,
30-
throwIfMoreThanOneConfig,
31-
} = require('../../error-utils');
32-
33-
// $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
34-
function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
35-
const foundConfigs: Array<{[string]: string}> = [];
36-
37-
const defaultExports = ast.body.filter(
38-
node => node.type === 'ExportDefaultDeclaration',
39-
);
40-
41-
defaultExports.forEach(statement =>
42-
findNativeComponentType(statement, foundConfigs, parser),
43-
);
44-
45-
throwIfConfigNotfound(foundConfigs);
46-
throwIfMoreThanOneConfig(foundConfigs);
47-
48-
const foundConfig = foundConfigs[0];
49-
50-
const namedExports = ast.body.filter(
51-
node => node.type === 'ExportNamedDeclaration',
52-
);
53-
54-
const commandsTypeNames = namedExports
55-
.map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
56-
.filter(Boolean);
57-
58-
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
59-
60-
return createComponentConfig(foundConfig, commandsTypeNames);
61-
}
6225

6326
function getCommandProperties(ast: $FlowFixMe, parser: Parser) {
6427
const {commandTypeName, commandOptionsExpression} = findComponentConfig(

0 commit comments

Comments
 (0)