descriptionlessDisables.mjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { RULE_NAME_ALL } from './constants.mjs';
  2. import optionsMatches from './utils/optionsMatches.mjs';
  3. import reportCommentProblem from './utils/reportCommentProblem.mjs';
  4. import validateDisableSettings from './validateDisableSettings.mjs';
  5. /**
  6. * @param {import('stylelint').PostcssResult} postcssResult
  7. * @returns {void}
  8. */
  9. export default function descriptionlessDisables(postcssResult) {
  10. const [enabled, options] = validateDisableSettings(
  11. postcssResult,
  12. 'reportDescriptionlessDisables',
  13. );
  14. if (!options) return;
  15. /** @type {Set<import('postcss').Node>} */
  16. const alreadyReported = new Set();
  17. for (const [rule, ruleRanges] of Object.entries(postcssResult.stylelint.disabledRanges)) {
  18. for (const range of ruleRanges) {
  19. if (range.description) continue;
  20. const node = range.node;
  21. if (alreadyReported.has(node)) continue;
  22. if (enabled === optionsMatches(options, 'except', rule)) {
  23. // An 'all' rule will get copied for each individual rule. If the
  24. // configuration is `[false, {except: ['specific-rule']}]`, we
  25. // don't want to report the copies that match except, so we record
  26. // the comment as already reported.
  27. if (!enabled && rule === RULE_NAME_ALL) alreadyReported.add(node);
  28. continue;
  29. }
  30. alreadyReported.add(node);
  31. reportCommentProblem({
  32. rule: '--report-descriptionless-disables',
  33. message: `Disable for "${rule}" is missing a description`,
  34. severity: options.severity,
  35. node,
  36. postcssResult,
  37. });
  38. }
  39. }
  40. }