UNPKG

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