blob: 004120fe9b18b5f9819f3e561d4623ae0a787408 [file] [log] [blame]
Tim van der Lippeefb716a2020-12-01 12:54:04 +00001'use strict';
2
Tim van der Lippe0a9b84d2021-03-24 11:53:15 +00003const optionsMatches = require('./utils/optionsMatches');
4const validateDisableSettings = require('./validateDisableSettings');
5
Tim van der Lippe16b82282021-11-08 13:50:26 +00006/** @typedef {import('postcss').Comment} PostcssComment */
Tim van der Lippeefb716a2020-12-01 12:54:04 +00007/** @typedef {import('stylelint').RangeType} RangeType */
8/** @typedef {import('stylelint').DisableReportRange} DisableReportRange */
Tim van der Lippe16b82282021-11-08 13:50:26 +00009/** @typedef {import('stylelint').DisableOptionsReport} StylelintDisableOptionsReport */
Tim van der Lippeefb716a2020-12-01 12:54:04 +000010
11/**
Tim van der Lippe16b82282021-11-08 13:50:26 +000012 * @param {import('stylelint').LintResult[]} results
Tim van der Lippeefb716a2020-12-01 12:54:04 +000013 */
Tim van der Lippe16b82282021-11-08 13:50:26 +000014module.exports = function descriptionlessDisables(results) {
Tim van der Lippeefb716a2020-12-01 12:54:04 +000015 results.forEach((result) => {
Tim van der Lippe0a9b84d2021-03-24 11:53:15 +000016 const settings = validateDisableSettings(
17 result._postcssResult,
18 'reportDescriptionlessDisables',
19 );
Tim van der Lippeefb716a2020-12-01 12:54:04 +000020
Tim van der Lippe0a9b84d2021-03-24 11:53:15 +000021 if (!settings) return;
Tim van der Lippecc71b282021-02-12 15:51:14 +000022
Tim van der Lippe0a9b84d2021-03-24 11:53:15 +000023 const [enabled, options, stylelintResult] = settings;
Tim van der Lippecc71b282021-02-12 15:51:14 +000024
25 const rangeData = stylelintResult.disabledRanges;
Tim van der Lippeefb716a2020-12-01 12:54:04 +000026
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
Tim van der Lippe0a9b84d2021-03-24 11:53:15 +000036 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
Tim van der Lippeefb716a2020-12-01 12:54:04 +000046 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,
Tim van der Lippe0a9b84d2021-03-24 11:53:15 +000057 severity: options.severity,
Tim van der Lippeefb716a2020-12-01 12:54:04 +000058 });
59 });
60 });
61 });
62};