index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 { isNumber } = require('../../utils/validateTypes');
  8. const ruleName = 'value-list-max-empty-lines';
  9. const messages = ruleMessages(ruleName, {
  10. expected: (max) => `Expected no more than ${max} empty ${max === 1 ? 'line' : 'lines'}`,
  11. });
  12. const meta = {
  13. url: 'https://stylelint.io/user-guide/rules/value-list-max-empty-lines',
  14. fixable: true,
  15. deprecated: true,
  16. };
  17. /** @type {import('stylelint').Rule} */
  18. const rule = (primary, _secondaryOptions, context) => {
  19. const maxAdjacentNewlines = primary + 1;
  20. return (root, result) => {
  21. const validOptions = validateOptions(result, ruleName, {
  22. actual: primary,
  23. possible: isNumber,
  24. });
  25. if (!validOptions) {
  26. return;
  27. }
  28. const violatedCRLFNewLinesRegex = new RegExp(`(?:\r\n){${maxAdjacentNewlines + 1},}`);
  29. const violatedLFNewLinesRegex = new RegExp(`\n{${maxAdjacentNewlines + 1},}`);
  30. const allowedLFNewLinesString = context.fix ? '\n'.repeat(maxAdjacentNewlines) : '';
  31. const allowedCRLFNewLinesString = context.fix ? '\r\n'.repeat(maxAdjacentNewlines) : '';
  32. root.walkDecls((decl) => {
  33. const value = getDeclarationValue(decl);
  34. if (context.fix) {
  35. const newValueString = value
  36. .replace(new RegExp(violatedLFNewLinesRegex, 'gm'), allowedLFNewLinesString)
  37. .replace(new RegExp(violatedCRLFNewLinesRegex, 'gm'), allowedCRLFNewLinesString);
  38. setDeclarationValue(decl, newValueString);
  39. } else if (violatedLFNewLinesRegex.test(value) || violatedCRLFNewLinesRegex.test(value)) {
  40. report({
  41. message: messages.expected(primary),
  42. node: decl,
  43. index: 0,
  44. result,
  45. ruleName,
  46. });
  47. }
  48. });
  49. };
  50. };
  51. rule.ruleName = ruleName;
  52. rule.messages = messages;
  53. rule.meta = meta;
  54. module.exports = rule;