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 }
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((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(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 /*
67 * If we have multiple messages place them under a messages key
68 * The first error will be logged as message key
69 * This is to adhere to TAP 13 loosely defined specification of having a message key
70 */
71 if ("message" in diagnostics) {
72 if (typeof diagnostics.messages === "undefined") {
73 diagnostics.messages = [];
74 }
75 diagnostics.messages.push(diagnostic);
76 } else {
77 diagnostics = diagnostic;
78 }
79 });
80 }
81
82 output += `${testResult} ${id + 1} - ${result.filePath}\n`;
83
84 // If we have an error include diagnostics
85 if (messages.length > 0) {
86 output += outputDiagnostics(diagnostics);
87 }
88
89 });
90
91 return output;
92};