-
-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathstylish.ts
96 lines (80 loc) · 3.35 KB
/
stylish.ts
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
/**
* Copyright JS Foundation and other contributors, https://js.foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @fileoverview Stylish reporter
* @author Sindre Sorhus
*/
import { DiagnosticSeverity, IRange } from '@stoplight/types';
import * as chalk from 'chalk';
import stripAnsi = require('strip-ansi');
import * as table from 'text-table';
import { IRuleResult } from '../../types';
import { Formatter } from './types';
import { getColorForSeverity, getHighestSeverity, getSeverityName, getSummary, groupBySource } from './utils';
import { printPath, PrintStyle } from '../../utils';
// -----------------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------------
function formatRange(range?: IRange): string {
if (range === void 0) return '';
return ` ${range.start.line + 1}:${range.start.character + 1}`;
}
function getMessageType(severity: DiagnosticSeverity): string {
const color = getColorForSeverity(severity);
const name = getSeverityName(severity);
return chalk[color](name);
}
// -----------------------------------------------------------------------------
// Public Interface
// -----------------------------------------------------------------------------
export const stylish: Formatter = results => {
let output = '\n';
const groupedResults = groupBySource(results);
const summaryColor = getColorForSeverity(getHighestSeverity(results));
const summaryText = getSummary(groupedResults);
Object.keys(groupedResults).map(path => {
const pathResults = groupedResults[path];
output += `${chalk.underline(path)}\n`;
const pathTableData = pathResults.map((result: IRuleResult) => [
formatRange(result.range),
getMessageType(result.severity),
result.code ?? '',
result.message,
printPath(result.path, PrintStyle.Dot),
]);
output += `${table(pathTableData, {
align: ['c', 'r', 'l'],
stringLength(str) {
return stripAnsi(str).length;
},
})
.split('\n')
.map((el: string) =>
el.replace(/(\d+)\s+(\d+)/u, (m: string, p1: string, p2: string) => chalk.dim(`${p1}:${p2}`)),
)
.join('\n')}\n\n`;
});
if (summaryText === null) {
return '';
}
output += chalk[summaryColor].bold(`\u2716 ${summaryText}\n`);
return output;
};