UNPKG

4.18 kBJavaScriptView Raw
1/**
2 * @fileoverview HTML reporter
3 * @author Julian Laval
4 */
5"use strict";
6
7const lodash = require("lodash");
8const fs = require("fs");
9const path = require("path");
10
11//------------------------------------------------------------------------------
12// Helpers
13//------------------------------------------------------------------------------
14
15const pageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-page.html"), "utf-8"));
16const messageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-message.html"), "utf-8"));
17const resultTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-result.html"), "utf-8"));
18
19/**
20 * Given a word and a count, append an s if count is not one.
21 * @param {string} word A word in its singular form.
22 * @param {int} count A number controlling whether word should be pluralized.
23 * @returns {string} The original word with an s on the end if count is not one.
24 */
25function pluralize(word, count) {
26 return (count === 1 ? word : `${word}s`);
27}
28
29/**
30 * Renders text along the template of x problems (x errors, x warnings)
31 * @param {string} totalErrors Total errors
32 * @param {string} totalWarnings Total warnings
33 * @returns {string} The formatted string, pluralized where necessary
34 */
35function renderSummary(totalErrors, totalWarnings) {
36 const totalProblems = totalErrors + totalWarnings;
37 let renderedText = `${totalProblems} ${pluralize("problem", totalProblems)}`;
38
39 if (totalProblems !== 0) {
40 renderedText += ` (${totalErrors} ${pluralize("error", totalErrors)}, ${totalWarnings} ${pluralize("warning", totalWarnings)})`;
41 }
42 return renderedText;
43}
44
45/**
46 * Get the color based on whether there are errors/warnings...
47 * @param {string} totalErrors Total errors
48 * @param {string} totalWarnings Total warnings
49 * @returns {int} The color code (0 = green, 1 = yellow, 2 = red)
50 */
51function renderColor(totalErrors, totalWarnings) {
52 if (totalErrors !== 0) {
53 return 2;
54 }
55 if (totalWarnings !== 0) {
56 return 1;
57 }
58 return 0;
59}
60
61/**
62 * Get HTML (table rows) describing the messages.
63 * @param {Array} messages Messages.
64 * @param {int} parentIndex Index of the parent HTML row.
65 * @returns {string} HTML (table rows) describing the messages.
66 */
67function renderMessages(messages, parentIndex) {
68
69 /**
70 * Get HTML (table row) describing a message.
71 * @param {Object} message Message.
72 * @returns {string} HTML (table row) describing a message.
73 */
74 return lodash.map(messages, message => {
75 const lineNumber = message.line || 0;
76 const columnNumber = message.column || 0;
77
78 return messageTemplate({
79 parentIndex,
80 lineNumber,
81 columnNumber,
82 severityNumber: message.severity,
83 severityName: message.severity === 1 ? "Warning" : "Error",
84 message: message.message,
85 ruleId: message.ruleId
86 });
87 }).join("\n");
88}
89
90/**
91 * @param {Array} results Test results.
92 * @returns {string} HTML string describing the results.
93 */
94function renderResults(results) {
95 return lodash.map(results, (result, index) => resultTemplate({
96 index,
97 color: renderColor(result.errorCount, result.warningCount),
98 filePath: result.filePath,
99 summary: renderSummary(result.errorCount, result.warningCount)
100
101 }) + renderMessages(result.messages, index)).join("\n");
102}
103
104//------------------------------------------------------------------------------
105// Public Interface
106//------------------------------------------------------------------------------
107
108module.exports = function(results) {
109 let totalErrors,
110 totalWarnings;
111
112 totalErrors = 0;
113 totalWarnings = 0;
114
115 // Iterate over results to get totals
116 results.forEach(result => {
117 totalErrors += result.errorCount;
118 totalWarnings += result.warningCount;
119 });
120
121 return pageTemplate({
122 date: new Date(),
123 reportColor: renderColor(totalErrors, totalWarnings),
124 reportSummary: renderSummary(totalErrors, totalWarnings),
125 results: renderResults(results)
126 });
127};