reportDisables.cjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const constants = require('./constants.cjs');
  5. const reportCommentProblem = require('./utils/reportCommentProblem.cjs');
  6. /**
  7. * Returns a report describing which `results` (if any) contain disabled ranges
  8. * for rules that disallow disables via `reportDisables: true`.
  9. *
  10. * @param {import('stylelint').PostcssResult} postcssResult
  11. * @returns {void}
  12. */
  13. function reportDisables(postcssResult) {
  14. const rangeData = postcssResult.stylelint.disabledRanges;
  15. const configRules = postcssResult.stylelint.config?.rules;
  16. if (!configRules) return;
  17. // If no rules actually disallow disables, don't bother looking for ranges
  18. // that correspond to disabled rules.
  19. if (!Object.values(configRules).some((rule) => reportDisablesForRule(rule))) {
  20. return;
  21. }
  22. for (const [rule, ranges] of Object.entries(rangeData)) {
  23. for (const range of ranges) {
  24. if (!configRules[rule]) continue;
  25. if (!reportDisablesForRule(configRules[rule])) continue;
  26. reportCommentProblem({
  27. rule: 'reportDisables',
  28. message: `Rule "${rule}" may not be disabled`,
  29. severity: constants.SEVERITY_ERROR,
  30. node: range.node,
  31. postcssResult,
  32. });
  33. }
  34. }
  35. }
  36. /**
  37. * @param {import('stylelint').ConfigRuleSettings<any, object>} options
  38. * @returns {boolean}
  39. */
  40. function reportDisablesForRule(options) {
  41. if (!options || !options[1]) return false;
  42. return Boolean(options[1].reportDisables);
  43. }
  44. module.exports = reportDisables;