isSharedLineComment.cjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const typeGuards = require('./typeGuards.cjs');
  5. const getNextNonSharedLineCommentNode = require('./getNextNonSharedLineCommentNode.cjs');
  6. const getPreviousNonSharedLineCommentNode = require('./getPreviousNonSharedLineCommentNode.cjs');
  7. /** @typedef {import('postcss').Node} PostcssNode */
  8. /**
  9. * @param {PostcssNode | void} a
  10. * @param {PostcssNode | void} b
  11. */
  12. function nodesShareLines(a, b) {
  13. const endLine = a && a.source && a.source.end && a.source.end.line;
  14. const startLine = b && b.source && b.source.start && b.source.start.line;
  15. return endLine === startLine;
  16. }
  17. /**
  18. * @param {PostcssNode} node
  19. * @returns {boolean}
  20. */
  21. function isSharedLineComment(node) {
  22. if (!typeGuards.isComment(node)) {
  23. return false;
  24. }
  25. const previousNonSharedLineCommentNode = getPreviousNonSharedLineCommentNode(node);
  26. if (nodesShareLines(previousNonSharedLineCommentNode, node)) {
  27. return true;
  28. }
  29. const nextNonSharedLineCommentNode = getNextNonSharedLineCommentNode(node);
  30. if (nextNonSharedLineCommentNode && nodesShareLines(node, nextNonSharedLineCommentNode)) {
  31. return true;
  32. }
  33. const parentNode = node.parent;
  34. // It's a first child and located on the same line as block start
  35. if (
  36. parentNode !== undefined &&
  37. !typeGuards.isRoot(parentNode) &&
  38. parentNode.index(node) === 0 &&
  39. node.raws.before !== undefined &&
  40. !node.raws.before.includes('\n')
  41. ) {
  42. return true;
  43. }
  44. return false;
  45. }
  46. module.exports = isSharedLineComment;