Skip to content
Browse files

Added CheckStyle reporter (Bamboo and Jenkins like this better than J…

…Unit output which immediately fails the build)
  • Loading branch information...
1 parent 3927807 commit 9a092500513df4b90a49f9cfb6921d4716a835a1 @relaxnow relaxnow committed with brentlintner Jul 3, 2012
Showing with 112 additions and 0 deletions.
  1. +5 −0 lib/cli.js
  2. +107 −0 lib/reporters/checkstyle.js
View
5 lib/cli.js
@@ -97,6 +97,7 @@ module.exports = {
'config': ['config', 'custom config file', 'string', false],
'reporter': ['reporter', 'custom reporter', 'string', undefined],
'jslint-reporter': ['jslint-reporter', 'use a jslint compatible xml reporter'],
+ 'checkstyle-reporter': ['checkstyle-reporter', 'use a CheckStyle compatible xml reporter'],
'show-non-errors': ['show-non-errors', 'show additional data generated by jshint'],
'extra-ext': ['extra-ext', 'comma-separated list of file extensions to use (.js is default)', 'string', '']
});
@@ -121,6 +122,10 @@ module.exports = {
customReporter = "./reporters/jslint_xml.js";
}
+ if (options['checkstyle-reporter']) {
+ customReporter = "./reporters/checkstyle.js";
+ }
+
if (options['show-non-errors']) {
customReporter = "./reporters/non_error.js";
}
View
107 lib/reporters/checkstyle.js
@@ -0,0 +1,107 @@
+// Author: Boy Baukema
+// http://github.com/relaxnow
+module.exports =
+{
+ reporter: function (results, data)
+ {
+ "use strict";
+
+ var files = {},
+ out = [],
+ pairs = {
+ "&": "&",
+ '"': """,
+ "'": "'",
+ "<": "&lt;",
+ ">": "&gt;"
+ },
+ file, fileName, i, issue, globals, unuseds;
+
+ function encode(s) {
+ for (var r in pairs) {
+ if (typeof(s) !== "undefined") {
+ s = s.replace(new RegExp(r, "g"), pairs[r]);
+ }
+ }
+ return s || "";
+ }
+
+ results.forEach(function (result) {
+ // Register the file
+ result.file = result.file.replace(/^\.\//, '');
+ if (!files[result.file]) {
+ files[result.file] = [];
+ }
+
+ // Add the error
+ files[result.file].push({
+ severity: 'error',
+ line: result.error.line,
+ column: result.error.character,
+ message: result.error.reason,
+ source: result.error.raw
+ });
+ });
+
+ data.forEach(function (result) {
+ file = data.file;
+ globals = result.implieds;
+ unuseds = result.unused;
+
+ // Register the file
+ result.file = result.file.replace(/^\.\//, '');
+ if (!files[result.file]) {
+ files[result.file] = [];
+ }
+
+ if (globals) {
+ globals.forEach(function (global) {
+ files[result.file].push({
+ severity: 'warning',
+ line: global.line,
+ column: 0,
+ message: "Implied global '" + global.name + "'",
+ source: 'jshint.implied-globals'
+ });
+ });
+ }
+ if (unuseds) {
+ unuseds.forEach(function (unused) {
+ files[result.file].push({
+ severity: 'warning',
+ line: unused.line,
+ column: 0,
+ message: "Unused variable: '" + unused.name + "'",
+ source: 'jshint.implied-unuseds'
+ });
+ });
+ }
+ });
+
+ out.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
+ out.push("<checkstyle version=\"4.3\">");
+
+ for (fileName in files) {
+ if (files.hasOwnProperty(fileName)) {
+ out.push("\t<file name=\"" + fileName + "\">");
+ for (i = 0; i < files[fileName].length; i++) {
+ issue = files[fileName][i];
+ out.push(
+ "\t\t<error " +
+ "line=\"" + issue.line + "\" " +
+ "column=\"" + issue.column + "\" " +
+ "severity=\"" + issue.severity + "\" " +
+ "message=\"" + encode(issue.message) + "\" " +
+ "source=\"" + encode(issue.source) + "\" " +
+ "/>"
+ );
+ }
+ out.push("\t</file>");
+ }
+ }
+
+ out.push("</checkstyle>");
+
+ process.stdout.write(out.join("\n") + "\n");
+ }
+};

0 comments on commit 9a09250

Please sign in to comment.
Something went wrong with that request. Please try again.