needlessDisables.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. const optionsMatches = require('./utils/optionsMatches');
  3. const putIfAbsent = require('./utils/putIfAbsent');
  4. const validateDisableSettings = require('./validateDisableSettings');
  5. /** @typedef {import('postcss').Comment} PostcssComment */
  6. /** @typedef {import('stylelint').DisabledRange} DisabledRange */
  7. /** @typedef {import('stylelint').RangeType} RangeType */
  8. /** @typedef {import('stylelint').DisableReportRange} DisableReportRange */
  9. /**
  10. * @param {import('stylelint').LintResult[]} results
  11. */
  12. module.exports = function needlessDisables(results) {
  13. for (const result of results) {
  14. const settings = validateDisableSettings(result._postcssResult, 'reportNeedlessDisables');
  15. if (!settings) continue;
  16. const [enabled, options, stylelintResult] = settings;
  17. const rangeData = stylelintResult.disabledRanges;
  18. if (!rangeData) continue;
  19. const disabledWarnings = stylelintResult.disabledWarnings || [];
  20. // A map from `stylelint-disable` comments to the set of rules that
  21. // are usefully disabled by each comment. We track this
  22. // comment-by-comment rather than range-by-range because ranges that
  23. // disable *all* rules are duplicated for each rule they apply to in
  24. // practice.
  25. /** @type {Map<PostcssComment, Set<string>>}} */
  26. const usefulDisables = new Map();
  27. for (const warning of disabledWarnings) {
  28. const rule = warning.rule;
  29. const ruleRanges = rangeData[rule];
  30. if (ruleRanges) {
  31. for (const range of ruleRanges) {
  32. if (isWarningInRange(warning, range)) {
  33. putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule);
  34. }
  35. }
  36. }
  37. for (const range of rangeData.all || []) {
  38. if (isWarningInRange(warning, range)) {
  39. putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule);
  40. }
  41. }
  42. }
  43. const allRangeComments = new Set((rangeData.all || []).map((range) => range.comment));
  44. for (const [rule, ranges] of Object.entries(rangeData)) {
  45. for (const range of ranges) {
  46. if (rule !== 'all' && allRangeComments.has(range.comment)) continue;
  47. if (enabled === optionsMatches(options, 'except', rule)) continue;
  48. const useful = usefulDisables.get(range.comment) || new Set();
  49. // Only emit a warning if this range's comment isn't useful for this rule.
  50. // For the special rule "all", only emit a warning if it's not useful for
  51. // *any* rules, because it covers all of them.
  52. if (rule === 'all' ? useful.size !== 0 : useful.has(rule)) continue;
  53. // If the comment doesn't have a location, we can't report a useful error.
  54. // In practice we expect all comments to have locations, though.
  55. if (!range.comment.source || !range.comment.source.start) continue;
  56. result.warnings.push({
  57. text: `Needless disable for "${rule}"`,
  58. rule: '--report-needless-disables',
  59. line: range.comment.source.start.line,
  60. column: range.comment.source.start.column,
  61. endLine: range.comment.source.end && range.comment.source.end.line,
  62. endColumn: range.comment.source.end && range.comment.source.end.column,
  63. severity: options.severity,
  64. });
  65. }
  66. }
  67. }
  68. };
  69. /**
  70. * @param {import('stylelint').DisabledWarning} warning
  71. * @param {RangeType} range
  72. * @return {boolean}
  73. */
  74. function isWarningInRange(warning, range) {
  75. const line = warning.line;
  76. // Need to check if range.end exist, because line number type cannot be compared to undefined
  77. return (
  78. range.start <= line &&
  79. ((range.end !== undefined && range.end >= line) || range.end === undefined)
  80. );
  81. }