2 * @fileoverview Stylish reporter
3 * @author Sindre Sorhus
7 const chalk = require("chalk"),
8 stripAnsi = require("strip-ansi"),
9 table = require("text-table");
11 //------------------------------------------------------------------------------
13 //------------------------------------------------------------------------------
16 * Given a word and a count, append an s if count is not one.
17 * @param {string} word A word in its singular form.
18 * @param {int} count A number controlling whether word should be pluralized.
19 * @returns {string} The original word with an s on the end if count is not one.
21 function pluralize(word, count) {
22 return (count === 1 ? word : `${word}s`);
25 //------------------------------------------------------------------------------
27 //------------------------------------------------------------------------------
29 module.exports = function(results) {
34 fixableErrorCount = 0,
35 fixableWarningCount = 0,
36 summaryColor = "yellow";
38 results.forEach(result => {
39 const messages = result.messages;
41 if (messages.length === 0) {
45 errorCount += result.errorCount;
46 warningCount += result.warningCount;
47 fixableErrorCount += result.fixableErrorCount;
48 fixableWarningCount += result.fixableWarningCount;
50 output += `${chalk.underline(result.filePath)}\n`;
53 messages.map(message => {
56 if (message.fatal || message.severity === 2) {
57 messageType = chalk.red("error");
60 messageType = chalk.yellow("warning");
68 message.message.replace(/([^ ])\.$/u, "$1"),
69 chalk.dim(message.ruleId || "")
73 align: ["", "r", "l"],
75 return stripAnsi(str).length;
78 ).split("\n").map(el => el.replace(/(\d+)\s+(\d+)/u, (m, p1, p2) => chalk.dim(`${p1}:${p2}`))).join("\n")}\n\n`;
81 const total = errorCount + warningCount;
84 output += chalk[summaryColor].bold([
85 "\u2716 ", total, pluralize(" problem", total),
86 " (", errorCount, pluralize(" error", errorCount), ", ",
87 warningCount, pluralize(" warning", warningCount), ")\n"
90 if (fixableErrorCount > 0 || fixableWarningCount > 0) {
91 output += chalk[summaryColor].bold([
92 " ", fixableErrorCount, pluralize(" error", fixableErrorCount), " and ",
93 fixableWarningCount, pluralize(" warning", fixableWarningCount),
94 " potentially fixable with the `--fix` option.\n"
99 // Resets output color, for prevent change on top level
100 return total > 0 ? chalk.reset(output) : "";