Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | /** @typedef {import('stylelint').RangeType} RangeType */ |
| 4 | /** @typedef {import('stylelint').UnusedRange} UnusedRange */ |
| 5 | /** @typedef {import('stylelint').StylelintDisableOptionsReport} StylelintDisableOptionsReport */ |
| 6 | |
| 7 | /** |
| 8 | * @param {import('stylelint').StylelintResult[]} results |
| 9 | * @param {import('stylelint').StylelintConfig|undefined} config |
| 10 | * @returns {StylelintDisableOptionsReport} |
| 11 | */ |
| 12 | module.exports = function (results, config = {}) { |
| 13 | /** @type {StylelintDisableOptionsReport} */ |
| 14 | const report = []; |
| 15 | const usedRules = new Set(Object.keys(config.rules || {})); |
| 16 | |
| 17 | usedRules.add('all'); |
| 18 | |
| 19 | results.forEach((result) => { |
| 20 | // File with `CssSyntaxError` have not `_postcssResult` |
| 21 | if (!result._postcssResult) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | /** @type {import('stylelint').StylelintDisableReportEntry} */ |
| 26 | const sourceReport = { source: result.source, ranges: [] }; |
| 27 | const rangeData = result._postcssResult.stylelint.disabledRanges; |
| 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 | |
| 40 | sourceReport.ranges.push({ |
| 41 | unusedRule: rule, |
| 42 | start: range.start, |
| 43 | end: range.end, |
| 44 | }); |
| 45 | }); |
| 46 | }); |
| 47 | |
| 48 | if (sourceReport.ranges.length > 0) { |
| 49 | report.push(sourceReport); |
| 50 | } |
| 51 | }); |
| 52 | |
| 53 | return report; |
| 54 | }; |