-
Notifications
You must be signed in to change notification settings - Fork 478
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
Add a JSON formatter #606
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
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); | ||
} | ||
|
||
})); | ||
|
||
})(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"
?There was a problem hiding this comment.
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 valuedquiet
option that's a bug in the rest ofcsslint
, right?There was a problem hiding this comment.
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.