Blame view

node_modules/eslint/lib/formatters/html.js 2.89 KB
c39994410   Ryan Glover   wip converting to...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  /**
   * @fileoverview HTML reporter
   * @author Julian Laval
   * @copyright 2015 Julian Laval. All rights reserved.
   */
  "use strict";
  
  var handlebars = require("handlebars").create();
  var fs = require("fs");
  var path = require("path");
  
  //------------------------------------------------------------------------------
  // Helpers
  //------------------------------------------------------------------------------
  
  /**
   * Given a word and a count, append an s if count is not one.
   * @param {string} word A word in its singular form.
   * @param {int} count A number controlling whether word should be pluralized.
   * @returns {string} The original word with an s on the end if count is not one.
   */
  function pluralize(word, count) {
      return (count === 1 ? word : word + "s");
  }
  
  /**
   * Renders text along the template of x problems (x errors, x warnings)
   * @param {string} totalErrors Total errors
   * @param {string} totalWarnings Total warnings
   * @returns {string} The formatted string, pluralized where necessary
   */
  handlebars.registerHelper("renderText", function(totalErrors, totalWarnings) {
      var totalProblems = totalErrors + totalWarnings;
      var renderedText = totalProblems + " " + pluralize("problem", totalProblems);
      if (totalProblems !== 0) {
          renderedText += " (" + totalErrors + " " + pluralize("error", totalErrors) + ", " + totalWarnings + " " + pluralize("warning", totalWarnings) + ")";
      }
      return renderedText;
  });
  
  /**
   * Get the color based on whether there are errors/warnings...
   * @param {string} totalErrors Total errors
   * @param {string} totalWarnings Total warnings
   * @returns {int} The color code (0 = green, 1 = yellow, 2 = red)
   */
  handlebars.registerHelper("getColor", function(totalErrors, totalWarnings) {
      if (totalErrors !== 0) {
          return 2;
      } else if (totalWarnings !== 0) {
          return 1;
      }
      return 0;
  });
  
  /**
   * Get the HTML row content based on the severity of the message
   * @param {int} severity Severity of the message
   * @returns {string} The generated HTML row
   */
  handlebars.registerHelper("getSeverity", function(severity) {
      // Return warning else error
      return new handlebars.SafeString((severity === 1) ? "<td class=\"clr-1\">Warning</td>" : "<td class=\"clr-2\">Error</td>");
  });
  
  //------------------------------------------------------------------------------
  // Public Interface
  //------------------------------------------------------------------------------
  
  module.exports = function(results) {
  
      var template = fs.readFileSync(path.join(__dirname, "html-template.html"), "utf-8");
  
      var data = {
          date: new Date(),
          totalErrors: 0,
          totalWarnings: 0,
          results: results
      };
  
      // Iterate over results to get totals
      results.forEach(function(result) {
          data.totalErrors += result.errorCount;
          data.totalWarnings += result.warningCount;
      });
  
      return handlebars.compile(template)(data);
  };