-
Notifications
You must be signed in to change notification settings - Fork 60.7k
/
Copy pathpretty-print-results.js
170 lines (154 loc) · 5.16 KB
/
pretty-print-results.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
import chalk from 'chalk'
function isNumber(value) {
return typeof value === 'number' && !isNaN(value)
}
function shorten(text, length = 70) {
if (text.length <= length) return text
return `${text.slice(0, length - 3)}…`
}
export function prettyPrintResults(results, { fixed = false } = {}) {
const PREFIX_PADDING = ' '.repeat(4)
const columnPadding = 'Description'.length // The longest column header word
function label(text, padding = columnPadding) {
if (padding < text.length) throw new Error('Padding must be greater than text length')
return `${PREFIX_PADDING}${chalk.dim(text.padEnd(padding))}`
}
for (const [file, flaws] of Object.entries(results)) {
console.log(chalk.bold(file))
console.log('') // blank line
// It's very possible that a the same file has multiple flaws of the
// same rule but on different line numbers.
const sorted = [...flaws]
.sort((a, b) => a.lineNumber - b.lineNumber)
.sort((a, b) => a.ruleDescription.localeCompare(b.ruleDescription))
.sort((a, b) => {
if (a.severity === b.severity) return 0
if (a.severity === 'error') return -1
if (b.severity === 'error') return 1
return 0
})
let ruleDescription = ''
const errorDetailsByDescription = new Map()
for (const { errorDetail, ruleDescription } of sorted) {
const details = errorDetailsByDescription.get(ruleDescription) || new Set()
details.add(errorDetail)
errorDetailsByDescription.set(ruleDescription, details)
}
for (const result of sorted) {
const distinctDetails = errorDetailsByDescription.get(result.ruleDescription).size > 1
if (ruleDescription !== result.ruleDescription) {
if (ruleDescription) {
console.log('')
}
if (result.fixable && fixed) {
console.log(
`${PREFIX_PADDING}${chalkFunColors('✨ FIXED!')} (was ${
result.severity === 'error' ? 'an error' : 'a warning'
})`,
)
} else {
console.log(
`${PREFIX_PADDING}${
result.severity === 'error'
? chalk.bold(chalk.red('ERROR'))
: result.severity === 'warning'
? chalk.yellow('WARNING')
: chalk.blue(result.severity)
}`,
)
}
if (result.fixable && !fixed) {
console.log(`${PREFIX_PADDING}${chalk.green('Fixable!')}`)
}
const ruleNames = result.ruleNames.map((name) => chalk.bold(name)).join(', ')
console.log(
label('Rule'),
ruleNames,
chalk.dim(indentWrappedString(result.ruleDescription, ruleNames.length)),
)
if (!distinctDetails && result.errorDetail) {
console.log(
label('Detail'),
`${indentWrappedString(result.errorDetail.replace(/\n/g, ' ').trim(), PREFIX_PADDING.length * 8)}`,
)
}
if (result.context) {
console.log(
label('Context'),
`${indentWrappedString(result.context.replace(/\n/g, ' ').trim(), PREFIX_PADDING.length * 8)}`,
)
}
}
let position = chalk.yellow(result.lineNumber)
if (isNumber(result.columnNumber) && result.columnNumber !== 1) {
position += ` (col ${chalk.yellow(result.columnNumber)})`
}
if (distinctDetails && result.errorDetail) {
console.log(
label('Detail'),
indentWrappedString(
result.errorDetail.replace(/\n/g, ' ').trim(),
PREFIX_PADDING.length * 8,
),
)
}
console.log(
label('Line'),
position.padEnd(6),
result.errorContext ? chalk.dim(shorten(result.errorContext.trim(), 80)) : '',
)
ruleDescription = result.ruleDescription
}
console.log('\n')
}
}
function chalkFunColors(text) {
const colors = [
'red',
'yellow',
'green',
'magenta',
'cyan',
'redBright',
'yellowBright',
'greenBright',
'magentaBright',
'cyanBright',
].sort(() => Math.random() - 0.5)
let colorIndex = 0
return text
.split('')
.map((char) => {
const color = colors[colorIndex]
colorIndex = (colorIndex + 1) % colors.length
return chalk[color](char)
})
.join('')
}
function indentWrappedString(str, startingIndent) {
const NEW_LINE_PADDING = ' '.repeat(16)
const width = process.stdout.columns || 80 // Use terminal width, default to 80 if not available
let indentedString = ''
let currentLine = ''
let isFirstLine = true
for (const word of str.split(' ')) {
const effectiveWidth = isFirstLine ? width - startingIndent : width - NEW_LINE_PADDING.length
if ((currentLine + word).length > effectiveWidth) {
if (isFirstLine) {
indentedString += currentLine.trim() + '\n'
isFirstLine = false
} else {
indentedString += NEW_LINE_PADDING + currentLine.trim() + '\n'
}
currentLine = word + ' '
} else {
currentLine += word + ' '
}
}
if (isFirstLine) {
indentedString += currentLine.trim()
} else {
indentedString += NEW_LINE_PADDING + currentLine.trim()
}
return indentedString
}