-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmarkdownlint-cli2-formatter-summarize.js
85 lines (81 loc) · 2.9 KB
/
markdownlint-cli2-formatter-summarize.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// @ts-check
"use strict";
const logColumns = (log, count, name, indent) => {
log(`${"".padEnd(indent || 0)}${count.toString().padStart(5)} ${name}`);
};
// Summarize the results
const outputFormatter = (options, params) => {
const { logMessage, results } = options;
const { byFile, byRule, byFileByRule, byRuleByFile } = (params || {});
// Calculate statistics
const countByFile = new Map();
const countByRule = new Map();
const countByFileByRule = new Map();
const countByRuleByFile = new Map();
for (const result of results) {
const { fileName, ruleNames } = result;
countByFile.set(fileName, (countByFile.get(fileName) || 0) + 1);
const ruleName = ruleNames.join("/");
countByRule.set(ruleName, (countByRule.get(ruleName) || 0) + 1);
const countByRuleOfFile = countByFileByRule.get(fileName) || new Map();
countByRuleOfFile.set(ruleName, (countByRuleOfFile.get(ruleName) || 0) + 1);
countByFileByRule.set(fileName, countByRuleOfFile);
const countByFileOfRule = countByRuleByFile.get(ruleName) || new Map();
countByFileOfRule.set(fileName, (countByFileOfRule.get(fileName) || 0) + 1);
countByRuleByFile.set(ruleName, countByFileOfRule);
}
// Show statistics by...
if (byFile) {
logColumns(logMessage, "Count", "File");
const files = [ ...countByFile.keys() ];
files.sort();
for (const file of files) {
logColumns(logMessage, countByFile.get(file), file);
}
const total = results.length;
logColumns(logMessage, total, "[Total]");
}
if (byRule) {
logColumns(logMessage, "Count", "Rule");
const rules = [ ...countByRule.keys() ];
rules.sort();
for (const rule of rules) {
logColumns(logMessage, countByRule.get(rule), rule);
}
const total = results.length;
logColumns(logMessage, total, "[Total]");
}
if (byFileByRule) {
const files = [ ...countByFileByRule.keys() ];
files.sort();
for (const file of files) {
logMessage(file);
logColumns(logMessage, "Count", "Rule", 2);
const countByRuleOfFile = countByFileByRule.get(file);
const rules = [ ...countByRuleOfFile.keys() ];
rules.sort();
for (const rule of rules) {
logColumns(logMessage, countByRuleOfFile.get(rule), rule, 2);
}
const total = countByFile.get(file);
logColumns(logMessage, total, "[Total]", 2);
}
}
if (byRuleByFile) {
const rules = [ ...countByRuleByFile.keys() ];
rules.sort();
for (const rule of rules) {
logMessage(rule);
logColumns(logMessage, "Count", "File", 2);
const countByFileOfRule = countByRuleByFile.get(rule);
const files = [ ...countByFileOfRule.keys() ];
files.sort();
for (const file of files) {
logColumns(logMessage, countByFileOfRule.get(file), file, 2);
}
const total = countByRule.get(rule);
logColumns(logMessage, total, "[Total]", 2);
}
}
};
module.exports = outputFormatter;