2 * @fileoverview "table reporter.
3 * @author Gajus Kuizinas <gajus@gajus.com>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const chalk = require("chalk"),
12 table = require("table").table;
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
19 * Given a word and a count, append an "s" if count is not one.
20 * @param {string} word A word.
21 * @param {number} count Quantity.
22 * @returns {string} The original word with an s on the end if count is not one.
24 function pluralize(word, count) {
25 return (count === 1 ? word : `${word}s`);
30 * @param {Array<Object>} messages Error messages relating to a specific file.
31 * @returns {string} A text table.
33 function drawTable(messages) {
36 if (messages.length === 0) {
44 chalk.bold("Message"),
48 messages.forEach(message => {
51 if (message.fatal || message.severity === 2) {
52 messageType = chalk.red("error");
54 messageType = chalk.yellow("warning");
90 drawHorizontalLine(index) {
97 * Draws a report (multiple tables).
98 * @param {Array} results Report results for every file.
99 * @returns {string} A column of text tables.
101 function drawReport(results) {
104 files = results.map(result => {
105 if (!result.messages.length) {
109 return `\n${result.filePath}\n\n${drawTable(result.messages)}`;
112 files = files.filter(content => content.trim());
114 return files.join("");
117 //------------------------------------------------------------------------------
119 //------------------------------------------------------------------------------
121 module.exports = function(report) {
130 report.forEach(fileReport => {
131 errorCount += fileReport.errorCount;
132 warningCount += fileReport.warningCount;
135 if (errorCount || warningCount) {
136 result = drawReport(report);
139 result += `\n${table([
141 chalk.red(pluralize(`${errorCount} Error`, errorCount))
144 chalk.yellow(pluralize(`${warningCount} Warning`, warningCount))
153 drawHorizontalLine() {