Skip to content

Commit 828bbd5

Browse files
committed
Handle multiple files
Results from multiple files should be handled in a single JSON object instead of output as multiple objects.
1 parent 2ef32ff commit 828bbd5

File tree

2 files changed

+60
-15
lines changed

2 files changed

+60
-15
lines changed

src/formatters/json.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CSSLint.addFormatter({
1111
*/
1212
startFormat: function() {
1313
"use strict";
14+
this.json = [];
1415
return "";
1516
},
1617

@@ -20,7 +21,15 @@ CSSLint.addFormatter({
2021
*/
2122
endFormat: function() {
2223
"use strict";
23-
return "";
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;
2433
},
2534

2635
/**
@@ -31,6 +40,13 @@ CSSLint.addFormatter({
3140
*/
3241
formatResults: function(results, filename, options) {
3342
"use strict";
34-
return options.quiet && results.messages.length < 1 ? "" : JSON.stringify(results);
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 "";
3551
}
3652
});

tests/formatters/json.js

+42-13
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,63 @@
77
name: "JSON formatter",
88

99
"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);
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);
1318
},
1419

1520
"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"});
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();
1827
Assert.areEqual("", actual);
1928
},
2029

2130
"Should have output when quiet option is specified and there are errors": function() {
2231
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", quiet: "true"});
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();
2839
Assert.areEqual(expected, actual);
2940
},
3041

3142
"File with problems should list them": function() {
3243
var result = { messages: [
3344
{ type: "warning", line: 1, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: [] },
3445
{ type: "error", line: 2, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: [] }
35-
], stats: [] },
36-
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\":[]}",
37-
actual = CSSLint.getFormatter("json").formatResults(result, "path/to/FILE", {fullPath: "/absolute/path/to/FILE"});
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();
3867
Assert.areEqual(expected, actual);
3968
}
4069

0 commit comments

Comments
 (0)