mediaFeatureColonSpaceChecker.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. const atRuleParamIndex = require('../utils/atRuleParamIndex');
  3. const report = require('../utils/report');
  4. const styleSearch = require('style-search');
  5. /**
  6. * @param {{
  7. * root: import('postcss').Root,
  8. * locationChecker: (args: { source: string, index: number, err: (message: string) => void }) => void,
  9. * fix: ((node: import('postcss').AtRule, index: number) => boolean) | null,
  10. * result: import('stylelint').PostcssResult,
  11. * checkedRuleName: string,
  12. * }} opts
  13. */
  14. module.exports = function mediaFeatureColonSpaceChecker(opts) {
  15. opts.root.walkAtRules(/^media$/i, (atRule) => {
  16. const params = atRule.raws.params ? atRule.raws.params.raw : atRule.params;
  17. styleSearch({ source: params, target: ':' }, (match) => {
  18. checkColon(params, match.startIndex, atRule);
  19. });
  20. });
  21. /**
  22. * @param {string} source
  23. * @param {number} index
  24. * @param {import('postcss').AtRule} node
  25. */
  26. function checkColon(source, index, node) {
  27. opts.locationChecker({
  28. source,
  29. index,
  30. err: (message) => {
  31. const colonIndex = index + atRuleParamIndex(node);
  32. if (opts.fix && opts.fix(node, colonIndex)) {
  33. return;
  34. }
  35. report({
  36. message,
  37. node,
  38. index: colonIndex,
  39. result: opts.result,
  40. ruleName: opts.checkedRuleName,
  41. });
  42. },
  43. });
  44. }
  45. };