index.js 2.7 KB

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