2 * @fileoverview TAP reporter
3 * @author Jonathan Kingston
7 const yaml = require("js-yaml");
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
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
18 function getMessageType(message) {
19 if (message.fatal || message.severity === 2) {
26 * Takes in a JavaScript object and outputs a TAP diagnostics string
27 * @param {Object} diagnostic JavaScript object to be embedded as YAML into output.
28 * @returns {string} diagnostics string with YAML embedded - TAP version 13 compliant
30 function outputDiagnostics(diagnostic) {
32 let output = `${prefix}---\n`;
34 output += prefix + yaml.safeDump(diagnostic).split("\n").join(`\n${prefix}`);
39 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
43 module.exports = function(results) {
44 let output = `TAP version 13\n1..${results.length}\n`;
46 results.forEach((result, id) => {
47 const messages = result.messages;
48 let testResult = "ok";
51 if (messages.length > 0) {
52 messages.forEach(message => {
53 const severity = getMessageType(message);
55 message: message.message,
58 line: message.line || 0,
59 column: message.column || 0,
60 ruleId: message.ruleId || ""
64 // This ensures a warning message is not flagged as error
65 if (severity === "error") {
66 testResult = "not ok";
70 * If we have multiple messages place them under a messages key
71 * The first error will be logged as message key
72 * This is to adhere to TAP 13 loosely defined specification of having a message key
74 if ("message" in diagnostics) {
75 if (typeof diagnostics.messages === "undefined") {
76 diagnostics.messages = [];
78 diagnostics.messages.push(diagnostic);
80 diagnostics = diagnostic;
85 output += `${testResult} ${id + 1} - ${result.filePath}\n`;
87 // If we have an error include diagnostics
88 if (messages.length > 0) {
89 output += outputDiagnostics(diagnostics);