Skip to content

Commit fc97414

Browse files
committed
Add a JSON formatter
Add a JSON formatter option, allowing easier use in tools that have native modes of parsing this output.
1 parent b06d61d commit fc97414

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/formatters/json.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
CSSLint.addFormatter({
2+
//format information
3+
id: "json",
4+
name: "JSON",
5+
6+
/**
7+
* Return content to be printed before all file results.
8+
* @return {String} to prepend before all results
9+
*/
10+
startFormat: function() {
11+
"use strict";
12+
return "";
13+
},
14+
15+
/**
16+
* Return content to be printed after all file results.
17+
* @return {String} to append after all results
18+
*/
19+
endFormat: function() {
20+
"use strict";
21+
return "";
22+
},
23+
24+
/**
25+
* Given CSS Lint results for a file, return output for this format.
26+
* @param results {Object} with error and warning messages
27+
* @return {String} output for results
28+
*/
29+
formatResults: function(results, filename, options) {
30+
"use strict";
31+
return options.quiet ? "" : JSON.stringify(results);
32+
}
33+
});

tests/formatters/json.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(function(){
2+
"use strict";
3+
var Assert = YUITest.Assert;
4+
5+
YUITest.TestRunner.add(new YUITest.TestCase({
6+
7+
name: "JSON formatter",
8+
9+
"File with no problems should say so": function() {
10+
var result = { messages: [], stats: [] },
11+
actual = CSSLint.getFormatter("json").formatResults(result, "path/to/FILE", {fullPath: "/absolute/path/to/FILE"});
12+
Assert.areEqual("{\"messages\":[],\"stats\":[]}", actual);
13+
},
14+
15+
"Should have no output when quiet option is specified and no errors": function() {
16+
var result = { messages: [], stats: [] },
17+
actual = CSSLint.getFormatter("json").formatResults(result, "path/to/FILE", {fullPath: "/absolute/path/to/FILE", quiet: "true"});
18+
Assert.areEqual("", actual);
19+
},
20+
21+
"File with problems should list them": function() {
22+
var result = { messages: [
23+
{ type: "warning", line: 1, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: [] },
24+
{ type: "error", line: 2, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: [] }
25+
], stats: [] },
26+
expected = "{\"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\":[]}",
27+
actual = CSSLint.getFormatter("json").formatResults(result, "path/to/FILE", {fullPath: "/absolute/path/to/FILE"});
28+
Assert.areEqual(expected, actual);
29+
}
30+
31+
}));
32+
33+
})();

0 commit comments

Comments
 (0)