index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict';
  2. const postcss = require('postcss');
  3. const report = require('../../utils/report');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const validateOptions = require('../../utils/validateOptions');
  6. const ruleName = 'linebreaks';
  7. const messages = ruleMessages(ruleName, {
  8. expected: (linebreak) => `Expected linebreak to be ${linebreak}`,
  9. });
  10. const meta = {
  11. url: 'https://stylelint.io/user-guide/rules/list/linebreaks',
  12. };
  13. /** @type {import('stylelint').Rule} */
  14. const rule = (primary, _secondaryOptions, context) => {
  15. return (root, result) => {
  16. const validOptions = validateOptions(result, ruleName, {
  17. actual: primary,
  18. possible: ['unix', 'windows'],
  19. });
  20. if (!validOptions) {
  21. return;
  22. }
  23. const shouldHaveCR = primary === 'windows';
  24. if (context.fix) {
  25. root.walk((node) => {
  26. if ('selector' in node) {
  27. node.selector = fixData(node.selector);
  28. }
  29. if ('value' in node) {
  30. node.value = fixData(node.value);
  31. }
  32. if ('text' in node) {
  33. node.text = fixData(node.text);
  34. }
  35. if (node.raws.before) {
  36. node.raws.before = fixData(node.raws.before);
  37. }
  38. if (typeof node.raws.after === 'string') {
  39. node.raws.after = fixData(node.raws.after);
  40. }
  41. });
  42. if (typeof root.raws.after === 'string') {
  43. root.raws.after = fixData(root.raws.after);
  44. }
  45. } else {
  46. if (root.source == null) throw new Error('The root node must have a source');
  47. const lines = root.source.input.css.split('\n');
  48. for (let i = 0; i < lines.length; i++) {
  49. let line = lines[i];
  50. if (i < lines.length - 1 && !line.includes('\r')) {
  51. line += '\n';
  52. }
  53. if (hasError(line)) {
  54. const lineNum = i + 1;
  55. const colNum = line.length;
  56. reportNewlineError(lineNum, colNum);
  57. }
  58. }
  59. }
  60. /**
  61. * @param {string} dataToCheck
  62. */
  63. function hasError(dataToCheck) {
  64. const hasNewlineToVerify = /[\r\n]/.test(dataToCheck);
  65. const hasCR = hasNewlineToVerify ? /\r/.test(dataToCheck) : false;
  66. return hasNewlineToVerify && hasCR !== shouldHaveCR;
  67. }
  68. /**
  69. * @param {string} data
  70. */
  71. function fixData(data) {
  72. if (data) {
  73. let res = data.replace(/\r/g, '');
  74. if (shouldHaveCR) {
  75. res = res.replace(/\n/g, '\r\n');
  76. }
  77. return res;
  78. }
  79. return data;
  80. }
  81. /**
  82. * @param {number} line
  83. * @param {number} column
  84. */
  85. function reportNewlineError(line, column) {
  86. // Creating a node manually helps us to point to empty lines.
  87. const node = postcss.rule({
  88. source: {
  89. start: { line, column, offset: 0 },
  90. input: new postcss.Input(''),
  91. },
  92. });
  93. report({
  94. message: messages.expected(primary),
  95. node,
  96. result,
  97. ruleName,
  98. });
  99. }
  100. };
  101. };
  102. rule.ruleName = ruleName;
  103. rule.messages = messages;
  104. rule.meta = meta;
  105. module.exports = rule;