Blame view

node_modules/eslint/lib/formatters/stylish.js 2.66 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
89
90
91
92
93
94
95
96
97
  /**
   * @fileoverview Stylish reporter
   * @author Sindre Sorhus
   */
  "use strict";
  
  var chalk = require("chalk"),
      table = require("text-table");
  
  //------------------------------------------------------------------------------
  // 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");
  }
  
  //------------------------------------------------------------------------------
  // Public Interface
  //------------------------------------------------------------------------------
  
  module.exports = function(results) {
  
      var output = "
  ",
          total = 0,
          errors = 0,
          warnings = 0,
          summaryColor = "yellow";
  
      results.forEach(function(result) {
          var messages = result.messages;
  
          if (messages.length === 0) {
              return;
          }
  
          total += messages.length;
          output += chalk.underline(result.filePath) + "
  ";
  
          output += table(
              messages.map(function(message) {
                  var messageType;
  
                  if (message.fatal || message.severity === 2) {
                      messageType = chalk.red("error");
                      summaryColor = "red";
                      errors++;
                  } else {
                      messageType = chalk.yellow("warning");
                      warnings++;
                  }
  
                  return [
                      "",
                      message.line || 0,
                      message.column || 0,
                      messageType,
                      message.message.replace(/\.$/, ""),
                      chalk.gray(message.ruleId || "")
                  ];
              }),
              {
                  align: ["", "r", "l"],
                  stringLength: function(str) {
                      return chalk.stripColor(str).length;
                  }
              }
          ).split("
  ").map(function(el) {
              return el.replace(/(\d+)\s+(\d+)/, function(m, p1, p2) {
                  return chalk.gray(p1 + ":" + p2);
              });
          }).join("
  ") + "
  
  ";
      });
  
      if (total > 0) {
          output += chalk[summaryColor].bold([
              "\u2716 ", total, pluralize(" problem", total),
              " (", errors, pluralize(" error", errors), ", ",
              warnings, pluralize(" warning", warnings), ")
  "
          ].join(""));
      }
  
      return total > 0 ? output : "";
  };