blob: a60f8a24f29f314bee7d1034f42ec95ead2653cd [file] [log] [blame]
Tim van der Lippeefb716a2020-12-01 12:54:04 +00001'use strict';
2
3/** @typedef {import('postcss/lib/comment')} PostcssComment */
4/** @typedef {import('stylelint').RangeType} RangeType */
5/** @typedef {import('stylelint').DisableReportRange} DisableReportRange */
6/** @typedef {import('stylelint').StylelintDisableOptionsReport} StylelintDisableOptionsReport */
7
8/**
9 * @param {import('stylelint').StylelintResult[]} results
10 */
11module.exports = function (results) {
12 results.forEach((result) => {
13 // File with `CssSyntaxError` have not `_postcssResult`
14 if (!result._postcssResult) {
15 return;
16 }
17
Tim van der Lippecc71b282021-02-12 15:51:14 +000018 const stylelintResult = result._postcssResult.stylelint;
19
20 if (!stylelintResult.config) return; // Linting error
21
22 if (!stylelintResult.config.reportDescriptionlessDisables) return;
23
24 const rangeData = stylelintResult.disabledRanges;
Tim van der Lippeefb716a2020-12-01 12:54:04 +000025
26 /** @type {Set<PostcssComment>} */
27 const alreadyReported = new Set();
28
29 Object.keys(rangeData).forEach((rule) => {
30 rangeData[rule].forEach((range) => {
31 if (range.description) return;
32
33 if (alreadyReported.has(range.comment)) return;
34
35 alreadyReported.add(range.comment);
36
37 // If the comment doesn't have a location, we can't report a useful error.
38 // In practice we expect all comments to have locations, though.
39 if (!range.comment.source || !range.comment.source.start) return;
40
41 result.warnings.push({
42 text: `Disable for "${rule}" is missing a description`,
43 rule: '--report-descriptionless-disables',
44 line: range.comment.source.start.line,
45 column: range.comment.source.start.column,
46 severity: 'error',
47 });
48 });
49 });
50 });
51};