Skip to content

Commit 6334170

Browse files
committed
Merge pull request CSSLint#606 from Arcanemagus/json-formatter
Add a JSON formatter
2 parents 81d9996 + 828bbd5 commit 6334170

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

src/formatters/json.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* globals JSON: true */
2+
3+
CSSLint.addFormatter({
4+
//format information
5+
id: "json",
6+
name: "JSON",
7+
8+
/**
9+
* Return content to be printed before all file results.
10+
* @return {String} to prepend before all results
11+
*/
12+
startFormat: function() {
13+
"use strict";
14+
this.json = [];
15+
return "";
16+
},
17+
18+
/**
19+
* Return content to be printed after all file results.
20+
* @return {String} to append after all results
21+
*/
22+
endFormat: function() {
23+
"use strict";
24+
var ret = "";
25+
if (this.json.length > 0) {
26+
if (this.json.length === 1) {
27+
ret = JSON.stringify(this.json[0]);
28+
} else {
29+
ret = JSON.stringify(this.json);
30+
}
31+
}
32+
return ret;
33+
},
34+
35+
/**
36+
* Given CSS Lint results for a file, return output for this format.
37+
* @param results {Object} with error and warning messages
38+
* @param filename {String} relative file path (Unused)
39+
* @return {String} output for results
40+
*/
41+
formatResults: function(results, filename, options) {
42+
"use strict";
43+
if (results.messages.length > 0 || !options.quiet) {
44+
this.json.push({
45+
filename: filename,
46+
messages: results.messages,
47+
stats: results.stats
48+
});
49+
}
50+
return "";
51+
}
52+
});

tests/formatters/json.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
var expected = "{\"filename\":\"path/to/FILE\",\"messages\":[],\"stats\":[]}";
12+
var formatter = CSSLint.getFormatter("json");
13+
var actual = formatter.startFormat() +
14+
formatter.formatResults(result, "path/to/FILE",
15+
{fullPath: "/absolute/path/to/FILE"}) +
16+
formatter.endFormat();
17+
Assert.areEqual(expected, actual);
18+
},
19+
20+
"Should have no output when quiet option is specified and no errors": function() {
21+
var result = { messages: [], stats: [] };
22+
var formatter = CSSLint.getFormatter("json");
23+
var actual = formatter.startFormat() +
24+
formatter.formatResults(result, "path/to/FILE",
25+
{fullPath: "/absolute/path/to/FILE", quiet: "true"}) +
26+
formatter.endFormat();
27+
Assert.areEqual("", actual);
28+
},
29+
30+
"Should have output when quiet option is specified and there are errors": function() {
31+
var result = { messages: [
32+
{ type: "warning", line: 1, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: [] }], stats: [] };
33+
var expected = "{\"filename\":\"path/to/FILE\",\"messages\":[{\"type\":\"warning\",\"line\":1,\"col\":1,\"message\":\"BOGUS\",\"evidence\":\"ALSO BOGUS\",\"rule\":[]}],\"stats\":[]}";
34+
var formatter = CSSLint.getFormatter("json");
35+
var actual = formatter.startFormat() +
36+
formatter.formatResults(result, "path/to/FILE",
37+
{fullPath: "/absolute/path/to/FILE", quiet: "true"}) +
38+
formatter.endFormat();
39+
Assert.areEqual(expected, actual);
40+
},
41+
42+
"File with problems should list them": function() {
43+
var result = { messages: [
44+
{ type: "warning", line: 1, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: [] },
45+
{ type: "error", line: 2, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: [] }
46+
], stats: [] };
47+
var expected = "{\"filename\":\"path/to/FILE\",\"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\":[]}";
48+
var formatter = CSSLint.getFormatter("json");
49+
var actual = formatter.startFormat() +
50+
formatter.formatResults(result, "path/to/FILE",
51+
{fullPath: "/absolute/path/to/FILE"}) +
52+
formatter.endFormat();
53+
Assert.areEqual(expected, actual);
54+
},
55+
56+
"Multiple files are handled properly": function () {
57+
var result = { messages: [
58+
{ type: "warning", line: 1, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: [] }], stats: [] };
59+
var expected = "[{\"filename\":\"path/to/FILE\",\"messages\":[{\"type\":\"warning\",\"line\":1,\"col\":1,\"message\":\"BOGUS\",\"evidence\":\"ALSO BOGUS\",\"rule\":[]}],\"stats\":[]},{\"filename\":\"path/to/FILE\",\"messages\":[{\"type\":\"warning\",\"line\":1,\"col\":1,\"message\":\"BOGUS\",\"evidence\":\"ALSO BOGUS\",\"rule\":[]}],\"stats\":[]}]";
60+
var formatter = CSSLint.getFormatter("json");
61+
var actual = formatter.startFormat() +
62+
formatter.formatResults(result, "path/to/FILE",
63+
{fullPath: "/absolute/path/to/FILE"}) +
64+
formatter.formatResults(result, "path/to/FILE",
65+
{fullPath: "/absolute/path/to/FILE"}) +
66+
formatter.endFormat();
67+
Assert.areEqual(expected, actual);
68+
}
69+
70+
}));
71+
72+
})();

0 commit comments

Comments
 (0)