-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstylish.js
122 lines (96 loc) · 2.75 KB
/
stylish.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
import path from 'path';
import chalk from 'chalk';
import logSymbols from 'log-symbols';
import table from 'text-table';
let currFile;
let currTable = [];
let filenames = [];
let options;
let optionsRead = false;
function resetState() {
currFile = undefined;
currTable = [];
filenames = [];
options = undefined;
optionsRead = false;
}
function createSummary(errs, warns, total, maxErrors, maxWarnings) {
if (total === 0) {
return 'No violations';
}
let output = '';
if (errs > 0) {
output += ` ${logSymbols.error} ${errs} ${errs > 1 ? 'errors' : 'error'}`;
if (typeof maxErrors === 'number') {
output += ` (Max Errors: ${maxErrors})`;
}
output += '\n';
}
if (warns > 0) {
output += ` ${logSymbols.warning} ${warns} ${
warns > 1 ? 'warnings' : 'warning'
}`;
if (typeof maxWarnings === 'number') {
output += ` (Max Warnings: ${maxWarnings})`;
}
output += '\n';
}
return output;
}
function doneHandler(kill) {
const errs = this.cache.errs.length;
const warns = this.cache.warnings.length;
const total = errs + warns;
const formattedMessage = `${table(currTable)
.split('\n')
.map((msg, i) => (filenames[i] ? `\n${filenames[i]}\n${msg}` : msg))
.join('\n')}\n\n`;
this.cache.msg = `${formattedMessage}${createSummary(
errs,
warns,
total,
this.config.maxErrors,
this.config.maxWarnings
)}`;
if (kill === 'kill') {
this.cache.msg += '\nStylint: Over Error or Warning Limit.';
} else if (total === 0) {
this.cache.msg = '';
}
resetState();
return this.done();
}
export default function(msg, done, kill) {
if (done === 'done') {
return doneHandler.call(this, kill);
}
if (!optionsRead) {
optionsRead = true;
const { absolutePath, verbose, ruleName } =
this.config.reporterOptions || {};
options = { absolutePath, verbose, ruleName };
}
const isWarning = this.state.severity === 'Warning';
if (currFile !== this.cache.file) {
currFile = this.cache.file;
let filename;
if (options.absolutePath) {
filename = path.isAbsolute(currFile) ? currFile : path.resolve(currFile);
} else {
filename = path.isAbsolute(currFile)
? path.relative(process.cwd(), currFile)
: currFile;
}
filenames[currTable.length] = chalk.underline(filename);
}
const column = typeof this.cache.col === 'number' ? this.cache.col : -1;
const rule = options.ruleName ? chalk.gray(this.cache.rule || '') : '';
currTable.push([
'',
chalk.gray(`line ${this.cache.lineNo}`),
chalk.gray(column >= 0 ? `col ${column}` : '-'),
`${isWarning ? chalk.yellow(msg) : chalk.red(msg)} ${rule}`.trim(),
options.verbose ? chalk.gray(this.cache.origLine.trim()) : '',
]);
return '';
}