Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | const _ = require('lodash'); |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame^] | 4 | const optionsMatches = require('./utils/optionsMatches'); |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 5 | const putIfAbsent = require('./utils/putIfAbsent'); |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame^] | 6 | const validateDisableSettings = require('./validateDisableSettings'); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 7 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 8 | /** @typedef {import('postcss/lib/comment')} PostcssComment */ |
| 9 | /** @typedef {import('stylelint').DisabledRange} DisabledRange */ |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 10 | /** @typedef {import('stylelint').RangeType} RangeType */ |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 11 | /** @typedef {import('stylelint').DisableReportRange} DisableReportRange */ |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * @param {import('stylelint').StylelintResult[]} results |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 15 | */ |
| 16 | module.exports = function (results) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 17 | results.forEach((result) => { |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame^] | 18 | const settings = validateDisableSettings(result._postcssResult, 'reportNeedlessDisables'); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 19 | |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame^] | 20 | if (!settings) return; |
Tim van der Lippe | cc71b28 | 2021-02-12 15:51:14 +0000 | [diff] [blame] | 21 | |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame^] | 22 | const [enabled, options, stylelintResult] = settings; |
Tim van der Lippe | cc71b28 | 2021-02-12 15:51:14 +0000 | [diff] [blame] | 23 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 24 | /** @type {{[ruleName: string]: Array<DisabledRange>}} */ |
Tim van der Lippe | cc71b28 | 2021-02-12 15:51:14 +0000 | [diff] [blame] | 25 | const rangeData = _.cloneDeep(stylelintResult.disabledRanges); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 26 | |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame^] | 27 | if (!rangeData) return; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 28 | |
Tim van der Lippe | cc71b28 | 2021-02-12 15:51:14 +0000 | [diff] [blame] | 29 | const disabledWarnings = stylelintResult.disabledWarnings || []; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 30 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 31 | // A map from `stylelint-disable` comments to the set of rules that |
| 32 | // are usefully disabled by each comment. We track this |
| 33 | // comment-by-comment rather than range-by-range because ranges that |
| 34 | // disable *all* rules are duplicated for each rule they apply to in |
| 35 | // practice. |
| 36 | /** @type {Map<PostcssComment, Set<string>>}} */ |
| 37 | const usefulDisables = new Map(); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 38 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 39 | for (const warning of disabledWarnings) { |
| 40 | const rule = warning.rule; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 41 | const ruleRanges = rangeData[rule]; |
| 42 | |
| 43 | if (ruleRanges) { |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 44 | for (const range of ruleRanges) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 45 | if (isWarningInRange(warning, range)) { |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 46 | putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 51 | for (const range of rangeData.all) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 52 | if (isWarningInRange(warning, range)) { |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 53 | putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 54 | } |
| 55 | } |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 56 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 57 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 58 | const rangeEntries = Object.entries(rangeData); |
| 59 | |
| 60 | // Get rid of the duplicated ranges for each `all` rule. We only care |
| 61 | // if the entire `all` rule is useful as a whole or not. |
| 62 | for (const range of rangeData.all) { |
| 63 | for (const [rule, ranges] of rangeEntries) { |
| 64 | if (rule === 'all') continue; |
| 65 | |
| 66 | _.remove(ranges, (otherRange) => range.comment === otherRange.comment); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | for (const [rule, ranges] of rangeEntries) { |
| 71 | for (const range of ranges) { |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame^] | 72 | if (enabled === optionsMatches(options, 'except', rule)) continue; |
| 73 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 74 | const useful = usefulDisables.get(range.comment) || new Set(); |
| 75 | |
| 76 | // Only emit a warning if this range's comment isn't useful for this rule. |
| 77 | // For the special rule "all", only emit a warning if it's not useful for |
Tim van der Lippe | cc71b28 | 2021-02-12 15:51:14 +0000 | [diff] [blame] | 78 | // *any* rules, because it covers all of them. |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 79 | if (rule === 'all' ? useful.size !== 0 : useful.has(rule)) continue; |
| 80 | |
| 81 | // If the comment doesn't have a location, we can't report a useful error. |
| 82 | // In practice we expect all comments to have locations, though. |
| 83 | if (!range.comment.source || !range.comment.source.start) continue; |
| 84 | |
| 85 | result.warnings.push({ |
| 86 | text: `Needless disable for "${rule}"`, |
| 87 | rule: '--report-needless-disables', |
| 88 | line: range.comment.source.start.line, |
| 89 | column: range.comment.source.start.column, |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame^] | 90 | severity: options.severity, |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 91 | }); |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 94 | }); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * @param {import('stylelint').DisabledWarning} warning |
| 99 | * @param {RangeType} range |
| 100 | * @return {boolean} |
| 101 | */ |
| 102 | function isWarningInRange(warning, range) { |
| 103 | const line = warning.line; |
| 104 | |
| 105 | // Need to check if range.end exist, because line number type cannot be compared to undefined |
| 106 | return ( |
| 107 | range.start <= line && |
| 108 | ((range.end !== undefined && range.end >= line) || range.end === undefined) |
| 109 | ); |
| 110 | } |