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