From 5654e8eef09fd85d3c7a73b7392f209c50a0b241 Mon Sep 17 00:00:00 2001 From: Ian Christian Myers Date: Mon, 22 Jul 2013 23:40:13 -0700 Subject: [PATCH] Formatter: Checkstyle Created checkstyle formatter based off of the example here: http://www.jshint.com/docs/config/ `eslint -f checkstyle foo.js` will not produce output that looks like this: Fixes #14 --- lib/formatters/checkstyle.js | 51 ++++++++++++ tests/lib/formatters/checkstyle.js | 129 +++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 lib/formatters/checkstyle.js create mode 100644 tests/lib/formatters/checkstyle.js diff --git a/lib/formatters/checkstyle.js b/lib/formatters/checkstyle.js new file mode 100644 index 00000000000..0be3fed473f --- /dev/null +++ b/lib/formatters/checkstyle.js @@ -0,0 +1,51 @@ +/** + * @fileoverview CheckStyle XML reporter + * @author Ian Christian Myers + */ + +//------------------------------------------------------------------------------ +// Helper Functions +//------------------------------------------------------------------------------ + +function getMessageType(message, rules) { + + if (message.fatal || rules[message.ruleId] === 2) { + return "error"; + } else { + return "warning"; + } + +} + +//------------------------------------------------------------------------------ +// Public Interface +//------------------------------------------------------------------------------ + +module.exports = function(results, config) { + + var output = "", + rules = config.rules || {}; + + output += ""; + output += ""; + + results.forEach(function(result) { + var messages = result.messages; + + output += ""; + + messages.forEach(function(message) { + output += ""; + }); + + output += ""; + + }); + + output += ""; + + return output; +}; diff --git a/tests/lib/formatters/checkstyle.js b/tests/lib/formatters/checkstyle.js new file mode 100644 index 00000000000..26073d646e2 --- /dev/null +++ b/tests/lib/formatters/checkstyle.js @@ -0,0 +1,129 @@ +/** + * @fileoverview Tests for checkstyle reporter. + * @author Ian Christian Myers + */ + +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +var vows = require("vows"), + assert = require("assert"), + formatter = require("../../../lib/formatters/checkstyle"); + +//------------------------------------------------------------------------------ +// Tests +//------------------------------------------------------------------------------ + +vows.describe("formatter:checkstyle").addBatch({ + + "when passed a single message": { + + topic: [{ + filePath: "foo.js", + messages: [{ + message: "Unexpected foo.", + line: 5, + column: 10, + ruleId: "foo" + }] + }], + + "should return a string in the format filename: line x, col y, Error - z for errors": function(topic) { + var config = { + rules: { foo: 2 } + }; + + var result = formatter(topic, config); + assert.equal("", result); + }, + + "should return a string in the format filename: line x, col y, Warning - z for warnings": function(topic) { + var config = { + rules: { foo: 1 } + }; + + var result = formatter(topic, config); + assert.equal("", result); + } + + }, + + "when passed a fatal error message": { + + topic: [{ + filePath: "foo.js", + messages: [{ + fatal: true, + message: "Unexpected foo.", + line: 5, + column: 10, + ruleId: "foo" + }] + }], + + "should return a string in the format filename: line x, col y, Error - z": function(topic) { + var config = {}; // doesn't matter what's in the config for this test + + var result = formatter(topic, config); + assert.equal("", result); + } + }, + + "when passed multiple messages": { + topic: [{ + filePath: "foo.js", + messages: [{ + message: "Unexpected foo.", + line: 5, + column: 10, + ruleId: "foo" + }, { + message: "Unexpected bar.", + line: 6, + column: 11, + ruleId: "bar" + }] + }], + + "should return a string with multiple entries": function(topic) { + var config = { + rules: { foo: 2, bar: 1 } + }; + + var result = formatter(topic, config); + assert.equal("", result); + } + + }, + + "when passed multiple files with 1 message each": { + topic: [{ + filePath: "foo.js", + messages: [{ + message: "Unexpected foo.", + line: 5, + column: 10, + ruleId: "foo" + }] + }, { + filePath: "bar.js", + messages: [{ + message: "Unexpected bar.", + line: 6, + column: 11, + ruleId: "bar" + }] + }], + + "should return a string with multiple entries": function(topic) { + var config = { + rules: { foo: 2, bar: 1 } + }; + + var result = formatter(topic, config); + assert.equal("", result); + } + } + + }).export(module);