Blame view

node_modules/eslint/lib/formatters/tap.js 2.71 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
  /**
   * @fileoverview TAP reporter
   * @author Jonathan Kingston
   */
  "use strict";
  
  var yaml = require("js-yaml");
  
  //------------------------------------------------------------------------------
  // Helper Functions
  //------------------------------------------------------------------------------
  
  /**
   * Returns a canonical error level string based upon the error message passed in.
   * @param {object} message Individual error message provided by eslint
   * @returns {String} Error level string
   */
  function getMessageType(message) {
      if (message.fatal || message.severity === 2) {
          return "error";
      } else {
          return "warning";
      }
  }
  
  /**
   * Takes in a JavaScript object and outputs a TAP diagnostics string
   * @param {object} diagnostic JavaScript object to be embedded as YAML into output.
   * @returns {string} diagnostics string with YAML embedded - TAP version 13 compliant
   */
  function outputDiagnostics(diagnostic) {
      var prefix = "  ";
      var output = prefix + "---
  ";
      output += prefix + yaml.safeDump(diagnostic).split("
  ").join("
  " + prefix);
      output += "...
  ";
      return output;
  }
  
  //------------------------------------------------------------------------------
  // Public Interface
  //------------------------------------------------------------------------------
  
  module.exports = function(results) {
      var output = "TAP version 13
  1.." + results.length + "
  ";
  
      results.forEach(function(result, id) {
          var messages = result.messages;
          var testResult = "ok";
          var diagnostics = {};
  
          if (messages.length > 0) {
              testResult = "not ok";
  
              messages.forEach(function(message) {
                  var diagnostic = {
                      message: message.message,
                      severity: getMessageType(message),
                      data: {
                          line: message.line || 0,
                          column: message.column || 0,
                          ruleId: message.ruleId || ""
                      }
                  };
  
                  // If we have multiple messages place them under a messages key
                  // The first error will be logged as message key
                  // This is to adhere to TAP 13 loosely defined specification of having a message key
                  if ("message" in diagnostics) {
                      diagnostics.messages = [diagnostic];
                  } else {
                      diagnostics = diagnostic;
                  }
              });
          }
  
          output += testResult + " " + (id + 1) + " - " + result.filePath + "
  ";
  
          // If we have an error include diagnostics
          if (messages.length > 0) {
              output += outputDiagnostics(diagnostics);
          }
  
      });
  
      return output;
  };