getPreviousNonSharedLineCommentNode.js 842 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. /** @typedef {import('postcss').Node} Node */
  3. /**
  4. * @param {Node} node
  5. */
  6. function getNodeLine(node) {
  7. return node.source && node.source.start && node.source.start.line;
  8. }
  9. /**
  10. * @param {Node | undefined} node
  11. * @returns {Node | undefined}
  12. */
  13. module.exports = function getPreviousNonSharedLineCommentNode(node) {
  14. if (node === undefined) {
  15. return undefined;
  16. }
  17. const previousNode = node.prev();
  18. if (!previousNode || previousNode.type !== 'comment') {
  19. return previousNode;
  20. }
  21. if (getNodeLine(node) === getNodeLine(previousNode)) {
  22. return getPreviousNonSharedLineCommentNode(previousNode);
  23. }
  24. const previousNode2 = previousNode.prev();
  25. if (previousNode2 && getNodeLine(previousNode) === getNodeLine(previousNode2)) {
  26. return getPreviousNonSharedLineCommentNode(previousNode);
  27. }
  28. return previousNode;
  29. };