resolveHOC.js 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. import isReactCreateClassCall from './isReactCreateClassCall.js';
  2. import isReactForwardRefCall from './isReactForwardRefCall.js';
  3. import resolveToValue from './resolveToValue.js';
  4. /**
  5. * If the path is a call expression, it recursively resolves to the
  6. * rightmost argument, stopping if it finds a React.createClass call expression
  7. *
  8. * Else the path itself is returned.
  9. */
  10. export default function resolveHOC(path) {
  11. if (path.isCallExpression() &&
  12. !isReactCreateClassCall(path) &&
  13. !isReactForwardRefCall(path)) {
  14. const node = path.node;
  15. const argumentLength = node.arguments.length;
  16. if (argumentLength && argumentLength > 0) {
  17. const args = path.get('arguments');
  18. const firstArg = args[0];
  19. // If the first argument is one of these types then the component might be the last argument
  20. // If there are all identifiers then we cannot figure out exactly and have to assume it is the first
  21. if (argumentLength > 1 &&
  22. (firstArg.isLiteral() ||
  23. firstArg.isObjectExpression() ||
  24. firstArg.isArrayExpression() ||
  25. firstArg.isSpreadElement())) {
  26. return resolveHOC(resolveToValue(args[argumentLength - 1]));
  27. }
  28. return resolveHOC(resolveToValue(firstArg));
  29. }
  30. }
  31. return path;
  32. }