Skip to content

Commit 77b08b1

Browse files
committed
Merge pull request CSSLint#630 from akrawitz/allow-statement
Add allow statement
2 parents 0c12e2a + 3a34a6c commit 77b08b1

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

src/core/CSSLint.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,30 @@ var CSSLint = (function() {
177177
var i = 0,
178178
reporter,
179179
lines,
180+
allow = {},
180181
report,
181182
parser = new parserlib.css.Parser({ starHack: true, ieFilters: true,
182183
underscoreHack: true, strict: false });
183184

184185
// normalize line endings
185186
lines = text.replace(/\n\r?/g, "$split$").split("$split$");
186187

188+
// find 'allow' comments
189+
CSSLint.Util.forEach(lines, function (line, lineno) {
190+
var allowLine = line && line.match(/\/\*[ \t]*csslint[ \t]+allow:[ \t]*([^\*]*)\*\//i),
191+
allowRules = allowLine && allowLine[1],
192+
allowRuleset = {};
193+
194+
if (allowRules) {
195+
allowRules.toLowerCase().split(",").forEach(function(allowRule){
196+
allowRuleset[allowRule.trim()] = true;
197+
});
198+
if (Object.keys(allowRuleset).length > 0) {
199+
allow[lineno + 1] = allowRuleset;
200+
}
201+
}
202+
});
203+
187204
if (!ruleset) {
188205
ruleset = this.getRuleset();
189206
}
@@ -194,7 +211,7 @@ var CSSLint = (function() {
194211
ruleset = applyEmbeddedRuleset(text, ruleset);
195212
}
196213

197-
reporter = new Reporter(lines, ruleset);
214+
reporter = new Reporter(lines, ruleset, allow);
198215

199216
ruleset.errors = 2; //always report parsing errors as errors
200217
for (i in ruleset) {
@@ -216,7 +233,8 @@ var CSSLint = (function() {
216233
report = {
217234
messages : reporter.messages,
218235
stats : reporter.stats,
219-
ruleset : reporter.ruleset
236+
ruleset : reporter.ruleset,
237+
allow : reporter.allow
220238
};
221239

222240
//sort by line numbers, rollups at the bottom

src/core/Reporter.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @param {Object} ruleset The set of rules to work with, including if
88
* they are errors or warnings.
99
*/
10-
function Reporter(lines, ruleset) {
10+
function Reporter(lines, ruleset, allow) {
1111
"use strict";
1212

1313
/**
@@ -39,6 +39,16 @@ function Reporter(lines, ruleset) {
3939
* @type Object
4040
*/
4141
this.ruleset = ruleset;
42+
43+
/**
44+
* Lines with specific rule messages to leave out of the report.
45+
* @property allow
46+
* @type Object
47+
*/
48+
this.allow = allow;
49+
if(!this.allow) {
50+
this.allow = {};
51+
}
4252
}
4353

4454
Reporter.prototype = {
@@ -90,6 +100,12 @@ Reporter.prototype = {
90100
*/
91101
report: function(message, line, col, rule) {
92102
"use strict";
103+
104+
// Check if rule violation should be allowed
105+
if (this.allow.hasOwnProperty(line) && this.allow[line].hasOwnProperty(rule.id)) {
106+
return;
107+
}
108+
93109
this.messages.push({
94110
type : this.ruleset[rule.id] === 2 ? "error" : "warning",
95111
line : line,

tests/core/CSSLint.js

+21
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@
4848
Assert.areEqual(2, result.ruleset["adjoining-classes"]);
4949
Assert.areEqual(1, result.ruleset["text-indent"]);
5050
Assert.areEqual(0, result.ruleset["box-sizing"]);
51+
},
52+
53+
"Allow statement on one line with one rule should be added to report": function(){
54+
var report = CSSLint.verify(".foo.bar{}\n.baz.qux{} /* csslint allow: box-sizing */\nquux.corge{}");
55+
Assert.isTrue(report.allow.hasOwnProperty("2"));
56+
Assert.isTrue(report.allow["2"].hasOwnProperty("box-sizing"));
57+
},
58+
59+
"Allow statement on one line with multiple rules should be added to report": function(){
60+
var report = CSSLint.verify(".foo.bar{}\n.baz.qux{} /* csslint allow: box-sizing, box-model */\nquux.corge{}");
61+
Assert.isTrue(report.allow.hasOwnProperty("2"));
62+
Assert.isTrue(report.allow["2"].hasOwnProperty("box-sizing"));
63+
Assert.isTrue(report.allow["2"].hasOwnProperty("box-model"));
64+
},
65+
66+
"Allow statements on multiple lines for different rules should be added to report": function(){
67+
var report = CSSLint.verify(".foo.bar{}\n.baz.qux{} /* csslint allow: box-sizing */\nquux.corge{}\ngrault.garply{} /* csslint allow: box-model */");
68+
Assert.isTrue(report.allow.hasOwnProperty("2"));
69+
Assert.isTrue(report.allow["2"].hasOwnProperty("box-sizing"));
70+
Assert.isTrue(report.allow.hasOwnProperty("4"));
71+
Assert.isTrue(report.allow["4"].hasOwnProperty("box-model"));
5172
}
5273

5374
}));

tests/core/Reporter.js

+24
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@
2828

2929
Assert.areEqual(1, reporter.messages.length);
3030
Assert.areEqual("error", reporter.messages[0].type);
31+
},
32+
33+
"Allow statement should drop message about specific rule on specific line but not other lines": function(){
34+
var reporter = new CSSLint._Reporter([], { "fake-rule": 1}, {"3": {"fake-rule": true}});
35+
reporter.report("Foo", 2, 1, { id: "fake-rule" });
36+
reporter.report("Bar", 3, 1, { id: "fake-rule" });
37+
38+
Assert.areEqual(1, reporter.messages.length);
39+
},
40+
41+
"Allow statement should drop message about specific rule on specific line but not other rules": function(){
42+
var reporter = new CSSLint._Reporter([], { "fake-rule": 1, "fake-rule2": 1}, {"3": {"fake-rule": true}});
43+
reporter.report("Foo", 3, 1, { id: "fake-rule" });
44+
reporter.report("Bar", 3, 1, { id: "fake-rule2" });
45+
46+
Assert.areEqual(1, reporter.messages.length);
47+
},
48+
49+
"Allow statement should drop messages about multiple rules on specific line": function(){
50+
var reporter = new CSSLint._Reporter([], { "fake-rule": 1, "fake-rule2": 1}, {"3": {"fake-rule": true, "fake-rule2": true}});
51+
reporter.report("Foo", 3, 1, { id: "fake-rule" });
52+
reporter.report("Bar", 3, 1, { id: "fake-rule2" });
53+
54+
Assert.areEqual(0, reporter.messages.length);
3155
}
3256

3357
}));

0 commit comments

Comments
 (0)