selectorListCommaWhitespaceChecker.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. const isStandardSyntaxRule = require('../utils/isStandardSyntaxRule');
  3. const report = require('../utils/report');
  4. const styleSearch = require('style-search');
  5. /**
  6. * @param {{
  7. * root: import('postcss').Root,
  8. * result: import('stylelint').PostcssResult,
  9. * locationChecker: (opts: { source: string, index: number, err: (msg: string) => void }) => void,
  10. * checkedRuleName: string,
  11. * fix: ((rule: import('postcss').Rule, index: number) => boolean) | null,
  12. * }} opts
  13. * @returns {void}
  14. */
  15. module.exports = function selectorListCommaWhitespaceChecker(opts) {
  16. opts.root.walkRules((rule) => {
  17. if (!isStandardSyntaxRule(rule)) {
  18. return;
  19. }
  20. const selector = rule.raws.selector ? rule.raws.selector.raw : rule.selector;
  21. styleSearch(
  22. {
  23. source: selector,
  24. target: ',',
  25. functionArguments: 'skip',
  26. },
  27. (match) => {
  28. checkDelimiter(selector, match.startIndex, rule);
  29. },
  30. );
  31. });
  32. /**
  33. * @param {string} source
  34. * @param {number} index
  35. * @param {import('postcss').Rule} node
  36. */
  37. function checkDelimiter(source, index, node) {
  38. opts.locationChecker({
  39. source,
  40. index,
  41. err: (message) => {
  42. if (opts.fix && opts.fix(node, index)) {
  43. return;
  44. }
  45. report({
  46. message,
  47. node,
  48. index,
  49. result: opts.result,
  50. ruleName: opts.checkedRuleName,
  51. });
  52. },
  53. });
  54. }
  55. };