Skip to content

Commit e3a5856

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 0fc42bc commit e3a5856

File tree

2 files changed

+60
-15
lines changed

2 files changed

+60
-15
lines changed

src/formatters/json.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CSSLint.addFormatter({
99
*/
1010
startFormat: function() {
1111
"use strict";
12+
this.json = [];
1213
return "";
1314
},
1415

@@ -18,7 +19,15 @@ CSSLint.addFormatter({
1819
*/
1920
endFormat: function() {
2021
"use strict";
21-
return "";
22+
var ret = "";
23+
if (this.json.length > 0) {
24+
if (this.json.length === 1) {
25+
ret = JSON.stringify(this.json[0]);
26+
} else {
27+
ret = JSON.stringify(this.json);
28+
}
29+
}
30+
return ret;
2231
},
2332

2433
/**
@@ -29,6 +38,13 @@ CSSLint.addFormatter({
2938
*/
3039
formatResults: function(results, filename, options) {
3140
"use strict";
32-
return options.quiet && results.messages.length < 1 ? "" : JSON.stringify(results);
41+
if (results.messages.length > 0 || !options.quiet) {
42+
this.json.push({
43+
filename: filename,
44+
messages: results.messages,
45+
stats: results.stats
46+
});
47+
}
48+
return "";
3349
}
3450
});

tests/formatters/json.js

Lines changed: 42 additions & 13 deletions
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)