forked from todogroup/repolinter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbol_formatter.js
More file actions
180 lines (174 loc) · 5.44 KB
/
symbol_formatter.js
File metadata and controls
180 lines (174 loc) · 5.44 KB
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
// Copyright 2017 TODO Group. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
const logSymbols = require('log-symbols')
const chalk = require('chalk')
const FormatResult = require('../lib/formatresult')
// eslint-disable-next-line no-unused-vars
const Result = require('../lib/result')
/**
* Pads a string with a space if the string exists,
* returns the falsey input value otherwise.
*
* @private
* @param {string?} string The string or null input
* @returns {string} A padded string or empty string
*/
function frontSpace(string) {
return string ? ' ' + string : ''
}
/**
* The default CLI formatter. Exported as defaultFormatter and resultFormatter.
*
* @protected
*/
class SymbolFormatter {
/**
* Format a FormatResult object into a line of human-readable text.
*
* @param {Result} result The result to format, must be valid
* @param {string} ruleName The name of the rule this result is from
* @param {string} rulePolicyUrl The policyUrl of the rule this result is from
* @param {string} rulePolicyInfo The policyInfo of the rule this result is from
* @param {string} errorSymbol The symbol to use if the result did not pass
* @param {string} okSymbol The symbol to use if the result passed
* @returns {string} The formatted string
*/
static formatResult(
result,
ruleName,
rulePolicyUrl = undefined,
rulePolicyInfo = undefined,
errorSymbol,
okSymbol = logSymbols.success
) {
// format lint output
let policyLines = ''
if (!result.passed) {
if (rulePolicyUrl)
policyLines += `\n\t${logSymbols.info} PolicyUrl: ${rulePolicyUrl}`
if (rulePolicyInfo)
policyLines += `\n\t${logSymbols.info} PolicyInfo: ${rulePolicyInfo}`
}
const formatbase = `\n${
result.passed ? okSymbol : errorSymbol
} ${ruleName}:${frontSpace(result.message)}${
!result.passed ? policyLines : ''
}`
// condensed one-line version for rules with no targets
if (result.targets.length === 0) {
return formatbase
}
// condensed one-line version for rules with one target
if (result.targets.length === 1) {
return (
formatbase +
`${frontSpace(result.targets[0].message)} (${
result.targets[0].path || result.targets[0].pattern
})`
)
}
// expanded version for more complicated rules
return (
formatbase +
result.targets
.map(
t =>
`\n\t${t.passed ? okSymbol : errorSymbol} ${t.path || t.pattern}${
t.message ? ': ' + t.message : ''
}`
)
.join('')
)
}
/**
* Get the logsymbol associated with a log level (specified in the JSON configuration schema)
*
* @param {string} level The log level string ("info", "warning", or "error"
* @returns {string} A corresponding logsymbol
*/
static getSymbol(level) {
switch (level) {
case 'info':
return logSymbols.info
case 'warning':
return logSymbols.warning
case 'error':
return logSymbols.error
default:
return logSymbols.error
}
}
/**
*
* @param {LintResult} output The linter output to format
* @param {boolean} dryRun Whether or not to generate in "report" format
* @returns {string} The formatted output
*/
static formatOutput(output, dryRun) {
const ret = [`Target directory: ${output.params.targetDir}`]
if (output.params.filterPaths.length) {
ret.push(
`\nPaths to include in checks:\n\t${output.params.filterPaths.join(
'\n\t'
)}`
)
}
if (output.errored) {
return ret.join('') + `\n${chalk.bgRed(output.errMsg)}`
}
// output axiom errors, if any
ret.push(
Object.entries(output.targets)
.filter(([k, v]) => v.passed !== true)
.map(([k, v]) =>
chalk.yellow(`\nAxiom ${k} failed to run with error: ${v.message}`)
)
.join('')
)
// lint section
ret.push(
chalk.inverse('\nLint:') +
output.results
.map(result => {
// log errors
if (result.status === FormatResult.ERROR) {
return `\n${logSymbols.error} ${chalk.bgRed(
`${result.ruleInfo.name} failed to run:`
)} ${result.runMessage}`
}
// log ignored rules
if (result.status === FormatResult.IGNORED) {
return `\n${logSymbols.info} ${result.ruleInfo.name}: ${result.runMessage}`
}
// log all others
return SymbolFormatter.formatResult(
result.lintResult,
result.ruleInfo.name,
result.ruleInfo.policyUrl,
result.ruleInfo.policyInfo,
SymbolFormatter.getSymbol(result.ruleInfo.level)
)
})
.join('')
)
// fix section
const fixresults = output.results.filter(r => r.fixResult)
if (fixresults.length > 0) {
ret.push(
chalk.inverse(`\nFix(es) ${dryRun ? 'suggested' : 'applied'}:`) +
fixresults.map(result =>
SymbolFormatter.formatResult(
result.fixResult,
result.ruleInfo.name,
result.ruleInfo.policyUrl,
result.ruleInfo.policyInfo,
SymbolFormatter.getSymbol(result.ruleInfo.level),
dryRun ? logSymbols.info : logSymbols.success
)
)
)
}
return ret.join('')
}
}
module.exports = SymbolFormatter