-
Notifications
You must be signed in to change notification settings - Fork 7
/
filterer.js
47 lines (39 loc) · 1.14 KB
/
filterer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Filters a set of ESLint results down to either errors or warnings (or both).
*
* @param {Object} report
* @param {boolean} errors
* @param {boolean} warnings
* @return {Object}
*/
module.exports = function filterer(report, errors, warnings) {
let key;
let opposite;
let severity;
// --errors always takes precedence over --warnings.
if (errors) {
key = 'errorCount';
opposite = 'warningCount';
severity = 2;
} else if (warnings) {
key = 'warningCount';
opposite = 'errorCount';
severity = 1;
// If neither are true, no filtering happens.
} else {
return report;
}
// Set the total count for the opposite count to zero.
report[opposite] = 0;
// Handle both --errors and --warnings in the same way.
report.results = report.results.
// Filter down to only those results which have a count of the appopriate type.
filter(r => r[key] > 0).
// Filter out opposite-type messages and set the opposite-type count to zero.
map(r => {
r.messages = r.messages.filter(message => message.severity === severity);
r[opposite] = 0;
return r;
});
return report;
};