reportDisables.mjs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { SEVERITY_ERROR } from './constants.mjs';
  2. import reportCommentProblem from './utils/reportCommentProblem.mjs';
  3. /**
  4. * Returns a report describing which `results` (if any) contain disabled ranges
  5. * for rules that disallow disables via `reportDisables: true`.
  6. *
  7. * @param {import('stylelint').PostcssResult} postcssResult
  8. * @returns {void}
  9. */
  10. export default function reportDisables(postcssResult) {
  11. const rangeData = postcssResult.stylelint.disabledRanges;
  12. const configRules = postcssResult.stylelint.config?.rules;
  13. if (!configRules) return;
  14. // If no rules actually disallow disables, don't bother looking for ranges
  15. // that correspond to disabled rules.
  16. if (!Object.values(configRules).some((rule) => reportDisablesForRule(rule))) {
  17. return;
  18. }
  19. for (const [rule, ranges] of Object.entries(rangeData)) {
  20. for (const range of ranges) {
  21. if (!configRules[rule]) continue;
  22. if (!reportDisablesForRule(configRules[rule])) continue;
  23. reportCommentProblem({
  24. rule: 'reportDisables',
  25. message: `Rule "${rule}" may not be disabled`,
  26. severity: SEVERITY_ERROR,
  27. node: range.node,
  28. postcssResult,
  29. });
  30. }
  31. }
  32. }
  33. /**
  34. * @param {import('stylelint').ConfigRuleSettings<any, object>} options
  35. * @returns {boolean}
  36. */
  37. function reportDisablesForRule(options) {
  38. if (!options || !options[1]) return false;
  39. return Boolean(options[1].reportDisables);
  40. }