Skip to content

Add JSON formatter #642

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions src/formatters/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* global JSON */

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

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

/**
* Return full JSON
* @return {String} to append after all results
*/
endFormat: function() {
"use strict";
return JSON.stringify(this.json);
},

/**
* 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
* @param options {Object} (UNUSED for now) specifies special handling of output
* @return {String} output for results
*/
formatResults: function(results, filename, options) {
"use strict";
options = options || {};

if (results.messages.length > 0) {
this.json.push({
filename: filename,
results: results
});
}
return "";
}
});
54 changes: 54 additions & 0 deletions tests/formatters/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(function() {
"use strict";
var Assert = YUITest.Assert;

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

name: "JSON formatter",

"File with no problems should return empty string": function() {
var result = {
messages: [],
stats: []
},
actual = CSSLint.getFormatter("json").formatResults(result, "path/to/FILE", {
fullPath: "/absolute/path/to/FILE"
});
Assert.areEqual("", actual);
},

"Files 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: []
};

/*jshint -W108 */
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":[]}}]';

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);
}

}));

})();