index.js 3.1 KB

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