index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const declarationColonSpaceChecker = require('../declarationColonSpaceChecker');
  3. const declarationValueIndex = require('../../utils/declarationValueIndex');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const validateOptions = require('../../utils/validateOptions');
  6. const whitespaceChecker = require('../../utils/whitespaceChecker');
  7. const ruleName = 'declaration-colon-space-after';
  8. const messages = ruleMessages(ruleName, {
  9. expectedAfter: () => 'Expected single space after ":"',
  10. rejectedAfter: () => 'Unexpected whitespace after ":"',
  11. expectedAfterSingleLine: () => 'Expected single space after ":" with a single-line declaration',
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/list/declaration-colon-space-after',
  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', 'always-single-line'],
  23. });
  24. if (!validOptions) {
  25. return;
  26. }
  27. declarationColonSpaceChecker({
  28. root,
  29. result,
  30. locationChecker: checker.after,
  31. checkedRuleName: ruleName,
  32. fix: context.fix
  33. ? (decl, index) => {
  34. const colonIndex = index - declarationValueIndex(decl);
  35. const between = decl.raws.between;
  36. if (between == null) throw new Error('`between` must be present');
  37. if (primary.startsWith('always')) {
  38. decl.raws.between =
  39. between.slice(0, colonIndex) + between.slice(colonIndex).replace(/^:\s*/, ': ');
  40. return true;
  41. }
  42. if (primary === 'never') {
  43. decl.raws.between =
  44. between.slice(0, colonIndex) + between.slice(colonIndex).replace(/^:\s*/, ':');
  45. return true;
  46. }
  47. return false;
  48. }
  49. : null,
  50. });
  51. };
  52. };
  53. rule.ruleName = ruleName;
  54. rule.messages = messages;
  55. rule.meta = meta;
  56. module.exports = rule;