comments.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 != null && toRemove.length)) return list;
  39. const set = new Set(toRemove);
  40. return list.filter(el => {
  41. return !set.has(el);
  42. });
  43. }
  44. function addComment(type, content, line) {
  45. _addComment(this.node, type, content, line);
  46. }
  47. function addComments(type, comments) {
  48. _addComments(this.node, type, comments);
  49. }
  50. //# sourceMappingURL=comments.js.map