UNPKG

2.92 kBJavaScriptView Raw
1/**
2 * @fileoverview TAP reporter
3 * @author Jonathan Kingston
4 */
5"use strict";
6
7const yaml = require("js-yaml");
8
9//------------------------------------------------------------------------------
10// Helper Functions
11//------------------------------------------------------------------------------
12
13/**
14 * Returns a canonical error level string based upon the error message passed in.
15 * @param {Object} message Individual error message provided by eslint
16 * @returns {string} Error level string
17 */
18function getMessageType(message) {
19 if (message.fatal || message.severity === 2) {
20 return "error";
21 } else {
22 return "warning";
23 }
24}
25
26/**
27 * Takes in a JavaScript object and outputs a TAP diagnostics string
28 * @param {Object} diagnostic JavaScript object to be embedded as YAML into output.
29 * @returns {string} diagnostics string with YAML embedded - TAP version 13 compliant
30 */
31function outputDiagnostics(diagnostic) {
32 const prefix = " ";
33 let output = prefix + "---\n";
34
35 output += prefix + yaml.safeDump(diagnostic).split("\n").join("\n" + prefix);
36 output += "...\n";
37 return output;
38}
39
40//------------------------------------------------------------------------------
41// Public Interface
42//------------------------------------------------------------------------------
43
44module.exports = function(results) {
45 let output = "TAP version 13\n1.." + results.length + "\n";
46
47 results.forEach(function(result, id) {
48 const messages = result.messages;
49 let testResult = "ok";
50 let diagnostics = {};
51
52 if (messages.length > 0) {
53 testResult = "not ok";
54
55 messages.forEach(function(message) {
56 const diagnostic = {
57 message: message.message,
58 severity: getMessageType(message),
59 data: {
60 line: message.line || 0,
61 column: message.column || 0,
62 ruleId: message.ruleId || ""
63 }
64 };
65
66 // If we have multiple messages place them under a messages key
67 // The first error will be logged as message key
68 // This is to adhere to TAP 13 loosely defined specification of having a message key
69 if ("message" in diagnostics) {
70 if (typeof diagnostics.messages === "undefined") {
71 diagnostics.messages = [];
72 }
73 diagnostics.messages.push(diagnostic);
74 } else {
75 diagnostics = diagnostic;
76 }
77 });
78 }
79
80 output += testResult + " " + (id + 1) + " - " + result.filePath + "\n";
81
82 // If we have an error include diagnostics
83 if (messages.length > 0) {
84 output += outputDiagnostics(diagnostics);
85 }
86
87 });
88
89 return output;
90};