forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eslint-relative-formatter.js
103 lines (89 loc) · 2.64 KB
/
eslint-relative-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
// The default ESLint formatter with relative paths.
// Forked from https://github.com/eslint/eslint/blob/main/lib/cli-engine/formatters/stylish.js
const path = require('path')
const chalk = require('chalk')
const stripAnsi = require('strip-ansi')
const table = require('text-table')
function pluralize(word, count) {
return count === 1 ? word : `${word}s`
}
module.exports = function (results) {
let output = '\n',
errorCount = 0,
warningCount = 0,
fixableErrorCount = 0,
fixableWarningCount = 0,
summaryColor = 'yellow'
results.forEach(result => {
const messages = result.messages
if (messages.length === 0) {
return
}
errorCount += result.errorCount
warningCount += result.warningCount
fixableErrorCount += result.fixableErrorCount
fixableWarningCount += result.fixableWarningCount
// This is the only line that is different from the original stylish formatter.
output += `${chalk.underline(path.relative(process.cwd(), result.filePath))}\n`
output += `${table(
messages.map(message => {
let messageType
if (message.fatal || message.severity === 2) {
messageType = chalk.red('error')
summaryColor = 'red'
} else {
messageType = chalk.yellow('warning')
}
return [
'',
message.line || 0,
message.column || 0,
messageType,
message.message.replace(/([^ ])\.$/u, '$1'),
chalk.dim(message.ruleId || ''),
]
}),
{
align: ['', 'r', 'l'],
stringLength(str) {
return stripAnsi(str).length
},
}
)
.split('\n')
.map(el => el.replace(/(\d+)\s+(\d+)/u, (m, p1, p2) => chalk.dim(`${p1}:${p2}`)))
.join('\n')}\n\n`
})
const total = errorCount + warningCount
if (total > 0) {
output += chalk[summaryColor].bold(
[
'\u2716 ',
total,
pluralize(' problem', total),
' (',
errorCount,
pluralize(' error', errorCount),
', ',
warningCount,
pluralize(' warning', warningCount),
')\n',
].join('')
)
if (fixableErrorCount > 0 || fixableWarningCount > 0) {
output += chalk[summaryColor].bold(
[
' ',
fixableErrorCount,
pluralize(' error', fixableErrorCount),
' and ',
fixableWarningCount,
pluralize(' warning', fixableWarningCount),
' potentially fixable with the `--fix` option.\n',
].join('')
)
}
}
// Resets output color, for prevent change on top level
return total > 0 ? chalk.reset(output) : ''
}