/
formatter.ts
187 lines (172 loc) · 5.55 KB
/
formatter.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
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
181
182
183
184
185
186
187
// LICENSE : MIT
// Original code is https://github.com/azer/prettify-error
// Author : azer
"use strict";
import { TextlintFixResult, TextlintMessage, TextlintResult } from "@textlint/kernel";
export interface TextLintFormatterOption {
formatterName: string;
color?: boolean;
}
const format = require("@azu/format-text");
const chalk = require("chalk");
const padStart = require("string.prototype.padstart");
const style = require("@azu/style-format");
const stripAnsi = require("strip-ansi");
const pluralize = require("pluralize");
// width is 2
const widthOfString = require("string-width");
// color set
let summaryColor = "yellow";
let greenColor = "green";
const template = style(
"{grey}{ruleId}: {red}{title}{reset}\n" +
"{grey}{filename}{reset}\n" +
" {red}{paddingForLineNo} {v}{reset}\n" +
" {grey}{previousLineNo}. {previousLine}{reset}\n" +
" {reset}{failingLineNo}. {failingLine}{reset}\n" +
" {grey}{nextLineNo}. {nextLine}{reset}\n" +
" {red}{paddingForLineNo} {^}{reset}\n" +
""
);
/**
*
* @param {string} code
* @param {TextLintMessage} message
* @returns {*}
*/
function failingCode(code: string, message: TextlintMessage): any {
let result = [];
const lines = code.split("\n");
let i = message.line - 3;
while (++i < message.line + 1) {
if (i + 1 !== message.line) {
result.push({
line: message.line - (message.line - i - 1),
code: lines[i]
});
continue;
}
result.push({
line: message.line,
col: message.column,
code: lines[i],
failed: true
});
}
return result;
}
function showColumn(codes: string, ch: string): string {
let result = "";
const codeObject: any = codes[1];
const sliced = codeObject.code.slice(0, codeObject.col);
const width = widthOfString(sliced);
if (width <= 0) {
return "";
}
let i = width - 1;
while (i--) {
result += " ";
}
return result + ch;
}
/**
*
* @param {string} code
* @param {string} filePath
* @param {TextLintMessage} message
* @returns {*}
*/
function prettyError(code: string, filePath: string, message: TextlintMessage): any {
if (!code) {
return;
}
const parsed = failingCode(code, message);
const previousLineNo = String(parsed[0].line);
const failingLineNo = String(parsed[1].line);
const nextLineNo = String(parsed[2].line);
const linumlen = Math.max(previousLineNo.length, failingLineNo.length, nextLineNo.length);
return format(template, {
ruleId: message.ruleId,
title: message.message,
filename: filePath + ":" + message.line + ":" + message.column,
previousLine: parsed[0].code ? parsed[0].code : "",
previousLineNo: padStart(previousLineNo, linumlen),
previousColNo: parsed[0].col,
failingLine: parsed[1].code,
failingLineNo: padStart(failingLineNo, linumlen),
failingColNo: parsed[1].col,
nextLine: parsed[2].code ? parsed[2].code : "",
nextLineNo: padStart(nextLineNo, linumlen),
nextColNo: parsed[2].col,
paddingForLineNo: padStart("", linumlen),
"^": showColumn(parsed, "^"),
v: showColumn(parsed, "v")
});
}
export function formatOutput(text: string, formatterOutput?: string, output?: string) {
return `${text}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
${output ? output : "[[NO OUTPUT]]"}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
${formatterOutput ? formatterOutput : "[[NO LINT MESSAGE]]"}`;
}
export function testerFormatter(
{ text, result, fixResult }: { text: string; result: TextlintResult; fixResult?: TextlintFixResult },
options: TextLintFormatterOption
) {
// default: true
const useColor = options.color !== undefined ? options.color : true;
let output = "";
let total = 0;
let errors = 0;
let warnings = 0;
let totalFixable = 0;
const code = text || require("fs").readFileSync(result.filePath, "utf-8");
const messages = result.messages;
if (messages.length === 0) {
return formatOutput(text);
}
total += messages.length;
messages.forEach(function(message) {
// fixable
const fixableIcon = message.fix ? chalk[greenColor].bold("\u2713 ") : "";
if (message.fix) {
totalFixable++;
}
if ((message as any).fatal || message.severity === 2) {
errors++;
} else {
warnings++;
}
const r = fixableIcon + prettyError(code, result.filePath, message);
if (r) {
output += r + "\n";
}
});
if (total > 0) {
output += chalk[summaryColor].bold(
[
"\u2716 ",
total,
pluralize(" problem", total),
" (",
errors,
pluralize(" error", errors),
", ",
warnings,
pluralize(" warning", warnings),
")\n"
].join("")
);
}
if (totalFixable > 0) {
output += chalk[greenColor].bold(
"✓ " + totalFixable + " fixable " + pluralize("problem", totalFixable) + ".\n"
);
output += "Try to run: $ " + chalk.underline("textlint --fix [file]") + "\n";
}
if (!useColor) {
return stripAnsi(formatOutput(text, output, fixResult ? fixResult.output : undefined));
}
return output;
}