Skip to content

Commit

Permalink
New: Visual Studio compatible formatter (fixes eslint#4708)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhpijnacker committed Dec 15, 2015
1 parent 3dcd374 commit 38450af
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/user-guide/command-line-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ This option specifies the output format for the console. Possible formats are:
* [junit](formatters/#junit)
* [tap](formatters/#tap)
* [unix](formatters/#unix)
* [visualstudio](formatters/#visualstudio)

Example:

Expand Down
59 changes: 59 additions & 0 deletions lib/formatters/visualstudio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @fileoverview VisualStudio reporter
* @author Ronald Pijnacker
*/
"use strict";

//------------------------------------------------------------------------------
// Helper Functions
//------------------------------------------------------------------------------

/**
* Returns the severity of warning or error
* @param {object} message message object to examine
* @returns {string} severity level
* @private
*/
function getMessageType(message) {
if (message.fatal || message.severity === 2) {
return "[Error] ";
} else {
return "[Warning]";
}
}


//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

module.exports = function(results) {

var output = "",
total = 0;

results.forEach(function(result) {

var messages = result.messages;
total += messages.length;

messages.forEach(function(message) {

output += result.filePath;
output += "(" + (message.line || 0);
output += "," + (message.column || 0);
output += "):\t" + getMessageType(message);
output += " " + message.message;
output += message.ruleId ? "\t(" + message.ruleId + ")" : "";
output += "\n";

});

});

if (total > 0) {
output += "\n" + total + " problem" + (total !== 1 ? "s" : "");
}

return output;
};
139 changes: 139 additions & 0 deletions tests/lib/formatters/visualstudio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**
* @fileoverview Tests for VisualStudio format.
* @author Ronald Pijnacker
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var assert = require("chai").assert,
formatter = require("../../../lib/formatters/visualstudio");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

describe("formatter:visualstudio", function() {
describe("when passed no messages", function() {
var code = [{
filePath: "foo.js",
messages: []
}];

it("should return nothing", function() {
var result = formatter(code);
assert.equal(result, "");
});
});

describe("when passed a single message", function() {
var code = [{
filePath: "foo.js",
messages: [{
message: "Unexpected foo.",
severity: 2,
line: 5,
column: 10,
ruleId: "foo"
}]
}];

it("should return a string in the format filename(x,y):\t[Error] z for errors", function() {
var result = formatter(code);
assert.equal(result, "foo.js(5,10):\t[Error] Unexpected foo.\t(foo)\n\n1 problem");
});

it("should return a string in the format filename(x,y):\t[Warning] z for warnings", function() {
code[0].messages[0].severity = 1;
var result = formatter(code);
assert.equal(result, "foo.js(5,10):\t[Warning] Unexpected foo.\t(foo)\n\n1 problem");
});
});

describe("when passed a fatal error message", function() {
var code = [{
filePath: "foo.js",
messages: [{
fatal: true,
message: "Unexpected foo.",
line: 5,
column: 10,
ruleId: "foo"
}]
}];

it("should return a string in the format filename(x,y):\t[Error] z", function() {
var result = formatter(code);
assert.equal(result, "foo.js(5,10):\t[Error] Unexpected foo.\t(foo)\n\n1 problem");
});
});

describe("when passed multiple messages", function() {
var code = [{
filePath: "foo.js",
messages: [{
message: "Unexpected foo.",
severity: 2,
line: 5,
column: 10,
ruleId: "foo"
}, {
message: "Unexpected bar.",
severity: 1,
line: 6,
column: 11,
ruleId: "bar"
}]
}];

it("should return a string with multiple entries", function() {
var result = formatter(code);
assert.equal(result, "foo.js(5,10):\t[Error] Unexpected foo.\t(foo)\nfoo.js(6,11):\t[Warning] Unexpected bar.\t(bar)\n\n2 problems");
});
});

describe("when passed multiple files with 1 message each", function() {
var code = [{
filePath: "foo.js",
messages: [{
message: "Unexpected foo.",
severity: 2,
line: 5,
column: 10,
ruleId: "foo"
}]
}, {
filePath: "bar.js",
messages: [{
message: "Unexpected bar.",
severity: 1,
line: 6,
column: 11,
ruleId: "bar"
}]
}];

it("should return a string with multiple entries", function() {
var result = formatter(code);
assert.equal(result, "foo.js(5,10):\t[Error] Unexpected foo.\t(foo)\nbar.js(6,11):\t[Warning] Unexpected bar.\t(bar)\n\n2 problems");
});
});

describe("when passed one file not found message", function() {
var code = [{
filePath: "foo.js",
messages: [{
fatal: true,
message: "Couldn't find foo.js."
}]
}];

it("should return a string without line and column", function() {
var result = formatter(code);
assert.equal(result, "foo.js(0,0):\t[Error] Couldn't find foo.js.\n\n1 problem");
});
});
});

0 comments on commit 38450af

Please sign in to comment.