react-docgen
Version:
A library to extract information from React components for documentation generation.
29 lines (28 loc) • 1.06 kB
JavaScript
import isReactCreateElementCall from './isReactCreateElementCall.js';
import isReactCloneElementCall from './isReactCloneElementCall.js';
import isReactChildrenElementCall from './isReactChildrenElementCall.js';
import findFunctionReturn from './findFunctionReturn.js';
const validPossibleStatelessComponentTypes = [
'ArrowFunctionExpression',
'FunctionDeclaration',
'FunctionExpression',
'ObjectMethod',
];
function isJSXElementOrReactCall(path) {
return (path.isJSXElement() ||
path.isJSXFragment() ||
(path.isCallExpression() &&
(isReactCreateElementCall(path) ||
isReactCloneElementCall(path) ||
isReactChildrenElementCall(path))));
}
/**
* Returns `true` if the path represents a function which returns a JSXElement
*/
export default function isStatelessComponent(path) {
if (!path.inType(...validPossibleStatelessComponentTypes)) {
return false;
}
const foundPath = findFunctionReturn(path, isJSXElementOrReactCall);
return Boolean(foundPath);
}