UNPKG

1.93 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4
5/** @typedef {import('stylelint').RangeType} RangeType */
6/** @typedef {import('stylelint').DisableReportRange} DisabledRange */
7/** @typedef {import('stylelint').StylelintDisableOptionsReport} StylelintDisableOptionsReport */
8
9/**
10 * Returns a report describing which `results` (if any) contain disabled ranges
11 * for rules that disallow disables via `reportDisables: true`.
12 *
13 * @param {import('stylelint').StylelintResult[]} results
14 * @returns {StylelintDisableOptionsReport}
15 */
16module.exports = function (results) {
17 /** @type {StylelintDisableOptionsReport} */
18 const report = [];
19
20 results.forEach((result) => {
21 // File with `CssSyntaxError` don't have `_postcssResult`s.
22 if (!result._postcssResult) {
23 return;
24 }
25
26 /** @type {{ranges: DisabledRange[], source: string}} */
27 const reported = { source: result.source || '', ranges: [] };
28
29 /** @type {{[ruleName: string]: Array<RangeType>}} */
30 const rangeData = result._postcssResult.stylelint.disabledRanges;
31
32 if (!rangeData) return;
33
34 const config = result._postcssResult.stylelint.config;
35
36 // If no rules actually disallow disables, don't bother looking for ranges
37 // that correspond to disabled rules.
38 if (!Object.values(_.get(config, 'rules', {})).some(reportDisablesForRule)) {
39 return [];
40 }
41
42 Object.keys(rangeData).forEach((rule) => {
43 rangeData[rule].forEach((range) => {
44 if (!reportDisablesForRule(_.get(config, ['rules', rule], []))) return;
45
46 reported.ranges.push({
47 rule,
48 start: range.start,
49 end: range.end,
50 unusedRule: rule,
51 });
52 });
53 });
54
55 reported.ranges = _.sortBy(reported.ranges, ['start', 'end']);
56
57 report.push(reported);
58 });
59
60 return report;
61};
62
63/**
64 * @param {[any, object]|null} options
65 * @return {boolean}
66 */
67function reportDisablesForRule(options) {
68 if (!options) return false;
69
70 return _.get(options[1], 'reportDisables', false);
71}