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