Skip to content

Commit

Permalink
Fix: off-by-one error in eslint-disable comment checking (#9195)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark authored and ilyavolodin committed Sep 1, 2017
1 parent 73815f6 commit 3e8b70a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/linter.js
Expand Up @@ -404,7 +404,7 @@ function modifyConfigsFromComments(filename, ast, config, linterContext) {
* Check if message of rule with ruleId should be ignored in location
* @param {Object[]} reportingConfig Collection of ignore records
* @param {string} ruleId Id of rule
* @param {Object} location Location of message
* @param {Object} location 1-indexed location of message
* @returns {boolean} True if message should be ignored, false otherwise
*/
function isDisabledByReportingConfig(reportingConfig, ruleId, location) {
Expand All @@ -414,8 +414,8 @@ function isDisabledByReportingConfig(reportingConfig, ruleId, location) {
const ignore = reportingConfig[i];

if ((!ignore.rule || ignore.rule === ruleId) &&
(location.line > ignore.start.line || (location.line === ignore.start.line && location.column >= ignore.start.column)) &&
(!ignore.end || (location.line < ignore.end.line || (location.line === ignore.end.line && location.column <= ignore.end.column)))) {
(location.line > ignore.start.line || (location.line === ignore.start.line && location.column > ignore.start.column)) &&
(!ignore.end || (location.line < ignore.end.line || (location.line === ignore.end.line && location.column - 1 <= ignore.end.column)))) {
return true;
}
}
Expand Down
27 changes: 27 additions & 0 deletions tests/lib/linter.js
Expand Up @@ -1548,6 +1548,33 @@ describe("Linter", () => {
assert.equal(messages.length, 0);
});

it("should report a violation when the report is right before the comment", () => {
const code = " /* eslint-disable */ ";

linter.defineRule("checker", context => ({
Program() {
context.report({ loc: { line: 1, column: 0 }, message: "foo" });
}
}));
const problems = linter.verify(code, { rules: { checker: "error" } });

assert.strictEqual(problems.length, 1);
assert.strictEqual(problems[0].message, "foo");
});

it("should not report a violation when the report is right at the start of the comment", () => {
const code = " /* eslint-disable */ ";

linter.defineRule("checker", context => ({
Program() {
context.report({ loc: { line: 1, column: 1 }, message: "foo" });
}
}));
const problems = linter.verify(code, { rules: { checker: "error" } });

assert.strictEqual(problems.length, 0);
});

it("rules should not change initial config", () => {
const config = { rules: { "test-plugin/test-rule": 2 } };
const codeA = "/*eslint test-plugin/test-rule: 0*/ var a = \"trigger violation\";";
Expand Down

0 comments on commit 3e8b70a

Please sign in to comment.