isStatelessComponent.js 1.0 KB

12345678910111213141516171819202122232425262728
  1. import isReactCreateElementCall from './isReactCreateElementCall.js';
  2. import isReactCloneElementCall from './isReactCloneElementCall.js';
  3. import isReactChildrenElementCall from './isReactChildrenElementCall.js';
  4. import findFunctionReturn from './findFunctionReturn.js';
  5. const validPossibleStatelessComponentTypes = [
  6. 'ArrowFunctionExpression',
  7. 'FunctionDeclaration',
  8. 'FunctionExpression',
  9. 'ObjectMethod',
  10. ];
  11. function isJSXElementOrReactCall(path) {
  12. return (path.isJSXElement() ||
  13. path.isJSXFragment() ||
  14. (path.isCallExpression() &&
  15. (isReactCreateElementCall(path) ||
  16. isReactCloneElementCall(path) ||
  17. isReactChildrenElementCall(path))));
  18. }
  19. /**
  20. * Returns `true` if the path represents a function which returns a JSXElement
  21. */
  22. export default function isStatelessComponent(path) {
  23. if (!path.inType(...validPossibleStatelessComponentTypes)) {
  24. return false;
  25. }
  26. const foundPath = findFunctionReturn(path, isJSXElementOrReactCall);
  27. return Boolean(foundPath);
  28. }