addEmptyLineBefore.js 481 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. /**
  3. * Add an empty line before a node. Mutates the node.
  4. *
  5. * @template {import('postcss').ChildNode} T
  6. * @param {T} node
  7. * @param {string} newline
  8. * @returns {T}
  9. */
  10. module.exports = function addEmptyLineBefore(node, newline) {
  11. const { raws } = node;
  12. if (typeof raws.before !== 'string') {
  13. return node;
  14. }
  15. raws.before = !/\r?\n/.test(raws.before)
  16. ? newline.repeat(2) + raws.before
  17. : raws.before.replace(/(\r?\n)/, `${newline}$1`);
  18. return node;
  19. };