e208667c4c540b9cdec65e7b06cf356e5bb9769238428ce8b3a3fbc8317de692287db19334f965b27f52cc97d035bcd88e077b1e71f4b915c44acf5c4cbbca 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = cloneNode;
  6. var _index = require("../definitions/index.js");
  7. var _index2 = require("../validators/generated/index.js");
  8. const {
  9. hasOwn
  10. } = {
  11. hasOwn: Function.call.bind(Object.prototype.hasOwnProperty)
  12. };
  13. function cloneIfNode(obj, deep, withoutLoc, commentsCache) {
  14. if (obj && typeof obj.type === "string") {
  15. return cloneNodeInternal(obj, deep, withoutLoc, commentsCache);
  16. }
  17. return obj;
  18. }
  19. function cloneIfNodeOrArray(obj, deep, withoutLoc, commentsCache) {
  20. if (Array.isArray(obj)) {
  21. return obj.map(node => cloneIfNode(node, deep, withoutLoc, commentsCache));
  22. }
  23. return cloneIfNode(obj, deep, withoutLoc, commentsCache);
  24. }
  25. function cloneNode(node, deep = true, withoutLoc = false) {
  26. return cloneNodeInternal(node, deep, withoutLoc, new Map());
  27. }
  28. function cloneNodeInternal(node, deep = true, withoutLoc = false, commentsCache) {
  29. if (!node) return node;
  30. const {
  31. type
  32. } = node;
  33. const newNode = {
  34. type: node.type
  35. };
  36. if ((0, _index2.isIdentifier)(node)) {
  37. newNode.name = node.name;
  38. if (hasOwn(node, "optional") && typeof node.optional === "boolean") {
  39. newNode.optional = node.optional;
  40. }
  41. if (hasOwn(node, "typeAnnotation")) {
  42. newNode.typeAnnotation = deep ? cloneIfNodeOrArray(node.typeAnnotation, true, withoutLoc, commentsCache) : node.typeAnnotation;
  43. }
  44. if (hasOwn(node, "decorators")) {
  45. newNode.decorators = deep ? cloneIfNodeOrArray(node.decorators, true, withoutLoc, commentsCache) : node.decorators;
  46. }
  47. } else if (!hasOwn(_index.NODE_FIELDS, type)) {
  48. throw new Error(`Unknown node type: "${type}"`);
  49. } else {
  50. for (const field of Object.keys(_index.NODE_FIELDS[type])) {
  51. if (hasOwn(node, field)) {
  52. if (deep) {
  53. newNode[field] = (0, _index2.isFile)(node) && field === "comments" ? maybeCloneComments(node.comments, deep, withoutLoc, commentsCache) : cloneIfNodeOrArray(node[field], true, withoutLoc, commentsCache);
  54. } else {
  55. newNode[field] = node[field];
  56. }
  57. }
  58. }
  59. }
  60. if (hasOwn(node, "loc")) {
  61. if (withoutLoc) {
  62. newNode.loc = null;
  63. } else {
  64. newNode.loc = node.loc;
  65. }
  66. }
  67. if (hasOwn(node, "leadingComments")) {
  68. newNode.leadingComments = maybeCloneComments(node.leadingComments, deep, withoutLoc, commentsCache);
  69. }
  70. if (hasOwn(node, "innerComments")) {
  71. newNode.innerComments = maybeCloneComments(node.innerComments, deep, withoutLoc, commentsCache);
  72. }
  73. if (hasOwn(node, "trailingComments")) {
  74. newNode.trailingComments = maybeCloneComments(node.trailingComments, deep, withoutLoc, commentsCache);
  75. }
  76. if (hasOwn(node, "extra")) {
  77. newNode.extra = Object.assign({}, node.extra);
  78. }
  79. return newNode;
  80. }
  81. function maybeCloneComments(comments, deep, withoutLoc, commentsCache) {
  82. if (!comments || !deep) {
  83. return comments;
  84. }
  85. return comments.map(comment => {
  86. const cache = commentsCache.get(comment);
  87. if (cache) return cache;
  88. const {
  89. type,
  90. value,
  91. loc
  92. } = comment;
  93. const ret = {
  94. type,
  95. value,
  96. loc
  97. };
  98. if (withoutLoc) {
  99. ret.loc = null;
  100. }
  101. commentsCache.set(comment, ret);
  102. return ret;
  103. });
  104. }
  105. //# sourceMappingURL=cloneNode.js.map