index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. const declarationValueIndex = require('../../utils/declarationValueIndex');
  3. const getDeclarationValue = require('../../utils/getDeclarationValue');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const setDeclarationValue = require('../../utils/setDeclarationValue');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const valueListCommaWhitespaceChecker = require('../valueListCommaWhitespaceChecker');
  8. const whitespaceChecker = require('../../utils/whitespaceChecker');
  9. const ruleName = 'value-list-comma-space-before';
  10. const messages = ruleMessages(ruleName, {
  11. expectedBefore: () => 'Expected single space before ","',
  12. rejectedBefore: () => 'Unexpected whitespace before ","',
  13. expectedBeforeSingleLine: () => 'Unexpected whitespace before "," in a single-line list',
  14. rejectedBeforeSingleLine: () => 'Unexpected whitespace before "," in a single-line list',
  15. });
  16. const meta = {
  17. url: 'https://stylelint.io/user-guide/rules/value-list-comma-space-before',
  18. fixable: true,
  19. deprecated: true,
  20. };
  21. /** @type {import('stylelint').Rule} */
  22. const rule = (primary, _secondaryOptions, context) => {
  23. const checker = whitespaceChecker('space', primary, messages);
  24. return (root, result) => {
  25. const validOptions = validateOptions(result, ruleName, {
  26. actual: primary,
  27. possible: ['always', 'never', 'always-single-line', 'never-single-line'],
  28. });
  29. if (!validOptions) {
  30. return;
  31. }
  32. /** @type {Map<import('postcss').Declaration, number[]> | undefined} */
  33. let fixData;
  34. valueListCommaWhitespaceChecker({
  35. root,
  36. result,
  37. locationChecker: checker.before,
  38. checkedRuleName: ruleName,
  39. fix: context.fix
  40. ? (declNode, index) => {
  41. const valueIndex = declarationValueIndex(declNode);
  42. if (index <= valueIndex) {
  43. return false;
  44. }
  45. fixData = fixData || new Map();
  46. const commaIndices = fixData.get(declNode) || [];
  47. commaIndices.push(index);
  48. fixData.set(declNode, commaIndices);
  49. return true;
  50. }
  51. : null,
  52. });
  53. if (fixData) {
  54. for (const [decl, commaIndices] of fixData.entries()) {
  55. for (const index of commaIndices.sort((a, b) => b - a)) {
  56. const value = getDeclarationValue(decl);
  57. const valueIndex = index - declarationValueIndex(decl);
  58. let beforeValue = value.slice(0, valueIndex);
  59. const afterValue = value.slice(valueIndex);
  60. if (primary.startsWith('always')) {
  61. beforeValue = beforeValue.replace(/\s*$/, ' ');
  62. } else if (primary.startsWith('never')) {
  63. beforeValue = beforeValue.replace(/\s*$/, '');
  64. }
  65. setDeclarationValue(decl, beforeValue + afterValue);
  66. }
  67. }
  68. }
  69. };
  70. };
  71. rule.ruleName = ruleName;
  72. rule.messages = messages;
  73. rule.meta = meta;
  74. module.exports = rule;