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