1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const putIfAbsent = require('./utils/putIfAbsent');
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | module.exports = function (results) {
|
15 | results.forEach((result) => {
|
16 |
|
17 | if (!result._postcssResult) {
|
18 | return;
|
19 | }
|
20 |
|
21 |
|
22 | const rangeData = _.cloneDeep(result._postcssResult.stylelint.disabledRanges);
|
23 |
|
24 | if (!rangeData) {
|
25 | return;
|
26 | }
|
27 |
|
28 | const disabledWarnings = result._postcssResult.stylelint.disabledWarnings || [];
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | const usefulDisables = new Map();
|
37 |
|
38 | for (const warning of disabledWarnings) {
|
39 | const rule = warning.rule;
|
40 | const ruleRanges = rangeData[rule];
|
41 |
|
42 | if (ruleRanges) {
|
43 | for (const range of ruleRanges) {
|
44 | if (isWarningInRange(warning, range)) {
|
45 | putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule);
|
46 | }
|
47 | }
|
48 | }
|
49 |
|
50 | for (const range of rangeData.all) {
|
51 | if (isWarningInRange(warning, range)) {
|
52 | putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule);
|
53 | }
|
54 | }
|
55 | }
|
56 |
|
57 | const rangeEntries = Object.entries(rangeData);
|
58 |
|
59 |
|
60 |
|
61 | for (const range of rangeData.all) {
|
62 | for (const [rule, ranges] of rangeEntries) {
|
63 | if (rule === 'all') continue;
|
64 |
|
65 | _.remove(ranges, (otherRange) => range.comment === otherRange.comment);
|
66 | }
|
67 | }
|
68 |
|
69 | for (const [rule, ranges] of rangeEntries) {
|
70 | for (const range of ranges) {
|
71 | const useful = usefulDisables.get(range.comment) || new Set();
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | if (rule === 'all' ? useful.size !== 0 : useful.has(rule)) continue;
|
77 |
|
78 |
|
79 |
|
80 | if (!range.comment.source || !range.comment.source.start) continue;
|
81 |
|
82 | result.warnings.push({
|
83 | text: `Needless disable for "${rule}"`,
|
84 | rule: '--report-needless-disables',
|
85 | line: range.comment.source.start.line,
|
86 | column: range.comment.source.start.column,
|
87 | severity: 'error',
|
88 | });
|
89 | }
|
90 | }
|
91 | });
|
92 | };
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | function isWarningInRange(warning, range) {
|
100 | const line = warning.line;
|
101 |
|
102 |
|
103 | return (
|
104 | range.start <= line &&
|
105 | ((range.end !== undefined && range.end >= line) || range.end === undefined)
|
106 | );
|
107 | }
|