index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. const ruleMessages = require('../../utils/ruleMessages');
  3. const selectorCombinatorSpaceChecker = require('../selectorCombinatorSpaceChecker');
  4. const validateOptions = require('../../utils/validateOptions');
  5. const whitespaceChecker = require('../../utils/whitespaceChecker');
  6. const ruleName = 'selector-combinator-space-after';
  7. const messages = ruleMessages(ruleName, {
  8. expectedAfter: (combinator) => `Expected single space after "${combinator}"`,
  9. rejectedAfter: (combinator) => `Unexpected whitespace after "${combinator}"`,
  10. });
  11. const meta = {
  12. url: 'https://stylelint.io/user-guide/rules/list/selector-combinator-space-after',
  13. };
  14. /** @type {import('stylelint').Rule} */
  15. const rule = (primary, _secondaryOptions, context) => {
  16. const checker = whitespaceChecker('space', primary, messages);
  17. return (root, result) => {
  18. const validOptions = validateOptions(result, ruleName, {
  19. actual: primary,
  20. possible: ['always', 'never'],
  21. });
  22. if (!validOptions) {
  23. return;
  24. }
  25. selectorCombinatorSpaceChecker({
  26. root,
  27. result,
  28. locationChecker: checker.after,
  29. locationType: 'after',
  30. checkedRuleName: ruleName,
  31. fix: context.fix
  32. ? (combinator) => {
  33. if (primary === 'always') {
  34. combinator.spaces.after = ' ';
  35. return true;
  36. }
  37. if (primary === 'never') {
  38. combinator.spaces.after = '';
  39. return true;
  40. }
  41. return false;
  42. }
  43. : null,
  44. });
  45. };
  46. };
  47. rule.ruleName = ruleName;
  48. rule.messages = messages;
  49. rule.meta = meta;
  50. module.exports = rule;