Skip to content

Commit aa1e5ed

Browse files
committed
add findNativeComponentType fn to parsers-commons.js
Add below fn which unifies the `defaultExports.forEach(statement => ..` behaviors between Flow & TS: - `findNativeComponentType` : This function is used to find the type of a native component provided the default exports statement from generated AST.
1 parent dd4da85 commit aa1e5ed

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,48 @@ const buildModuleSchema = (
657657
);
658658
};
659659

660+
/**
661+
* This function is used to find the type of a native component
662+
* provided the default exports statement from generated AST.
663+
* @param statement The statement to be parsed.
664+
* @param foundConfigs The 'mutable' array of configs that have been found.
665+
* @param parser The language parser to be used.
666+
* @returns void
667+
*/
668+
function findNativeComponentType(
669+
statement: $FlowFixMe,
670+
foundConfigs: Array<{[string]: string}>,
671+
parser: Parser,
672+
): void {
673+
let declaration = statement.declaration;
674+
675+
// codegenNativeComponent can be nested inside a cast
676+
// expression so we need to go one level deeper
677+
if (
678+
declaration.type === 'TSAsExpression' ||
679+
declaration.type === 'TypeCastExpression'
680+
) {
681+
declaration = declaration.expression;
682+
}
683+
684+
try {
685+
if (declaration.callee.name === 'codegenNativeComponent') {
686+
const typeArgumentParams =
687+
parser.getTypeArgumentParamsFromDeclaration(declaration);
688+
const funcArgumentParams = declaration.arguments;
689+
690+
const nativeComponentType: {[string]: string} =
691+
parser.getNativeComponentType(typeArgumentParams, funcArgumentParams);
692+
if (funcArgumentParams.length > 1) {
693+
nativeComponentType.optionsExpression = funcArgumentParams[1];
694+
}
695+
foundConfigs.push(nativeComponentType);
696+
}
697+
} catch (e) {
698+
// ignore
699+
}
700+
}
701+
660702
module.exports = {
661703
wrapModuleSchema,
662704
unwrapNullable,
@@ -671,4 +713,5 @@ module.exports = {
671713
createComponentConfig,
672714
parseModuleName,
673715
buildModuleSchema,
716+
findNativeComponentType,
674717
};

0 commit comments

Comments
 (0)