Skip to content

Commit 503b16a

Browse files
author
Sergey Chernyshev
committed
Add JSON formatter
1 parent 9c62146 commit 503b16a

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/formatters/json.js

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

tests/formatters/json.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 return empty string": function() {
10+
var result = {
11+
messages: [],
12+
stats: []
13+
},
14+
actual = CSSLint.getFormatter("json").formatResults(result, "path/to/FILE", {
15+
fullPath: "/absolute/path/to/FILE"
16+
});
17+
Assert.areEqual("", actual);
18+
},
19+
20+
"Files with problems should list them": function() {
21+
var result = {
22+
messages: [{
23+
type: "warning",
24+
line: 1,
25+
col: 1,
26+
message: "BOGUS",
27+
evidence: "ALSO BOGUS",
28+
rule: []
29+
}, {
30+
type: "error",
31+
line: 2,
32+
col: 1,
33+
message: "BOGUS",
34+
evidence: "ALSO BOGUS",
35+
rule: []
36+
}],
37+
stats: []
38+
};
39+
40+
/*jshint -W108 */
41+
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":[]}}]';
42+
43+
var formatter = CSSLint.getFormatter("json");
44+
45+
var actual = formatter.startFormat() + formatter.formatResults(result, "path/to/FILE", {
46+
fullPath: "/absolute/path/to/FILE"
47+
}) + formatter.endFormat();
48+
49+
Assert.areEqual(expected, actual);
50+
}
51+
52+
}));
53+
54+
})();

0 commit comments

Comments
 (0)