Skip to content

Add a JSON formatter #606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/formatters/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* globals JSON: true */

CSSLint.addFormatter({
//format information
id: "json",
name: "JSON",

/**
* Return content to be printed before all file results.
* @return {String} to prepend before all results
*/
startFormat: function() {
"use strict";
this.json = [];
return "";
},

/**
* Return content to be printed after all file results.
* @return {String} to append after all results
*/
endFormat: function() {
"use strict";
var ret = "";
if (this.json.length > 0) {
if (this.json.length === 1) {
ret = JSON.stringify(this.json[0]);
} else {
ret = JSON.stringify(this.json);
}
}
return ret;
},

/**
* Given CSS Lint results for a file, return output for this format.
* @param results {Object} with error and warning messages
* @param filename {String} relative file path (Unused)
* @return {String} output for results
*/
formatResults: function(results, filename, options) {
"use strict";
if (results.messages.length > 0 || !options.quiet) {
this.json.push({
filename: filename,
messages: results.messages,
stats: results.stats
});
}
return "";
}
});
72 changes: 72 additions & 0 deletions tests/formatters/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
(function(){
"use strict";
var Assert = YUITest.Assert;

YUITest.TestRunner.add(new YUITest.TestCase({

name: "JSON formatter",

"File with no problems should say so": function() {
var result = { messages: [], stats: [] };
var expected = "{\"filename\":\"path/to/FILE\",\"messages\":[],\"stats\":[]}";
var formatter = CSSLint.getFormatter("json");
var actual = formatter.startFormat() +
formatter.formatResults(result, "path/to/FILE",
{fullPath: "/absolute/path/to/FILE"}) +
formatter.endFormat();
Assert.areEqual(expected, actual);
},

"Should have no output when quiet option is specified and no errors": function() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you define errors & warnings in your result object to test if quiet has an effect?
What happens if I set quiet: "false" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what you are asking here.

The way JSON.stringify works means that the result object being passed in there would still be output of {"messages":[],"stats":[]} if quiet is not true.

Formatters have nothing to do with parsing the options, if csslint isn't sending in a properly valued quiet option that's a bug in the rest of csslint, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I just realized what you mean, and that's a silly bug, fixing.

var result = { messages: [], stats: [] };
var formatter = CSSLint.getFormatter("json");
var actual = formatter.startFormat() +
formatter.formatResults(result, "path/to/FILE",
{fullPath: "/absolute/path/to/FILE", quiet: "true"}) +
formatter.endFormat();
Assert.areEqual("", actual);
},

"Should have output when quiet option is specified and there are errors": function() {
var result = { messages: [
{ type: "warning", line: 1, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: [] }], stats: [] };
var expected = "{\"filename\":\"path/to/FILE\",\"messages\":[{\"type\":\"warning\",\"line\":1,\"col\":1,\"message\":\"BOGUS\",\"evidence\":\"ALSO BOGUS\",\"rule\":[]}],\"stats\":[]}";
var formatter = CSSLint.getFormatter("json");
var actual = formatter.startFormat() +
formatter.formatResults(result, "path/to/FILE",
{fullPath: "/absolute/path/to/FILE", quiet: "true"}) +
formatter.endFormat();
Assert.areEqual(expected, actual);
},

"File with problems should list them": function() {
var result = { 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: [] };
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\":[]}";
var formatter = CSSLint.getFormatter("json");
var actual = formatter.startFormat() +
formatter.formatResults(result, "path/to/FILE",
{fullPath: "/absolute/path/to/FILE"}) +
formatter.endFormat();
Assert.areEqual(expected, actual);
},

"Multiple files are handled properly": function () {
var result = { messages: [
{ type: "warning", line: 1, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: [] }], stats: [] };
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\":[]}]";
var formatter = CSSLint.getFormatter("json");
var actual = formatter.startFormat() +
formatter.formatResults(result, "path/to/FILE",
{fullPath: "/absolute/path/to/FILE"}) +
formatter.formatResults(result, "path/to/FILE",
{fullPath: "/absolute/path/to/FILE"}) +
formatter.endFormat();
Assert.areEqual(expected, actual);
}

}));

})();