Skip to content
This repository was archived by the owner on Jan 9, 2018. It is now read-only.

Commit e8b8855

Browse files
committed
Include Rule ID as identifier in checkstyle output
1 parent 27a2174 commit e8b8855

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/formatters/checkstyle-xml.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@
8888
return "net.csslint." + rule.name.replace(/\s/g,"");
8989
};
9090

91+
/**
92+
* Generate an identifier string for a rule.
93+
* Uses the rule's id when present, else "generic".
94+
* @param {Object} rule
95+
* @return rule source as {String}
96+
*/
97+
var generateIdentifier = function(rule) {
98+
if (!rule || !("id" in rule)) {
99+
return "generic";
100+
}
101+
return rule.id;
102+
};
91103

92104

93105
if (messages.length > 0) {
@@ -96,7 +108,8 @@
96108
//ignore rollups for now
97109
if (!message.rollup) {
98110
output.push("<error line=\"" + message.line + "\" column=\"" + message.col + "\" severity=\"" + message.type + "\"" +
99-
" message=\"" + xmlEscape(message.message) + "\" source=\"" + generateSource(message.rule) +"\"/>");
111+
" message=\"" + xmlEscape(message.message) + "\" source=\"" + generateSource(message.rule) +"\"" +
112+
" identifier=\"" + generateIdentifier(message.rule) + "\"/>");
100113
}
101114
});
102115
output.push("</file>");

tests/formatters/checkstyle-xml.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
"File with problems should list them": function(){
1616
var result = { messages: [
17-
{ type: "warning", line: 1, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: { name: "A Rule"} },
18-
{ type: "error", line: 2, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: { name: "Some Other Rule"} }
17+
{ type: "warning", line: 1, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: { id: "a-rule", name: "A Rule"} },
18+
{ type: "error", line: 2, col: 1, message: "BOGUS", evidence: "ALSO BOGUS", rule: { id: "some-other-rule", name: "Some Other Rule"} }
1919
], stats: [] },
2020
file = "<file name=\"FILE\">",
21-
error1 = "<error line=\"1\" column=\"1\" severity=\"warning\" message=\"BOGUS\" source=\"net.csslint.ARule\"/>",
22-
error2 = "<error line=\"2\" column=\"1\" severity=\"error\" message=\"BOGUS\" source=\"net.csslint.SomeOtherRule\"/>",
21+
error1 = "<error line=\"1\" column=\"1\" severity=\"warning\" message=\"BOGUS\" source=\"net.csslint.ARule\" identifier=\"a-rule\"/>",
22+
error2 = "<error line=\"2\" column=\"1\" severity=\"error\" message=\"BOGUS\" source=\"net.csslint.SomeOtherRule\" identifier=\"some-other-rule\"/>",
2323
expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?><checkstyle>" + file + error1 + error2 + "</file></checkstyle>",
2424
actual = CSSLint.format(result, "FILE", "checkstyle-xml");
2525
Assert.areEqual(expected, actual);
@@ -32,8 +32,8 @@
3232
{ type: "error", line: 2, col: 1, message: specialCharsSting, evidence: "ALSO BOGUS", rule: [] }
3333
], stats: [] },
3434
file = "<file name=\"FILE\">",
35-
error1 = "<error line=\"1\" column=\"1\" severity=\"warning\" message=\"sneaky, 'sneaky', &lt;sneaky&gt;, sneak &amp; sneaky\" source=\"net.csslint.generic\"/>",
36-
error2 = "<error line=\"2\" column=\"1\" severity=\"error\" message=\"sneaky, 'sneaky', &lt;sneaky&gt;, sneak &amp; sneaky\" source=\"net.csslint.generic\"/>",
35+
error1 = "<error line=\"1\" column=\"1\" severity=\"warning\" message=\"sneaky, 'sneaky', &lt;sneaky&gt;, sneak &amp; sneaky\" source=\"net.csslint.generic\" identifier=\"generic\"/>",
36+
error2 = "<error line=\"2\" column=\"1\" severity=\"error\" message=\"sneaky, 'sneaky', &lt;sneaky&gt;, sneak &amp; sneaky\" source=\"net.csslint.generic\" identifier=\"generic\"/>",
3737
expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?><checkstyle>" + file + error1 + error2 + "</file></checkstyle>",
3838
actual = CSSLint.format(result, "FILE", "checkstyle-xml");
3939
Assert.areEqual(expected, actual);

0 commit comments

Comments
 (0)