/
stylish.ts
101 lines (93 loc) · 3.18 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
97
98
99
100
101
"use strict";
const chalk = require("chalk");
const table = require("text-table");
const widthOfString = require("string-width");
import { TextlintFixResult } from "@textlint/types";
/**
* Given a word and a count, append an s if count is not one.
* @param {string} word A word in its singular form.
* @param {number} count A number controlling whether word should be pluralized.
* @returns {string} The original word with an s on the end if count is not one.
*/
function pluralize(word: string, count: number): string {
return count === 1 ? word : `${word}s`;
}
export default function(results: TextlintFixResult[], options: any) {
// default: true
chalk.enabled = options.color !== undefined ? options.color : true;
let output = "\n";
let totalFixed = 0;
let errors = 0;
const summaryColor = "yellow";
const greenColor = "green";
results.forEach(function(result) {
if (!result.applyingMessages || !result.remainingMessages) {
return;
}
const messages = result.applyingMessages;
// still error count
const remainingMessages = result.remainingMessages;
errors += remainingMessages.length;
if (messages.length === 0) {
return;
}
output += `${chalk.underline(result.filePath)}\n`;
output += `${table(
messages.map(function(message) {
// fixable
totalFixed++;
const messageType = chalk[greenColor].bold("\u2714 ");
return [
"",
message.line || 0,
message.column || 0,
messageType,
message.message.replace(/\.$/, ""),
chalk.gray(message.ruleId || "")
];
}),
{
align: ["", "r", "l"],
stringLength: (str: string) => {
const lines = chalk.stripColor(str).split("\n");
return Math.max.apply(
null,
lines.map(function(line: string) {
return widthOfString(line);
})
);
}
}
)
.split("\n")
.map(function(el: string) {
return el.replace(/(\d+)\s+(\d+)/, function(_m, p1, p2) {
return chalk.gray(`${p1}:${p2}`);
});
})
.join("\n")}\n\n`;
});
if (totalFixed > 0) {
output += chalk[greenColor].bold(
[
// http://www.fileformat.info/info/unicode/char/2714/index.htm
"\u2714 Fixed ",
totalFixed,
pluralize(" problem", totalFixed),
"\n"
].join("")
);
}
if (errors > 0) {
output += chalk[summaryColor].bold(
[
// http://www.fileformat.info/info/unicode/char/2716/index.htm
"\u2716 Remaining ",
errors,
pluralize(" problem", errors),
"\n"
].join("")
);
}
return totalFixed > 0 ? output : "";
}