Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame] | 3 | const optionsMatches = require('./utils/optionsMatches'); |
| 4 | const validateDisableSettings = require('./validateDisableSettings'); |
| 5 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 6 | /** @typedef {import('postcss').Comment} PostcssComment */ |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 7 | /** @typedef {import('stylelint').RangeType} RangeType */ |
| 8 | /** @typedef {import('stylelint').DisableReportRange} DisableReportRange */ |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 9 | /** @typedef {import('stylelint').DisableOptionsReport} StylelintDisableOptionsReport */ |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 10 | |
| 11 | /** |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 12 | * @param {import('stylelint').LintResult[]} results |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 13 | */ |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 14 | module.exports = function descriptionlessDisables(results) { |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 15 | results.forEach((result) => { |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame] | 16 | const settings = validateDisableSettings( |
| 17 | result._postcssResult, |
| 18 | 'reportDescriptionlessDisables', |
| 19 | ); |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 20 | |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame] | 21 | if (!settings) return; |
Tim van der Lippe | cc71b28 | 2021-02-12 15:51:14 +0000 | [diff] [blame] | 22 | |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame] | 23 | const [enabled, options, stylelintResult] = settings; |
Tim van der Lippe | cc71b28 | 2021-02-12 15:51:14 +0000 | [diff] [blame] | 24 | |
| 25 | const rangeData = stylelintResult.disabledRanges; |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 26 | |
| 27 | /** @type {Set<PostcssComment>} */ |
| 28 | const alreadyReported = new Set(); |
| 29 | |
| 30 | Object.keys(rangeData).forEach((rule) => { |
| 31 | rangeData[rule].forEach((range) => { |
| 32 | if (range.description) return; |
| 33 | |
| 34 | if (alreadyReported.has(range.comment)) return; |
| 35 | |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame] | 36 | if (enabled === optionsMatches(options, 'except', rule)) { |
| 37 | // An 'all' rule will get copied for each individual rule. If the |
| 38 | // configuration is `[false, {except: ['specific-rule']}]`, we |
| 39 | // don't want to report the copies that match except, so we record |
| 40 | // the comment as already reported. |
| 41 | if (!enabled && rule === 'all') alreadyReported.add(range.comment); |
| 42 | |
| 43 | return; |
| 44 | } |
| 45 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 46 | alreadyReported.add(range.comment); |
| 47 | |
| 48 | // If the comment doesn't have a location, we can't report a useful error. |
| 49 | // In practice we expect all comments to have locations, though. |
| 50 | if (!range.comment.source || !range.comment.source.start) return; |
| 51 | |
| 52 | result.warnings.push({ |
| 53 | text: `Disable for "${rule}" is missing a description`, |
| 54 | rule: '--report-descriptionless-disables', |
| 55 | line: range.comment.source.start.line, |
| 56 | column: range.comment.source.start.column, |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame] | 57 | severity: options.severity, |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 58 | }); |
| 59 | }); |
| 60 | }); |
| 61 | }); |
| 62 | }; |