comments.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.addComment = addComment;
  6. exports.addComments = addComments;
  7. exports.shareCommentsWithSiblings = shareCommentsWithSiblings;
  8. var _t = require("@babel/types");
  9. const {
  10. addComment: _addComment,
  11. addComments: _addComments
  12. } = _t;
  13. function shareCommentsWithSiblings() {
  14. if (typeof this.key === "string") return;
  15. const node = this.node;
  16. if (!node) return;
  17. const trailing = node.trailingComments;
  18. const leading = node.leadingComments;
  19. if (!trailing && !leading) return;
  20. const prev = this.getSibling(this.key - 1);
  21. const next = this.getSibling(this.key + 1);
  22. const hasPrev = Boolean(prev.node);
  23. const hasNext = Boolean(next.node);
  24. if (hasPrev) {
  25. if (leading) {
  26. prev.addComments("trailing", removeIfExisting(leading, prev.node.trailingComments));
  27. }
  28. if (trailing && !hasNext) prev.addComments("trailing", trailing);
  29. }
  30. if (hasNext) {
  31. if (trailing) {
  32. next.addComments("leading", removeIfExisting(trailing, next.node.leadingComments));
  33. }
  34. if (leading && !hasPrev) next.addComments("leading", leading);
  35. }
  36. }
  37. function removeIfExisting(list, toRemove) {
  38. if (!toRemove) return list;
  39. let lastFoundIndex = -1;
  40. return list.filter(el => {
  41. const i = toRemove.indexOf(el, lastFoundIndex);
  42. if (i === -1) return true;
  43. lastFoundIndex = i;
  44. });
  45. }
  46. function addComment(type, content, line) {
  47. _addComment(this.node, type, content, line);
  48. }
  49. function addComments(type, comments) {
  50. _addComments(this.node, type, comments);
  51. }
  52. //# sourceMappingURL=comments.js.map