atRuleNameSpaceChecker.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. const isStandardSyntaxAtRule = require('../utils/isStandardSyntaxAtRule');
  3. const report = require('../utils/report');
  4. /**
  5. * @param {{
  6. * root: import('postcss').Root,
  7. * locationChecker: (opts: { source: string, index: number, err: (msg: string) => void, errTarget: string }) => void,
  8. * result: import('stylelint').PostcssResult,
  9. * checkedRuleName: string,
  10. * fix?: ((atRule: import('postcss').AtRule) => void) | null,
  11. * }} options
  12. */
  13. module.exports = function atRuleNameSpaceChecker(options) {
  14. options.root.walkAtRules((atRule) => {
  15. if (!isStandardSyntaxAtRule(atRule)) {
  16. return;
  17. }
  18. checkColon(
  19. `@${atRule.name}${atRule.raws.afterName || ''}${atRule.params}`,
  20. atRule.name.length,
  21. atRule,
  22. );
  23. });
  24. /**
  25. * @param {string} source
  26. * @param {number} index
  27. * @param {import('postcss').AtRule} node
  28. */
  29. function checkColon(source, index, node) {
  30. options.locationChecker({
  31. source,
  32. index,
  33. err: (m) => {
  34. if (options.fix) {
  35. options.fix(node);
  36. return;
  37. }
  38. report({
  39. message: m,
  40. node,
  41. index,
  42. result: options.result,
  43. ruleName: options.checkedRuleName,
  44. });
  45. },
  46. errTarget: `@${node.name}`,
  47. });
  48. }
  49. };