-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmarkdownlint-cli2-formatter-summarize.js
81 lines (77 loc) · 2.75 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
// @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 = {};
const countByRule = {};
const countByFileByRule = {};
const countByRuleByFile = {};
for (const result of results) {
const { fileName, ruleNames } = result;
countByFile[fileName] = (countByFile[fileName] || 0) + 1;
const ruleName = ruleNames.join("/");
countByRule[ruleName] = (countByRule[ruleName] || 0) + 1;
const countByRuleOfFile = countByFileByRule[fileName] || {};
countByRuleOfFile[ruleName] = (countByRuleOfFile[ruleName] || 0) + 1;
countByFileByRule[fileName] = countByRuleOfFile;
const countByFileOfRule = countByRuleByFile[ruleName] || {};
countByFileOfRule[fileName] = (countByFileOfRule[fileName] || 0) + 1;
countByRuleByFile[ruleName] = countByFileOfRule;
}
// Show statistics by...
if (byFile) {
logColumns(logMessage, "Count", "File");
const files = Object.keys(countByFile);
files.sort();
files.forEach((file) => logColumns(logMessage, countByFile[file], file));
const total = results.length;
logColumns(logMessage, total, "[Total]");
}
if (byRule) {
logColumns(logMessage, "Count", "Rule");
const rules = Object.keys(countByRule);
rules.sort();
rules.forEach((rule) => logColumns(logMessage, countByRule[rule], rule));
const total = results.length;
logColumns(logMessage, total, "[Total]");
}
if (byFileByRule) {
const files = Object.keys(countByFileByRule);
files.sort();
for (const file of files) {
logMessage(file);
logColumns(logMessage, "Count", "Rule", 2);
const countByRuleOfFile = countByFileByRule[file];
const rules = Object.keys(countByRuleOfFile);
rules.sort();
rules.forEach(
(rule) => logColumns(logMessage, countByRuleOfFile[rule], rule, 2)
);
const total = countByFile[file];
logColumns(logMessage, total, "[Total]", 2);
}
}
if (byRuleByFile) {
const rules = Object.keys(countByRuleByFile);
rules.sort();
for (const rule of rules) {
logMessage(rule);
logColumns(logMessage, "Count", "File", 2);
const countByFileOfRule = countByRuleByFile[rule];
const files = Object.keys(countByFileOfRule);
files.sort();
files.forEach(
(file) => logColumns(logMessage, countByFileOfRule[file], file, 2)
);
const total = countByRule[rule];
logColumns(logMessage, total, "[Total]", 2);
}
}
};
module.exports = outputFormatter;