diff --git a/src/formatters/json.js b/src/formatters/json.js new file mode 100644 index 00000000..9c6cf279 --- /dev/null +++ b/src/formatters/json.js @@ -0,0 +1,47 @@ +/* global JSON */ + +CSSLint.addFormatter({ + //format information + id: "json", + name: "CSSLint JSON format", + json: [], + options: {}, + + /** + * Return content to be printed before all file results. + * @return {String} to prepend before all results + */ + startFormat: function() { + "use strict"; + return ""; + }, + + /** + * Return full JSON + * @return {String} to append after all results + */ + endFormat: function() { + "use strict"; + return JSON.stringify(this.json); + }, + + /** + * Given CSS Lint results for a file, return output for this format. + * @param results {Object} with error and warning messages + * @param filename {String} relative file path + * @param options {Object} (UNUSED for now) specifies special handling of output + * @return {String} output for results + */ + formatResults: function(results, filename, options) { + "use strict"; + options = options || {}; + + if (results.messages.length > 0) { + this.json.push({ + filename: filename, + results: results + }); + } + return ""; + } +}); diff --git a/tests/formatters/json.js b/tests/formatters/json.js new file mode 100644 index 00000000..a58575f2 --- /dev/null +++ b/tests/formatters/json.js @@ -0,0 +1,54 @@ +(function() { + "use strict"; + var Assert = YUITest.Assert; + + YUITest.TestRunner.add(new YUITest.TestCase({ + + name: "JSON formatter", + + "File with no problems should return empty string": function() { + var result = { + messages: [], + stats: [] + }, + actual = CSSLint.getFormatter("json").formatResults(result, "path/to/FILE", { + fullPath: "/absolute/path/to/FILE" + }); + Assert.areEqual("", actual); + }, + + "Files with problems should list them": function() { + var result = { + messages: [{ + type: "warning", + line: 1, + col: 1, + message: "BOGUS", + evidence: "ALSO BOGUS", + rule: [] + }, { + type: "error", + line: 2, + col: 1, + message: "BOGUS", + evidence: "ALSO BOGUS", + rule: [] + }], + stats: [] + }; + + /*jshint -W108 */ + var expected = '[{"filename":"path/to/FILE","results":{"messages":[{"type":"warning","line":1,"col":1,"message":"BOGUS","evidence":"ALSO BOGUS","rule":[]},{"type":"error","line":2,"col":1,"message":"BOGUS","evidence":"ALSO BOGUS","rule":[]}],"stats":[]}}]'; + + var formatter = CSSLint.getFormatter("json"); + + var actual = formatter.startFormat() + formatter.formatResults(result, "path/to/FILE", { + fullPath: "/absolute/path/to/FILE" + }) + formatter.endFormat(); + + Assert.areEqual(expected, actual); + } + + })); + +})();