UNPKG

3.49 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").table,
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(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(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(content => content.trim());
104
105 return files.join("");
106}
107
108//------------------------------------------------------------------------------
109// Public Interface
110//------------------------------------------------------------------------------
111
112module.exports = function(report) {
113 let result,
114 errorCount,
115 warningCount;
116
117 result = "";
118 errorCount = 0;
119 warningCount = 0;
120
121 report.forEach(fileReport => {
122 errorCount += fileReport.errorCount;
123 warningCount += fileReport.warningCount;
124 });
125
126 if (errorCount || warningCount) {
127 result = drawReport(report);
128 }
129
130 result += `\n${table([
131 [
132 chalk.red(pluralize("Error", errorCount, true))
133 ],
134 [
135 chalk.yellow(pluralize("Warning", warningCount, true))
136 ]
137 ], {
138 columns: {
139 0: {
140 width: 110,
141 wrapWord: true
142 }
143 },
144 drawHorizontalLine() {
145 return true;
146 }
147 })}`;
148
149 return result;
150};