forked from sindresorhus/eslint-formatter-pretty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (180 loc) · 5.35 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"use strict";
const path = require("path");
const chalk = require("chalk");
const logSymbols = require("log-symbols");
const plur = require("plur");
const stringWidth = require("string-width");
const ansiEscapes = require("ansi-escapes");
const { supportsHyperlink } = require("supports-hyperlinks");
const getRuleDocs = require("eslint-rule-docs");
module.exports = (results, data) => {
const lines = [];
let errorCount = 0;
let warningCount = 0;
let maxLineWidth = 0;
let maxColumnWidth = 0;
let maxMessageWidth = 0;
let showLineNumbers = false;
let maxRuleWidth = 0;
let summary = {};
results
.sort((a, b) => {
if (a.errorCount === b.errorCount) {
return b.warningCount - a.warningCount;
}
if (a.errorCount === 0) {
return -1;
}
if (b.errorCount === 0) {
return 1;
}
return b.errorCount - a.errorCount;
})
.forEach((result) => {
const { messages, filePath } = result;
if (messages.length === 0) {
return;
}
warningCount += result.warningCount;
if (lines.length !== 0) {
lines.push({ type: "separator" });
}
const firstErrorOrWarning =
messages.find(({ severity }) => severity === 2) || messages[0];
const trueErrorCount = messages.filter((x) => {
return x.fatal || x.severity === 2 || x.severity === "error";
}).length;
errorCount += trueErrorCount;
if (trueErrorCount > 0) {
lines.push({
type: "header",
filePath,
relativeFilePath: path.relative(".", filePath),
firstLineCol:
firstErrorOrWarning.line + ":" + firstErrorOrWarning.column,
});
}
messages
.sort((a, b) => {
if (a.fatal === b.fatal && a.severity === b.severity) {
if (a.line === b.line) {
return a.column < b.column ? -1 : 1;
}
return a.line < b.line ? -1 : 1;
}
if ((a.fatal || a.severity === 2) && (!b.fatal || b.severity !== 2)) {
return 1;
}
return -1;
})
.forEach((x) => {
let { message } = x;
// Stylize inline code blocks
message = message.replace(/\B`(.*?)`\B|\B'(.*?)'\B/g, (m, p1, p2) =>
chalk.bold(p1 || p2)
);
const line = String(x.line || 0);
const column = String(x.column || 0);
const lineWidth = stringWidth(line);
const columnWidth = stringWidth(column);
const messageWidth = stringWidth(message);
const ruleWidth = stringWidth(x.ruleId);
maxLineWidth = Math.max(lineWidth, maxLineWidth);
maxColumnWidth = Math.max(columnWidth, maxColumnWidth);
maxMessageWidth = Math.max(messageWidth, maxMessageWidth);
maxRuleWidth = Math.max(ruleWidth, maxRuleWidth);
showLineNumbers = showLineNumbers || x.line || x.column;
const isError = x.fatal || x.severity === 2 || x.severity === "error";
if (isError) {
lines.push({
type: "message",
severity: isError ? "error" : "warning",
line,
lineWidth,
column,
columnWidth,
message,
messageWidth,
ruleId: x.ruleId || "",
});
} else {
if (!summary[x.ruleId]) summary[x.ruleId] = { nr: 0, files: [] };
summary[x.ruleId].nr++;
if (summary[x.ruleId].files.indexOf(result.filePath) == -1)
summary[x.ruleId].files.push(result.filePath);
}
});
});
let output = "\n";
if (process.stdout.isTTY && !process.env.CI) {
// Make relative paths Command-clickable in iTerm
output += ansiEscapes.iTerm.setCwd();
}
if (Object.keys(summary).length > 0) {
output += " " + chalk.blue("Warning summary") + "\n";
Object.keys(summary).forEach((ruleId) => {
output +=
" " +
chalk.gray(ruleId) +
" ".repeat(maxRuleWidth - ruleId.length) +
" " +
chalk.yellow(summary[ruleId].nr) +
" errors in " +
chalk.yellow(summary[ruleId].files.length) +
" files" +
"\n";
});
output += "\n";
}
if (errorCount > 0) {
output += " " + chalk.blue("Error details") + "\n";
output +=
lines
.map((x) => {
if (x.type === "header") {
// Add the line number so it's Command-click'able in some terminals
// Use dim & gray for terminals like iTerm that doesn't support `hidden`
const position = showLineNumbers
? chalk.hidden.dim.gray(`:${x.firstLineCol}`)
: "";
return " " + chalk.underline(x.relativeFilePath) + position;
}
if (x.type === "message") {
let ruleUrl;
try {
ruleUrl = data.rulesMeta[x.ruleId].docs.url;
} catch {
try {
ruleUrl = getRuleDocs(x.ruleId).url;
} catch {}
}
const line = [
"",
x.severity === "warning" ? "warning" : "error",
" ".repeat(maxLineWidth - x.lineWidth) +
chalk.dim(x.line + chalk.gray(":") + x.column),
" ".repeat(maxColumnWidth - x.columnWidth) + x.message,
" ".repeat(maxMessageWidth - x.messageWidth) +
(ruleUrl && supportsHyperlink(process.stdout)
? ansiEscapes.link(chalk.dim(x.ruleId), ruleUrl)
: chalk.dim(x.ruleId)),
];
if (!showLineNumbers) {
line.splice(2, 1);
}
return line.join(" ");
}
return "";
})
.join("\n") + "\n\n";
}
if (warningCount > 0) {
output +=
" " + chalk.yellow(`${warningCount} ${plur("warning", warningCount)}`);
}
if (errorCount > 0) {
output +=
", " + chalk.red(`${errorCount} ${plur("error", errorCount)}`) + "\n";
}
return errorCount + warningCount > 0 ? output : "";
};