addEmptyLineAfter.js 597 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. /**
  3. * Add an empty line after a node. Mutates the node.
  4. *
  5. * @template {import('postcss').Rule | import('postcss').AtRule} T
  6. * @param {T} node
  7. * @param {string} newline
  8. * @returns {T}
  9. */
  10. module.exports = function addEmptyLineAfter(node, newline) {
  11. const { raws } = node;
  12. if (typeof raws.after !== 'string') {
  13. return node;
  14. }
  15. const spaces = raws.after.split(';');
  16. const after = spaces[spaces.length - 1] || '';
  17. if (!/\r?\n/.test(after)) {
  18. raws.after += newline.repeat(2);
  19. } else {
  20. raws.after = raws.after.replace(/(\r?\n)/, `${newline}$1`);
  21. }
  22. return node;
  23. };