index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. const hasBlock = require('../../utils/hasBlock');
  3. const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
  4. const rawNodeString = require('../../utils/rawNodeString');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const whitespaceChecker = require('../../utils/whitespaceChecker');
  9. const ruleName = 'at-rule-semicolon-space-before';
  10. const messages = ruleMessages(ruleName, {
  11. expectedBefore: () => 'Expected single space before ";"',
  12. rejectedBefore: () => 'Unexpected whitespace before ";"',
  13. });
  14. const meta = {
  15. url: 'https://stylelint.io/user-guide/rules/list/at-rule-semicolon-space-before',
  16. };
  17. /** @type {import('stylelint').Rule} */
  18. const rule = (primary) => {
  19. const checker = whitespaceChecker('space', primary, messages);
  20. return (root, result) => {
  21. const validOptions = validateOptions(result, ruleName, {
  22. actual: primary,
  23. possible: ['always', 'never'],
  24. });
  25. if (!validOptions) {
  26. return;
  27. }
  28. root.walkAtRules((atRule) => {
  29. if (hasBlock(atRule)) {
  30. return;
  31. }
  32. if (!isStandardSyntaxAtRule(atRule)) {
  33. return;
  34. }
  35. const nodeString = rawNodeString(atRule);
  36. checker.before({
  37. source: nodeString,
  38. index: nodeString.length,
  39. err: (m) => {
  40. report({
  41. message: m,
  42. node: atRule,
  43. index: nodeString.length - 1,
  44. result,
  45. ruleName,
  46. });
  47. },
  48. });
  49. });
  50. };
  51. };
  52. rule.ruleName = ruleName;
  53. rule.messages = messages;
  54. rule.meta = meta;
  55. module.exports = rule;