descriptionlessDisables.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. const optionsMatches = require('./utils/optionsMatches');
  3. const validateDisableSettings = require('./validateDisableSettings');
  4. /** @typedef {import('postcss').Comment} PostcssComment */
  5. /** @typedef {import('stylelint').DisableReportRange} DisableReportRange */
  6. /** @typedef {import('stylelint').DisableOptionsReport} StylelintDisableOptionsReport */
  7. /**
  8. * @param {import('stylelint').LintResult[]} results
  9. */
  10. module.exports = function descriptionlessDisables(results) {
  11. for (const result of results) {
  12. const settings = validateDisableSettings(
  13. result._postcssResult,
  14. 'reportDescriptionlessDisables',
  15. );
  16. if (!settings) continue;
  17. const [enabled, options, stylelintResult] = settings;
  18. /** @type {Set<PostcssComment>} */
  19. const alreadyReported = new Set();
  20. for (const [rule, ruleRanges] of Object.entries(stylelintResult.disabledRanges)) {
  21. for (const range of ruleRanges) {
  22. if (range.description) continue;
  23. if (alreadyReported.has(range.comment)) continue;
  24. if (enabled === optionsMatches(options, 'except', rule)) {
  25. // An 'all' rule will get copied for each individual rule. If the
  26. // configuration is `[false, {except: ['specific-rule']}]`, we
  27. // don't want to report the copies that match except, so we record
  28. // the comment as already reported.
  29. if (!enabled && rule === 'all') alreadyReported.add(range.comment);
  30. continue;
  31. }
  32. alreadyReported.add(range.comment);
  33. // If the comment doesn't have a location, we can't report a useful error.
  34. // In practice we expect all comments to have locations, though.
  35. if (!range.comment.source || !range.comment.source.start) continue;
  36. result.warnings.push({
  37. text: `Disable for "${rule}" is missing a description`,
  38. rule: '--report-descriptionless-disables',
  39. line: range.comment.source.start.line,
  40. column: range.comment.source.start.column,
  41. endLine: range.comment.source.end && range.comment.source.end.line,
  42. endColumn: range.comment.source.end && range.comment.source.end.column,
  43. severity: options.severity,
  44. });
  45. }
  46. }
  47. }
  48. };