Skip to content

Commit baeb536

Browse files
author
epriestley
committed
Add "rule" attribute to "lintxml" output format
Summary: Currently, there's no way to determine which rule generated a lint message if you have a parser on top of CSSLint. Provide a `rule` attribute which has the rule ID. (The specific motivating use case is that I have a program which runs multiple linters, manages lint message severity above the linter level, and has more severity levels than CSSLint itself does, so using flags like `--ignore` would require special casing and reduce flexibility.) Test Plan: Added new test coverage and executed unit tests.
1 parent f1ef64d commit baeb536

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/formatters/lint-xml.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ CSSLint.addFormatter({
3939
* - & is the escape sequence for &
4040
* - &lt; is the escape sequence for <
4141
* - &gt; is the escape sequence for >
42-
*
42+
*
4343
* @param {String} message to escape
4444
* @return escaped message as {String}
4545
*/
@@ -51,13 +51,17 @@ CSSLint.addFormatter({
5151
};
5252

5353
if (messages.length > 0) {
54-
54+
5555
output.push("<file name=\""+filename+"\">");
5656
CSSLint.Util.forEach(messages, function (message, i) {
5757
if (message.rollup) {
5858
output.push("<issue severity=\"" + message.type + "\" reason=\"" + escapeSpecialCharacters(message.message) + "\" evidence=\"" + escapeSpecialCharacters(message.evidence) + "\"/>");
5959
} else {
60-
output.push("<issue line=\"" + message.line + "\" char=\"" + message.col + "\" severity=\"" + message.type + "\"" +
60+
var rule = '';
61+
if (message.rule && message.rule.id) {
62+
rule = 'rule=\"' + escapeSpecialCharacters(message.rule.id) + '\" ';
63+
}
64+
output.push("<issue " + rule + "line=\"" + message.line + "\" char=\"" + message.col + "\" severity=\"" + message.type + "\"" +
6165
" reason=\"" + escapeSpecialCharacters(message.message) + "\" evidence=\"" + escapeSpecialCharacters(message.evidence) + "\"/>");
6266
}
6367
});

tests/formatters/lint-xml.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
YUITest.TestRunner.add(new YUITest.TestCase({
77

88
name: "Lint XML formatter test",
9-
9+
1010
"File with no problems should say so": function(){
1111
var result = { messages: [], stats: [] },
1212
expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?><lint></lint>";
@@ -38,6 +38,25 @@
3838
expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?><lint>" + file + error1 + error2 + "</file></lint>",
3939
actual = CSSLint.format(result, "FILE", "lint-xml");
4040
Assert.areEqual(expected, actual);
41+
},
42+
43+
"Messages should include rule IDs": function() {
44+
var result = { messages: [
45+
{ type: "error", line: 1, col: 1, message: "X", evidence: "Y", rule: { id: "Z" } }
46+
], stats: [] };
47+
48+
var expected =
49+
'<?xml version="1.0" encoding="utf-8"?>' +
50+
'<lint>' +
51+
'<file name="FILE">' +
52+
'<issue rule="Z" line="1" char="1" severity="error" reason="X" evidence="Y"/>' +
53+
'</file>' +
54+
'</lint>';
55+
56+
var actual = CSSLint.format(result, "FILE", "lint-xml");
57+
58+
Assert.areEqual(expected, actual);
4159
}
60+
4261
}));
4362
})();

0 commit comments

Comments
 (0)