index.js 1.5 KB

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