index.js 2.7 KB

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