7ac7722288fdb92e5ef9a472a2875d65800e74ad3bdd9bf103d0bc9cdceb5e7df64b6b8f5ec4066c0029d65605ddf459eba913be8c00a099ae2510d687e7c1 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getFunctionName;
  6. var _index = require("../validators/generated/index.js");
  7. function getNameFromLiteralId(id) {
  8. if ((0, _index.isNullLiteral)(id)) {
  9. return "null";
  10. }
  11. if ((0, _index.isRegExpLiteral)(id)) {
  12. return `/${id.pattern}/${id.flags}`;
  13. }
  14. if ((0, _index.isTemplateLiteral)(id)) {
  15. return id.quasis.map(quasi => quasi.value.raw).join("");
  16. }
  17. if (id.value !== undefined) {
  18. return String(id.value);
  19. }
  20. return null;
  21. }
  22. function getObjectMemberKey(node) {
  23. if (!node.computed || (0, _index.isLiteral)(node.key)) {
  24. return node.key;
  25. }
  26. }
  27. function getFunctionName(node, parent) {
  28. if ("id" in node && node.id) {
  29. return {
  30. name: node.id.name,
  31. originalNode: node.id
  32. };
  33. }
  34. let prefix = "";
  35. let id;
  36. if ((0, _index.isObjectProperty)(parent, {
  37. value: node
  38. })) {
  39. id = getObjectMemberKey(parent);
  40. } else if ((0, _index.isObjectMethod)(node) || (0, _index.isClassMethod)(node)) {
  41. id = getObjectMemberKey(node);
  42. if (node.kind === "get") prefix = "get ";else if (node.kind === "set") prefix = "set ";
  43. } else if ((0, _index.isVariableDeclarator)(parent, {
  44. init: node
  45. })) {
  46. id = parent.id;
  47. } else if ((0, _index.isAssignmentExpression)(parent, {
  48. operator: "=",
  49. right: node
  50. })) {
  51. id = parent.left;
  52. }
  53. if (!id) return null;
  54. const name = (0, _index.isLiteral)(id) ? getNameFromLiteralId(id) : (0, _index.isIdentifier)(id) ? id.name : (0, _index.isPrivateName)(id) ? id.id.name : null;
  55. if (name == null) return null;
  56. return {
  57. name: prefix + name,
  58. originalNode: id
  59. };
  60. }
  61. //# sourceMappingURL=getFunctionName.js.map