descriptionlessDisables.js 2.1 KB

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