Skip to content

Commit 0d3f9c3

Browse files
committed
unify defaultExports.forEach(statement => ... (Flow, TS) to findNativeComponentType fn in parser-commons.js
1 parent aa1e5ed commit 0d3f9c3

File tree

2 files changed

+20
-64
lines changed
  • packages/react-native-codegen/src/parsers

2 files changed

+20
-64
lines changed

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

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,22 @@ 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');
2423
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
24+
const {
25+
createComponentConfig,
26+
findNativeComponentType,
27+
} = require('../../parsers-commons');
2528

26-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
27-
* LTI update could not be added via codemod */
28-
function findComponentConfig(ast) {
29-
const foundConfigs = [];
29+
// $FlowFixMe[signature-verification-failure] there's no flowtype for AST
30+
function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
31+
const foundConfigs: Array<{[string]: string}> = [];
3032

3133
const defaultExports = ast.body.filter(
3234
node => node.type === 'ExportDefaultDeclaration',
3335
);
3436

3537
defaultExports.forEach(statement => {
36-
let declaration = statement.declaration;
37-
38-
// codegenNativeComponent can be nested inside a cast
39-
// expression so we need to go one level deeper
40-
if (declaration.type === 'TypeCastExpression') {
41-
declaration = declaration.expression;
42-
}
43-
44-
try {
45-
if (declaration.callee.name === 'codegenNativeComponent') {
46-
const typeArgumentParams = declaration.typeArguments.params;
47-
const funcArgumentParams = declaration.arguments;
48-
49-
const nativeComponentType: {[string]: string} = {
50-
propsTypeName: typeArgumentParams[0].id.name,
51-
componentName: funcArgumentParams[0].value,
52-
};
53-
if (funcArgumentParams.length > 1) {
54-
nativeComponentType.optionsExpression = funcArgumentParams[1];
55-
}
56-
foundConfigs.push(nativeComponentType);
57-
}
58-
} catch (e) {
59-
// ignore
60-
}
38+
findNativeComponentType(statement, foundConfigs, parser);
6139
});
6240

6341
if (foundConfigs.length === 0) {
@@ -180,7 +158,7 @@ function buildComponentSchema(
180158
commandTypeName,
181159
commandOptionsExpression,
182160
optionsExpression,
183-
} = findComponentConfig(ast);
161+
} = findComponentConfig(ast, parser);
184162

185163
const types = parser.getTypes(ast);
186164

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

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,23 @@ 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');
2524
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
25+
const {
26+
createComponentConfig,
27+
findNativeComponentType,
28+
} = require('../../parsers-commons');
2629

27-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
28-
* LTI update could not be added via codemod */
29-
function findComponentConfig(ast) {
30-
const foundConfigs = [];
30+
// $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
31+
function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
32+
const foundConfigs: Array<{[string]: string}> = [];
3133

3234
const defaultExports = ast.body.filter(
3335
node => node.type === 'ExportDefaultDeclaration',
3436
);
3537

36-
defaultExports.forEach(statement => {
37-
let declaration = statement.declaration;
38-
39-
// codegenNativeComponent can be nested inside a cast
40-
// expression so we need to go one level deeper
41-
if (declaration.type === 'TSAsExpression') {
42-
declaration = declaration.expression;
43-
}
44-
45-
try {
46-
if (declaration.callee.name === 'codegenNativeComponent') {
47-
const typeArgumentParams = declaration.typeParameters.params;
48-
const funcArgumentParams = declaration.arguments;
49-
50-
const nativeComponentType: {[string]: string} = {
51-
propsTypeName: typeArgumentParams[0].typeName.name,
52-
componentName: funcArgumentParams[0].value,
53-
};
54-
if (funcArgumentParams.length > 1) {
55-
nativeComponentType.optionsExpression = funcArgumentParams[1];
56-
}
57-
foundConfigs.push(nativeComponentType);
58-
}
59-
} catch (e) {
60-
// ignore
61-
}
62-
});
38+
defaultExports.forEach(statement =>
39+
findNativeComponentType(statement, foundConfigs, parser),
40+
);
6341

6442
if (foundConfigs.length === 0) {
6543
throw new Error('Could not find component config for native component');
@@ -185,7 +163,7 @@ function buildComponentSchema(
185163
commandTypeName,
186164
commandOptionsExpression,
187165
optionsExpression,
188-
} = findComponentConfig(ast);
166+
} = findComponentConfig(ast, parser);
189167

190168
const types = parser.getTypes(ast);
191169

0 commit comments

Comments
 (0)