forked from jasancheg/sails-eslint
/
pretty-formatter.js
109 lines (88 loc) · 3.11 KB
/
pretty-formatter.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
'use strict';
var path = require('path');
var plur = require('plur');
var chalk = require('chalk');
var repeating = require('repeating');
var logSymbols = require('log-symbols');
var stringWidth = require('string-width');
var ansiEscapes = require('ansi-escapes');
module.exports = function(results) {
var lines = [];
var errorCount = 0;
var warningCount = 0;
var maxLineWidth = 0;
var maxColumnWidth = 0;
var maxMessageWidth = 0;
results = results && typeof results === 'object' ? results : [];
results.forEach(function(result) {
var messages = result.messages;
if(messages.length === 0) return;
errorCount += result.errorCount;
warningCount += result.warningCount;
if(lines.length !== 0) {
lines.push({type: 'separator'});
}
var filePath = result.filePath;
lines.push({
type: 'header',
filePath: filePath,
relativeFilePath: path.relative('.', filePath),
firstLineCol: messages[0].line + ':' + messages[0].column
});
messages.forEach(function(x) {
var msg = x.message;
// stylize inline code blocks
msg = msg.replace(/\B`(.*?)`\B|\B'(.*?)'\B/g, function(m, p1, p2) {
return chalk.bold(p1 || p2);
});
var line = String(x.line || 0);
var column = String(x.column || 0);
var lineWidth = stringWidth(line);
var columnWidth = stringWidth(column);
var messageWidth = stringWidth(msg);
maxLineWidth = Math.max(lineWidth, maxLineWidth);
maxColumnWidth = Math.max(columnWidth, maxColumnWidth);
maxMessageWidth = Math.max(messageWidth, maxMessageWidth);
lines.push({
type: 'message',
severity: (x.fatal || x.severity === 2) ? 'error' : 'warning',
line: line,
lineWidth: lineWidth,
column: column,
columnWidth: columnWidth,
message: msg,
messageWidth: messageWidth,
ruleId: x.ruleId || ''
});
});
});
var output = '\n';
if(process.stdout.isTTY && !process.env.CI) {
// make relative paths Cmd+click'able in iTerm
output += ansiEscapes.iTerm.setCwd();
}
output += lines.map(function(x) {
if(x.type === 'header') {
// add the line number so it's Cmd+click'able in some terminals
// use dim & gray for terminals like iTerm that doesn't support `hidden`
return ' ' + chalk.underline(x.relativeFilePath + chalk.hidden.dim.gray(':' + x.firstLineCol));
}
if(x.type === 'message') {
return [
'',
x.severity === 'warning' ? logSymbols.warning : logSymbols.error,
repeating(maxLineWidth - x.lineWidth) + chalk.dim(x.line + chalk.gray(':') + x.column),
repeating(maxColumnWidth - x.columnWidth) + x.message,
repeating(maxMessageWidth - x.messageWidth) + chalk.gray.dim(x.ruleId)
].join(' ');
}
return '';
}).join('\n') + '\n\n';
if(errorCount > 0) {
output += ' ' + chalk.red(errorCount + ' ' + plur('error', errorCount)) + '\n';
}
if(warningCount > 0) {
output += ' ' + chalk.yellow(warningCount + ' ' + plur('warning', warningCount)) + '\n';
}
return (errorCount + warningCount) > 0 ? output : '';
};