getName.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. /**
  7. * Get the name of variable that contains node
  8. *
  9. * @param {Path} path to the node
  10. *
  11. * @return {String} The target
  12. */
  13. var _default = function _default(t) {
  14. return function (path) {
  15. var namedNode;
  16. path.find(function (path) {
  17. // const X = styled
  18. if (path.isAssignmentExpression()) {
  19. namedNode = path.node.left; // const X = { Y: styled }
  20. } else if (path.isObjectProperty()) {
  21. namedNode = path.node.key; // class Y { (static) X = styled }
  22. } else if (path.isClassProperty()) {
  23. namedNode = path.node.key; // let X; X = styled
  24. } else if (path.isVariableDeclarator()) {
  25. namedNode = path.node.id;
  26. } else if (path.isStatement()) {
  27. // we've hit a statement, we should stop crawling up
  28. return true;
  29. } // we've got an displayName (if we need it) no need to continue
  30. if (namedNode) return true;
  31. }); // foo.bar -> bar
  32. if (t.isMemberExpression(namedNode)) {
  33. namedNode = namedNode.property;
  34. } // identifiers are the only thing we can reliably get a name from
  35. return t.isIdentifier(namedNode) ? namedNode.name : undefined;
  36. };
  37. };
  38. exports.default = _default;