UNPKG

2.04 kBJavaScriptView Raw
1'use strict';
2
3const optionsMatches = require('./utils/optionsMatches');
4const validateDisableSettings = require('./validateDisableSettings');
5
6/** @typedef {import('postcss').Comment} PostcssComment */
7/** @typedef {import('stylelint').RangeType} RangeType */
8/** @typedef {import('stylelint').DisableReportRange} DisableReportRange */
9/** @typedef {import('stylelint').DisableOptionsReport} StylelintDisableOptionsReport */
10
11/**
12 * @param {import('stylelint').LintResult[]} results
13 */
14module.exports = function descriptionlessDisables(results) {
15 results.forEach((result) => {
16 const settings = validateDisableSettings(
17 result._postcssResult,
18 'reportDescriptionlessDisables',
19 );
20
21 if (!settings) return;
22
23 const [enabled, options, stylelintResult] = settings;
24
25 const rangeData = stylelintResult.disabledRanges;
26
27 /** @type {Set<PostcssComment>} */
28 const alreadyReported = new Set();
29
30 Object.keys(rangeData).forEach((rule) => {
31 rangeData[rule].forEach((range) => {
32 if (range.description) return;
33
34 if (alreadyReported.has(range.comment)) return;
35
36 if (enabled === optionsMatches(options, 'except', rule)) {
37 // An 'all' rule will get copied for each individual rule. If the
38 // configuration is `[false, {except: ['specific-rule']}]`, we
39 // don't want to report the copies that match except, so we record
40 // the comment as already reported.
41 if (!enabled && rule === 'all') alreadyReported.add(range.comment);
42
43 return;
44 }
45
46 alreadyReported.add(range.comment);
47
48 // If the comment doesn't have a location, we can't report a useful error.
49 // In practice we expect all comments to have locations, though.
50 if (!range.comment.source || !range.comment.source.start) return;
51
52 result.warnings.push({
53 text: `Disable for "${rule}" is missing a description`,
54 rule: '--report-descriptionless-disables',
55 line: range.comment.source.start.line,
56 column: range.comment.source.start.column,
57 severity: options.severity,
58 });
59 });
60 });
61 });
62};