index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. const getDeclarationValue = require('../../utils/getDeclarationValue');
  3. const report = require('../../utils/report');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const setDeclarationValue = require('../../utils/setDeclarationValue');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const valueParser = require('postcss-value-parser');
  8. const { isNumber } = require('../../utils/validateTypes');
  9. const ruleName = 'function-max-empty-lines';
  10. const messages = ruleMessages(ruleName, {
  11. expected: (max) => `Expected no more than ${max} empty ${max === 1 ? 'line' : 'lines'}`,
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/function-max-empty-lines',
  15. fixable: true,
  16. };
  17. /**
  18. * @param {import('postcss').Declaration} decl
  19. */
  20. function placeIndexOnValueStart(decl) {
  21. if (decl.raws.between == null) throw new Error('`between` must be present');
  22. return decl.prop.length + decl.raws.between.length - 1;
  23. }
  24. /** @type {import('stylelint').Rule} */
  25. const rule = (primary, _secondaryOptions, context) => {
  26. const maxAdjacentNewlines = primary + 1;
  27. return (root, result) => {
  28. const validOptions = validateOptions(result, ruleName, {
  29. actual: primary,
  30. possible: isNumber,
  31. });
  32. if (!validOptions) {
  33. return;
  34. }
  35. const violatedCRLFNewLinesRegex = new RegExp(`(?:\r\n){${maxAdjacentNewlines + 1},}`);
  36. const violatedLFNewLinesRegex = new RegExp(`\n{${maxAdjacentNewlines + 1},}`);
  37. const allowedLFNewLinesString = context.fix ? '\n'.repeat(maxAdjacentNewlines) : '';
  38. const allowedCRLFNewLinesString = context.fix ? '\r\n'.repeat(maxAdjacentNewlines) : '';
  39. root.walkDecls((decl) => {
  40. if (!decl.value.includes('(')) {
  41. return;
  42. }
  43. const stringValue = getDeclarationValue(decl);
  44. /** @type {Array<[string, string]>} */
  45. const splittedValue = [];
  46. let sourceIndexStart = 0;
  47. valueParser(stringValue).walk((node) => {
  48. if (
  49. node.type !== 'function' /* ignore non functions */ ||
  50. node.value.length === 0 /* ignore sass lists */
  51. ) {
  52. return;
  53. }
  54. const stringifiedNode = valueParser.stringify(node);
  55. if (
  56. !violatedLFNewLinesRegex.test(stringifiedNode) &&
  57. !violatedCRLFNewLinesRegex.test(stringifiedNode)
  58. ) {
  59. return;
  60. }
  61. if (context.fix) {
  62. const newNodeString = stringifiedNode
  63. .replace(new RegExp(violatedLFNewLinesRegex, 'gm'), allowedLFNewLinesString)
  64. .replace(new RegExp(violatedCRLFNewLinesRegex, 'gm'), allowedCRLFNewLinesString);
  65. splittedValue.push([
  66. stringValue.slice(sourceIndexStart, node.sourceIndex),
  67. newNodeString,
  68. ]);
  69. sourceIndexStart = node.sourceIndex + stringifiedNode.length;
  70. } else {
  71. report({
  72. message: messages.expected(primary),
  73. node: decl,
  74. index: placeIndexOnValueStart(decl) + node.sourceIndex,
  75. result,
  76. ruleName,
  77. });
  78. }
  79. });
  80. if (context.fix && splittedValue.length > 0) {
  81. const updatedValue =
  82. splittedValue.reduce((acc, curr) => acc + curr[0] + curr[1], '') +
  83. stringValue.slice(sourceIndexStart);
  84. setDeclarationValue(decl, updatedValue);
  85. }
  86. });
  87. };
  88. };
  89. rule.ruleName = ruleName;
  90. rule.messages = messages;
  91. rule.meta = meta;
  92. module.exports = rule;