isSharedLineComment.mjs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { isComment, isRoot } from './typeGuards.mjs';
  2. import getNextNonSharedLineCommentNode from './getNextNonSharedLineCommentNode.mjs';
  3. import getPreviousNonSharedLineCommentNode from './getPreviousNonSharedLineCommentNode.mjs';
  4. /** @typedef {import('postcss').Node} PostcssNode */
  5. /**
  6. * @param {PostcssNode | void} a
  7. * @param {PostcssNode | void} b
  8. */
  9. function nodesShareLines(a, b) {
  10. const endLine = a && a.source && a.source.end && a.source.end.line;
  11. const startLine = b && b.source && b.source.start && b.source.start.line;
  12. return endLine === startLine;
  13. }
  14. /**
  15. * @param {PostcssNode} node
  16. * @returns {boolean}
  17. */
  18. export default function isSharedLineComment(node) {
  19. if (!isComment(node)) {
  20. return false;
  21. }
  22. const previousNonSharedLineCommentNode = getPreviousNonSharedLineCommentNode(node);
  23. if (nodesShareLines(previousNonSharedLineCommentNode, node)) {
  24. return true;
  25. }
  26. const nextNonSharedLineCommentNode = getNextNonSharedLineCommentNode(node);
  27. if (nextNonSharedLineCommentNode && nodesShareLines(node, nextNonSharedLineCommentNode)) {
  28. return true;
  29. }
  30. const parentNode = node.parent;
  31. // It's a first child and located on the same line as block start
  32. if (
  33. parentNode !== undefined &&
  34. !isRoot(parentNode) &&
  35. parentNode.index(node) === 0 &&
  36. node.raws.before !== undefined &&
  37. !node.raws.before.includes('\n')
  38. ) {
  39. return true;
  40. }
  41. return false;
  42. }