UNPKG

3.66 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4const optionsMatches = require('./utils/optionsMatches');
5const putIfAbsent = require('./utils/putIfAbsent');
6const validateDisableSettings = require('./validateDisableSettings');
7
8/** @typedef {import('postcss/lib/comment')} PostcssComment */
9/** @typedef {import('stylelint').DisabledRange} DisabledRange */
10/** @typedef {import('stylelint').RangeType} RangeType */
11/** @typedef {import('stylelint').DisableReportRange} DisableReportRange */
12
13/**
14 * @param {import('stylelint').StylelintResult[]} results
15 */
16module.exports = function (results) {
17 results.forEach((result) => {
18 const settings = validateDisableSettings(result._postcssResult, 'reportNeedlessDisables');
19
20 if (!settings) return;
21
22 const [enabled, options, stylelintResult] = settings;
23
24 /** @type {{[ruleName: string]: Array<DisabledRange>}} */
25 const rangeData = _.cloneDeep(stylelintResult.disabledRanges);
26
27 if (!rangeData) return;
28
29 const disabledWarnings = stylelintResult.disabledWarnings || [];
30
31 // A map from `stylelint-disable` comments to the set of rules that
32 // are usefully disabled by each comment. We track this
33 // comment-by-comment rather than range-by-range because ranges that
34 // disable *all* rules are duplicated for each rule they apply to in
35 // practice.
36 /** @type {Map<PostcssComment, Set<string>>}} */
37 const usefulDisables = new Map();
38
39 for (const warning of disabledWarnings) {
40 const rule = warning.rule;
41 const ruleRanges = rangeData[rule];
42
43 if (ruleRanges) {
44 for (const range of ruleRanges) {
45 if (isWarningInRange(warning, range)) {
46 putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule);
47 }
48 }
49 }
50
51 for (const range of rangeData.all) {
52 if (isWarningInRange(warning, range)) {
53 putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule);
54 }
55 }
56 }
57
58 const rangeEntries = Object.entries(rangeData);
59
60 // Get rid of the duplicated ranges for each `all` rule. We only care
61 // if the entire `all` rule is useful as a whole or not.
62 for (const range of rangeData.all) {
63 for (const [rule, ranges] of rangeEntries) {
64 if (rule === 'all') continue;
65
66 _.remove(ranges, (otherRange) => range.comment === otherRange.comment);
67 }
68 }
69
70 for (const [rule, ranges] of rangeEntries) {
71 for (const range of ranges) {
72 if (enabled === optionsMatches(options, 'except', rule)) continue;
73
74 const useful = usefulDisables.get(range.comment) || new Set();
75
76 // Only emit a warning if this range's comment isn't useful for this rule.
77 // For the special rule "all", only emit a warning if it's not useful for
78 // *any* rules, because it covers all of them.
79 if (rule === 'all' ? useful.size !== 0 : useful.has(rule)) continue;
80
81 // If the comment doesn't have a location, we can't report a useful error.
82 // In practice we expect all comments to have locations, though.
83 if (!range.comment.source || !range.comment.source.start) continue;
84
85 result.warnings.push({
86 text: `Needless disable for "${rule}"`,
87 rule: '--report-needless-disables',
88 line: range.comment.source.start.line,
89 column: range.comment.source.start.column,
90 severity: options.severity,
91 });
92 }
93 }
94 });
95};
96
97/**
98 * @param {import('stylelint').DisabledWarning} warning
99 * @param {RangeType} range
100 * @return {boolean}
101 */
102function isWarningInRange(warning, range) {
103 const line = warning.line;
104
105 // Need to check if range.end exist, because line number type cannot be compared to undefined
106 return (
107 range.start <= line &&
108 ((range.end !== undefined && range.end >= line) || range.end === undefined)
109 );
110}