Skip to content

Commit

Permalink
Add JSON formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Chernyshev committed Feb 25, 2016
1 parent 9c62146 commit 503b16a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/formatters/json.js
Original file line number Diff line number Diff line change
@@ -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 "";
}
});
54 changes: 54 additions & 0 deletions tests/formatters/json.js
Original file line number Diff line number Diff line change
@@ -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);
}

}));

})();

0 comments on commit 503b16a

Please sign in to comment.