UNPKG

1.82 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4
5/** @typedef {import('stylelint').RangeType} RangeType */
6/** @typedef {import('stylelint').DisableReportRange} DisabledRange */
7
8/**
9 * Returns a report describing which `results` (if any) contain disabled ranges
10 * for rules that disallow disables via `reportDisables: true`.
11 *
12 * @param {import('stylelint').StylelintResult[]} results
13 */
14module.exports = function (results) {
15 results.forEach((result) => {
16 // File with `CssSyntaxError` don't have `_postcssResult`s.
17 if (!result._postcssResult) {
18 return;
19 }
20
21 /** @type {{[ruleName: string]: Array<RangeType>}} */
22 const rangeData = result._postcssResult.stylelint.disabledRanges;
23
24 if (!rangeData) return;
25
26 const config = result._postcssResult.stylelint.config;
27
28 // If no rules actually disallow disables, don't bother looking for ranges
29 // that correspond to disabled rules.
30 if (!Object.values(_.get(config, 'rules', {})).some(reportDisablesForRule)) {
31 return [];
32 }
33
34 Object.keys(rangeData).forEach((rule) => {
35 rangeData[rule].forEach((range) => {
36 if (!reportDisablesForRule(_.get(config, ['rules', rule], []))) return;
37
38 // If the comment doesn't have a location, we can't report a useful error.
39 // In practice we expect all comments to have locations, though.
40 if (!range.comment.source || !range.comment.source.start) return;
41
42 result.warnings.push({
43 text: `Rule "${rule}" may not be disabled`,
44 rule: 'reportDisables',
45 line: range.comment.source.start.line,
46 column: range.comment.source.start.column,
47 severity: 'error',
48 });
49 });
50 });
51 });
52};
53
54/**
55 * @param {[any, object]|null} options
56 * @return {boolean}
57 */
58function reportDisablesForRule(options) {
59 if (!options) return false;
60
61 return _.get(options[1], 'reportDisables', false);
62}