index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/linebreaks',
  12. fixable: true,
  13. };
  14. /** @type {import('stylelint').Rule} */
  15. const rule = (primary, _secondaryOptions, context) => {
  16. return (root, result) => {
  17. const validOptions = validateOptions(result, ruleName, {
  18. actual: primary,
  19. possible: ['unix', 'windows'],
  20. });
  21. if (!validOptions) {
  22. return;
  23. }
  24. const shouldHaveCR = primary === 'windows';
  25. if (context.fix) {
  26. root.walk((node) => {
  27. if ('selector' in node) {
  28. node.selector = fixData(node.selector);
  29. }
  30. if ('value' in node) {
  31. node.value = fixData(node.value);
  32. }
  33. if ('text' in node) {
  34. node.text = fixData(node.text);
  35. }
  36. if (node.raws.before) {
  37. node.raws.before = fixData(node.raws.before);
  38. }
  39. if (typeof node.raws.after === 'string') {
  40. node.raws.after = fixData(node.raws.after);
  41. }
  42. });
  43. if (typeof root.raws.after === 'string') {
  44. root.raws.after = fixData(root.raws.after);
  45. }
  46. } else {
  47. if (root.source == null) throw new Error('The root node must have a source');
  48. const lines = root.source.input.css.split('\n');
  49. for (let [i, line] of lines.entries()) {
  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;