isSharedLineComment.js 1.4 KB

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