Skip to content

Commit 2100e95

Browse files
committed
Merge pull request #564 from cwygoda/feature/ignore
Added ignore capability.
2 parents b06d61d + a21247b commit 2100e95

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

src/core/CSSLint.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ var CSSLint = (function() {
178178
reporter,
179179
lines,
180180
allow = {},
181+
ignore = [],
181182
report,
182183
parser = new parserlib.css.Parser({ starHack: true, ieFilters: true,
183184
underscoreHack: true, strict: false });
@@ -201,6 +202,29 @@ var CSSLint = (function() {
201202
}
202203
});
203204

205+
var ignoreStart = null,
206+
ignoreEnd = null;
207+
CSSLint.Util.forEach(lines, function (line, lineno) {
208+
// Keep oldest, "unclosest" ignore:start
209+
if(null === ignoreStart && line.match(/\/\*[ \t]*csslint[ \t]+ignore:start[ \t]*\*\//i)) {
210+
ignoreStart = lineno;
211+
}
212+
213+
if(line.match(/\/\*[ \t]*csslint[ \t]+ignore:end[ \t]*\*\//i)) {
214+
ignoreEnd = lineno;
215+
}
216+
217+
if(null !== ignoreStart && null !== ignoreEnd) {
218+
ignore.push([ignoreStart, ignoreEnd]);
219+
ignoreStart = ignoreEnd = null;
220+
}
221+
});
222+
223+
// Close remaining ignore block, if any
224+
if(null !== ignoreStart) {
225+
ignore.push([ignoreStart, lines.length]);
226+
}
227+
204228
if (!ruleset) {
205229
ruleset = this.getRuleset();
206230
}
@@ -211,7 +235,7 @@ var CSSLint = (function() {
211235
ruleset = applyEmbeddedRuleset(text, ruleset);
212236
}
213237

214-
reporter = new Reporter(lines, ruleset, allow);
238+
reporter = new Reporter(lines, ruleset, allow, ignore);
215239

216240
ruleset.errors = 2; //always report parsing errors as errors
217241
for (i in ruleset) {
@@ -234,7 +258,8 @@ var CSSLint = (function() {
234258
messages : reporter.messages,
235259
stats : reporter.stats,
236260
ruleset : reporter.ruleset,
237-
allow : reporter.allow
261+
allow : reporter.allow,
262+
ignore : reporter.ignore
238263
};
239264

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

src/core/Reporter.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
* @param {String[]} lines The text lines of the source.
77
* @param {Object} ruleset The set of rules to work with, including if
88
* they are errors or warnings.
9+
* @param {Object} explicitly allowed lines
10+
* @param {[][]} ingore list of line ranges to be ignored
911
*/
10-
function Reporter(lines, ruleset, allow) {
12+
function Reporter(lines, ruleset, allow, ignore) {
1113
"use strict";
1214

1315
/**
@@ -49,6 +51,16 @@ function Reporter(lines, ruleset, allow) {
4951
if(!this.allow) {
5052
this.allow = {};
5153
}
54+
55+
/**
56+
* Linesets not to include in the report.
57+
* @property ignore
58+
* @type [][]
59+
*/
60+
this.ignore = ignore;
61+
if(!this.ignore) {
62+
this.ignore = [];
63+
}
5264
}
5365

5466
Reporter.prototype = {
@@ -106,6 +118,16 @@ Reporter.prototype = {
106118
return;
107119
}
108120

121+
var ignore = false;
122+
CSSLint.Util.forEach(this.ignore, function (range) {
123+
if(range[0] <= line && line <= range[1]) {
124+
ignore = true;
125+
}
126+
});
127+
if(ignore) {
128+
return;
129+
}
130+
109131
this.messages.push({
110132
type : this.ruleset[rule.id] === 2 ? "error" : "warning",
111133
line : line,

tests/core/CSSLint.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,35 @@
6969
Assert.isTrue(report.allow["2"].hasOwnProperty("box-sizing"));
7070
Assert.isTrue(report.allow.hasOwnProperty("4"));
7171
Assert.isTrue(report.allow["4"].hasOwnProperty("box-model"));
72+
},
73+
74+
"Full ignore blocks should be captured": function(){
75+
var report = CSSLint.verify("/* csslint ignore:start */\n\n/* csslint ignore:end */");
76+
Assert.areEqual(1, report.ignore.length);
77+
Assert.areEqual(0, report.ignore[0][0]);
78+
Assert.areEqual(2, report.ignore[0][1]);
79+
},
80+
81+
"Whitespace should be no problem inside ignore comments": function(){
82+
var report = CSSLint.verify("/* csslint ignore:start */\n\n/* csslint ignore:end */,\n/*csslint ignore:start*/\n/*csslint ignore:end*/");
83+
Assert.areEqual(2, report.ignore.length);
84+
Assert.areEqual(0, report.ignore[0][0]);
85+
Assert.areEqual(2, report.ignore[0][1]);
86+
Assert.areEqual(3, report.ignore[1][0]);
87+
Assert.areEqual(4, report.ignore[1][1]);
88+
},
89+
90+
"Ignore blocks should be autoclosed": function(){
91+
var report = CSSLint.verify("/* csslint ignore:start */\n\n");
92+
Assert.areEqual(1, report.ignore.length);
93+
Assert.areEqual(0, report.ignore[0][0]);
94+
Assert.areEqual(3, report.ignore[0][1]);
95+
},
96+
97+
"Restarting ignore should be harmless": function(){
98+
var report = CSSLint.verify("/* csslint ignore:start */\n/* csslint ignore:start */\n");
99+
Assert.areEqual(1, report.ignore.length);
100+
Assert.areEqual(0, report.ignore[0][0]);
72101
}
73102

74103
}));

tests/core/Reporter.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252
reporter.report("Bar", 3, 1, { id: "fake-rule2" });
5353

5454
Assert.areEqual(0, reporter.messages.length);
55+
},
56+
57+
"Ignores should step over a report in their range": function(){
58+
var reporter = new CSSLint._Reporter([], { "fake-rule": 1}, {}, [[1,3]]);
59+
reporter.report("Foo", 2, 1, { id: "fake-rule" });
60+
reporter.report("Bar", 5, 1, { id: "fake-rule" });
61+
62+
Assert.areEqual(1, reporter.messages.length);
5563
}
5664

5765
}));

0 commit comments

Comments
 (0)