Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/core/CSSLint.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ var CSSLint = (function() {
reporter,
lines,
allow = {},
ignore = [],
report,
parser = new parserlib.css.Parser({ starHack: true, ieFilters: true,
underscoreHack: true, strict: false });
Expand All @@ -201,6 +202,29 @@ var CSSLint = (function() {
}
});

var ignoreStart = null,
ignoreEnd = null;
CSSLint.Util.forEach(lines, function (line, lineno) {
// Keep oldest, "unclosest" ignore:start
if(null === ignoreStart && line.match(/\/\*[ \t]*csslint[ \t]+ignore:start[ \t]*\*\//i)) {
ignoreStart = lineno;
}

if(line.match(/\/\*[ \t]*csslint[ \t]+ignore:end[ \t]*\*\//i)) {
ignoreEnd = lineno;
}

if(null !== ignoreStart && null !== ignoreEnd) {
ignore.push([ignoreStart, ignoreEnd]);
ignoreStart = ignoreEnd = null;
}
});

// Close remaining ignore block, if any
if(null !== ignoreStart) {
ignore.push([ignoreStart, lines.length]);
}

if (!ruleset) {
ruleset = this.getRuleset();
}
Expand All @@ -211,7 +235,7 @@ var CSSLint = (function() {
ruleset = applyEmbeddedRuleset(text, ruleset);
}

reporter = new Reporter(lines, ruleset, allow);
reporter = new Reporter(lines, ruleset, allow, ignore);

ruleset.errors = 2; //always report parsing errors as errors
for (i in ruleset) {
Expand All @@ -234,7 +258,8 @@ var CSSLint = (function() {
messages : reporter.messages,
stats : reporter.stats,
ruleset : reporter.ruleset,
allow : reporter.allow
allow : reporter.allow,
ignore : reporter.ignore
};

//sort by line numbers, rollups at the bottom
Expand Down
24 changes: 23 additions & 1 deletion src/core/Reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
* @param {String[]} lines The text lines of the source.
* @param {Object} ruleset The set of rules to work with, including if
* they are errors or warnings.
* @param {Object} explicitly allowed lines
* @param {[][]} ingore list of line ranges to be ignored
*/
function Reporter(lines, ruleset, allow) {
function Reporter(lines, ruleset, allow, ignore) {
"use strict";

/**
Expand Down Expand Up @@ -49,6 +51,16 @@ function Reporter(lines, ruleset, allow) {
if(!this.allow) {
this.allow = {};
}

/**
* Linesets not to include in the report.
* @property ignore
* @type [][]
*/
this.ignore = ignore;
if(!this.ignore) {
this.ignore = [];
}
}

Reporter.prototype = {
Expand Down Expand Up @@ -106,6 +118,16 @@ Reporter.prototype = {
return;
}

var ignore = false;
CSSLint.Util.forEach(this.ignore, function (range) {
if(range[0] <= line && line <= range[1]) {
ignore = true;
}
});
if(ignore) {
return;
}

this.messages.push({
type : this.ruleset[rule.id] === 2 ? "error" : "warning",
line : line,
Expand Down
29 changes: 29 additions & 0 deletions tests/core/CSSLint.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@
Assert.isTrue(report.allow["2"].hasOwnProperty("box-sizing"));
Assert.isTrue(report.allow.hasOwnProperty("4"));
Assert.isTrue(report.allow["4"].hasOwnProperty("box-model"));
},

"Full ignore blocks should be captured": function(){
var report = CSSLint.verify("/* csslint ignore:start */\n\n/* csslint ignore:end */");
Assert.areEqual(1, report.ignore.length);
Assert.areEqual(0, report.ignore[0][0]);
Assert.areEqual(2, report.ignore[0][1]);
},

"Whitespace should be no problem inside ignore comments": function(){
var report = CSSLint.verify("/* csslint ignore:start */\n\n/* csslint ignore:end */,\n/*csslint ignore:start*/\n/*csslint ignore:end*/");
Assert.areEqual(2, report.ignore.length);
Assert.areEqual(0, report.ignore[0][0]);
Assert.areEqual(2, report.ignore[0][1]);
Assert.areEqual(3, report.ignore[1][0]);
Assert.areEqual(4, report.ignore[1][1]);
},

"Ignore blocks should be autoclosed": function(){
var report = CSSLint.verify("/* csslint ignore:start */\n\n");
Assert.areEqual(1, report.ignore.length);
Assert.areEqual(0, report.ignore[0][0]);
Assert.areEqual(3, report.ignore[0][1]);
},

"Restarting ignore should be harmless": function(){
var report = CSSLint.verify("/* csslint ignore:start */\n/* csslint ignore:start */\n");
Assert.areEqual(1, report.ignore.length);
Assert.areEqual(0, report.ignore[0][0]);
}

}));
Expand Down
8 changes: 8 additions & 0 deletions tests/core/Reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
reporter.report("Bar", 3, 1, { id: "fake-rule2" });

Assert.areEqual(0, reporter.messages.length);
},

"Ignores should step over a report in their range": function(){
var reporter = new CSSLint._Reporter([], { "fake-rule": 1}, {}, [[1,3]]);
reporter.report("Foo", 2, 1, { id: "fake-rule" });
reporter.report("Bar", 5, 1, { id: "fake-rule" });

Assert.areEqual(1, reporter.messages.length);
}

}));
Expand Down