UNPKG

3.55 kBJavaScriptView Raw
1/**
2 * @fileoverview "table reporter.
3 * @author Gajus Kuizinas <gajus@gajus.com>
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Requirements
9//------------------------------------------------------------------------------
10
11const chalk = require("chalk"),
12 table = require("table").default,
13 pluralize = require("pluralize");
14
15//------------------------------------------------------------------------------
16// Helpers
17//------------------------------------------------------------------------------
18
19/**
20 * Draws text table.
21 * @param {Array<Object>} messages Error messages relating to a specific file.
22 * @returns {string} A text table.
23 */
24function drawTable(messages) {
25 const rows = [];
26
27 if (messages.length === 0) {
28 return "";
29 }
30
31 rows.push([
32 chalk.bold("Line"),
33 chalk.bold("Column"),
34 chalk.bold("Type"),
35 chalk.bold("Message"),
36 chalk.bold("Rule ID")
37 ]);
38
39 messages.forEach(function(message) {
40 let messageType;
41
42 if (message.fatal || message.severity === 2) {
43 messageType = chalk.red("error");
44 } else {
45 messageType = chalk.yellow("warning");
46 }
47
48 rows.push([
49 message.line || 0,
50 message.column || 0,
51 messageType,
52 message.message,
53 message.ruleId || ""
54 ]);
55 });
56
57 return table(rows, {
58 columns: {
59 0: {
60 width: 8,
61 wrapWord: true
62 },
63 1: {
64 width: 8,
65 wrapWord: true
66 },
67 2: {
68 width: 8,
69 wrapWord: true
70 },
71 3: {
72 paddingRight: 5,
73 width: 50,
74 wrapWord: true
75 },
76 4: {
77 width: 20,
78 wrapWord: true
79 }
80 },
81 drawHorizontalLine(index) {
82 return index === 1;
83 }
84 });
85}
86
87/**
88 * Draws a report (multiple tables).
89 * @param {Array} results Report results for every file.
90 * @returns {string} A column of text tables.
91 */
92function drawReport(results) {
93 let files;
94
95 files = results.map(function(result) {
96 if (!result.messages.length) {
97 return "";
98 }
99
100 return "\n" + result.filePath + "\n\n" + drawTable(result.messages);
101 });
102
103 files = files.filter(function(content) {
104 return content.trim();
105 });
106
107 return files.join("");
108}
109
110//------------------------------------------------------------------------------
111// Public Interface
112//------------------------------------------------------------------------------
113
114module.exports = function(report) {
115 let result,
116 errorCount,
117 warningCount;
118
119 result = "";
120 errorCount = 0;
121 warningCount = 0;
122
123 report.forEach(function(fileReport) {
124 errorCount += fileReport.errorCount;
125 warningCount += fileReport.warningCount;
126 });
127
128 if (errorCount || warningCount) {
129 result = drawReport(report);
130 }
131
132 result += "\n" + table([
133 [
134 chalk.red(pluralize("Error", errorCount, true))
135 ],
136 [
137 chalk.yellow(pluralize("Warning", warningCount, true))
138 ]
139 ], {
140 columns: {
141 0: {
142 width: 110,
143 wrapWord: true
144 }
145 },
146 drawHorizontalLine() {
147 return true;
148 }
149 });
150
151 return result;
152};