[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Stylish reporter
|
---|
| 3 | * @author Sindre Sorhus
|
---|
| 4 | */
|
---|
| 5 | "use strict";
|
---|
| 6 |
|
---|
| 7 | const chalk = require("chalk"),
|
---|
| 8 | stripAnsi = require("strip-ansi"),
|
---|
| 9 | table = require("text-table");
|
---|
| 10 |
|
---|
| 11 | //------------------------------------------------------------------------------
|
---|
| 12 | // Helpers
|
---|
| 13 | //------------------------------------------------------------------------------
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 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.
|
---|
| 20 | */
|
---|
| 21 | function pluralize(word, count) {
|
---|
| 22 | return (count === 1 ? word : `${word}s`);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | //------------------------------------------------------------------------------
|
---|
| 26 | // Public Interface
|
---|
| 27 | //------------------------------------------------------------------------------
|
---|
| 28 |
|
---|
| 29 | module.exports = function(results) {
|
---|
| 30 |
|
---|
| 31 | let output = "\n",
|
---|
| 32 | errorCount = 0,
|
---|
| 33 | warningCount = 0,
|
---|
| 34 | fixableErrorCount = 0,
|
---|
| 35 | fixableWarningCount = 0,
|
---|
| 36 | summaryColor = "yellow";
|
---|
| 37 |
|
---|
| 38 | results.forEach(result => {
|
---|
| 39 | const messages = result.messages;
|
---|
| 40 |
|
---|
| 41 | if (messages.length === 0) {
|
---|
| 42 | return;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | errorCount += result.errorCount;
|
---|
| 46 | warningCount += result.warningCount;
|
---|
| 47 | fixableErrorCount += result.fixableErrorCount;
|
---|
| 48 | fixableWarningCount += result.fixableWarningCount;
|
---|
| 49 |
|
---|
| 50 | output += `${chalk.underline(result.filePath)}\n`;
|
---|
| 51 |
|
---|
| 52 | output += `${table(
|
---|
| 53 | messages.map(message => {
|
---|
| 54 | let messageType;
|
---|
| 55 |
|
---|
| 56 | if (message.fatal || message.severity === 2) {
|
---|
| 57 | messageType = chalk.red("error");
|
---|
| 58 | summaryColor = "red";
|
---|
| 59 | } else {
|
---|
| 60 | messageType = chalk.yellow("warning");
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | return [
|
---|
| 64 | "",
|
---|
| 65 | message.line || 0,
|
---|
| 66 | message.column || 0,
|
---|
| 67 | messageType,
|
---|
| 68 | message.message.replace(/([^ ])\.$/u, "$1"),
|
---|
| 69 | chalk.dim(message.ruleId || "")
|
---|
| 70 | ];
|
---|
| 71 | }),
|
---|
| 72 | {
|
---|
| 73 | align: ["", "r", "l"],
|
---|
| 74 | stringLength(str) {
|
---|
| 75 | return stripAnsi(str).length;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | ).split("\n").map(el => el.replace(/(\d+)\s+(\d+)/u, (m, p1, p2) => chalk.dim(`${p1}:${p2}`))).join("\n")}\n\n`;
|
---|
| 79 | });
|
---|
| 80 |
|
---|
| 81 | const total = errorCount + warningCount;
|
---|
| 82 |
|
---|
| 83 | if (total > 0) {
|
---|
| 84 | output += chalk[summaryColor].bold([
|
---|
| 85 | "\u2716 ", total, pluralize(" problem", total),
|
---|
| 86 | " (", errorCount, pluralize(" error", errorCount), ", ",
|
---|
| 87 | warningCount, pluralize(" warning", warningCount), ")\n"
|
---|
| 88 | ].join(""));
|
---|
| 89 |
|
---|
| 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"
|
---|
| 95 | ].join(""));
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | // Resets output color, for prevent change on top level
|
---|
| 100 | return total > 0 ? chalk.reset(output) : "";
|
---|
| 101 | };
|
---|