react-diagram-schema
Version:
Parses React components from a file entry point and generates/writes a complete schema to a file
27 lines (23 loc) • 1.14 kB
JavaScript
// getInlineComponentDeclaratorPaths.js
function isInlineReactComponent(program_bodyPath) {
// 1) helper function: filter variable declarations to select React inline declared React components
const isInlineReactComponent = (path) => {
// WARNING: .get() is eager — it can be called before &&. Thus, conditional statements won't be resolved in the expected order.
if (!path.isVariableDeclaration()) {
return false;
}
// by separating the conditions with an if statement,
// we ensure that the "type" property is checked only when the previous condition is true
const inlineDeclaration = path.get("declarations")[0];
return (
(inlineDeclaration?.node.init.type === "ArrowFunctionExpression" ||
(inlineDeclaration?.node.init.type === "CallExpression" &&
inlineDeclaration?.node.init.callee.name === "forwardRef" &&
inlineDeclaration?.node.init.arguments[0].type ===
"ArrowFunctionExpression")) &&
/^[A-Z]/.test(inlineDeclaration.node.id.name)
);
};
return isInlineReactComponent(program_bodyPath);
}
module.exports = isInlineReactComponent;