UNPKG

2.18 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 for (const result of results) {
16 const settings = validateDisableSettings(
17 result._postcssResult,
18 'reportDescriptionlessDisables',
19 );
20
21 if (!settings) continue;
22
23 const [enabled, options, stylelintResult] = settings;
24
25 /** @type {Set<PostcssComment>} */
26 const alreadyReported = new Set();
27
28 for (const [rule, ruleRanges] of Object.entries(stylelintResult.disabledRanges)) {
29 for (const range of ruleRanges) {
30 if (range.description) continue;
31
32 if (alreadyReported.has(range.comment)) continue;
33
34 if (enabled === optionsMatches(options, 'except', rule)) {
35 // An 'all' rule will get copied for each individual rule. If the
36 // configuration is `[false, {except: ['specific-rule']}]`, we
37 // don't want to report the copies that match except, so we record
38 // the comment as already reported.
39 if (!enabled && rule === 'all') alreadyReported.add(range.comment);
40
41 continue;
42 }
43
44 alreadyReported.add(range.comment);
45
46 // If the comment doesn't have a location, we can't report a useful error.
47 // In practice we expect all comments to have locations, though.
48 if (!range.comment.source || !range.comment.source.start) continue;
49
50 result.warnings.push({
51 text: `Disable for "${rule}" is missing a description`,
52 rule: '--report-descriptionless-disables',
53 line: range.comment.source.start.line,
54 column: range.comment.source.start.column,
55 endLine: range.comment.source.end && range.comment.source.end.line,
56 endColumn: range.comment.source.end && range.comment.source.end.column,
57 severity: options.severity,
58 });
59 }
60 }
61 }
62};