Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: off-by-one error in eslint-disable comment checking #9195

Merged
merged 1 commit into from Sep 1, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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