Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | /** @typedef {import('stylelint').RangeType} RangeType */ |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 4 | |
| 5 | /** |
| 6 | * @param {import('stylelint').StylelintResult[]} results |
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 | module.exports = function (results) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 9 | results.forEach((result) => { |
| 10 | // File with `CssSyntaxError` have not `_postcssResult` |
| 11 | if (!result._postcssResult) { |
| 12 | return; |
| 13 | } |
| 14 | |
Tim van der Lippe | cc71b28 | 2021-02-12 15:51:14 +0000 | [diff] [blame] | 15 | const stylelintResult = result._postcssResult.stylelint; |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 16 | |
Tim van der Lippe | cc71b28 | 2021-02-12 15:51:14 +0000 | [diff] [blame] | 17 | if (!stylelintResult.config) return; // Linting error |
| 18 | |
| 19 | if (!stylelintResult.config.reportInvalidScopeDisables) return; |
| 20 | |
| 21 | const configRules = stylelintResult.config.rules || {}; |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 22 | |
| 23 | const usedRules = new Set(Object.keys(configRules)); |
| 24 | |
| 25 | usedRules.add('all'); |
| 26 | |
Tim van der Lippe | cc71b28 | 2021-02-12 15:51:14 +0000 | [diff] [blame] | 27 | const rangeData = stylelintResult.disabledRanges; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 28 | const disabledRules = Object.keys(rangeData); |
| 29 | |
| 30 | disabledRules.forEach((rule) => { |
| 31 | if (usedRules.has(rule)) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | rangeData[rule].forEach((range) => { |
| 36 | if (!range.strictStart && !range.strictEnd) { |
| 37 | return; |
| 38 | } |
| 39 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 40 | // If the comment doesn't have a location, we can't report a useful error. |
| 41 | // In practice we expect all comments to have locations, though. |
| 42 | if (!range.comment.source || !range.comment.source.start) return; |
| 43 | |
| 44 | result.warnings.push({ |
| 45 | text: `Rule "${rule}" isn't enabled`, |
| 46 | rule: '--report-invalid-scope-disables', |
| 47 | line: range.comment.source.start.line, |
| 48 | column: range.comment.source.start.column, |
| 49 | severity: 'error', |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 50 | }); |
| 51 | }); |
| 52 | }); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 53 | }); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 54 | }; |