From a840a9006aa54ecec02f6a301ac84ccc1ed6d6e5 Mon Sep 17 00:00:00 2001 From: Alexey Yaroshevich Date: Thu, 22 Jun 2017 05:56:12 +0300 Subject: [PATCH] Update: Allow eslint-disable-line to be wrapper in BlockComment --- lib/linter.js | 23 +++++++++++++++++++---- tests/lib/linter.js | 10 ++++------ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/linter.js b/lib/linter.js index d2f1f46574c..eaf39071fb4 100755 --- a/lib/linter.js +++ b/lib/linter.js @@ -310,6 +310,19 @@ function enableReporting(reportingConfig, start, rulesToEnable) { } } +/** + * Add data to reporting configuration to disable reporting for list of rules + * for specific line + * @param {Object[]} reportingConfig Current reporting configuration + * @param {number} line Line number to disable + * @param {string[]} rulesToDisable List of rules + * @returns {void} + */ +function disableReportingAtLine(reportingConfig, line, rulesToDisable) { + disableReporting(reportingConfig, { line, column: 0 }, rulesToDisable); + enableReporting(reportingConfig, { line, column: Infinity }, rulesToDisable); +} + /** * Parses comments in file to extract file-specific config of rules, globals * and environments and merges them with global config; also code blocks @@ -363,6 +376,10 @@ function modifyConfigsFromComments(filename, ast, config, linterContext) { enableReporting(reportingConfig, comment.loc.start, Object.keys(parseListConfig(value))); break; + case "eslint-disable-line": + disableReportingAtLine(reportingConfig, comment.loc.start.line, Object.keys(parseListConfig(value))); + break; + case "eslint": { const items = parseJsonConfig(value, comment.loc, messages); @@ -379,11 +396,9 @@ function modifyConfigsFromComments(filename, ast, config, linterContext) { } } else { // comment.type === "Line" if (match[1] === "eslint-disable-line") { - disableReporting(reportingConfig, { line: comment.loc.start.line, column: 0 }, Object.keys(parseListConfig(value))); - enableReporting(reportingConfig, comment.loc.end, Object.keys(parseListConfig(value))); + disableReportingAtLine(reportingConfig, comment.loc.start.line, Object.keys(parseListConfig(value))); } else if (match[1] === "eslint-disable-next-line") { - disableReporting(reportingConfig, comment.loc.start, Object.keys(parseListConfig(value))); - enableReporting(reportingConfig, { line: comment.loc.start.line + 2 }, Object.keys(parseListConfig(value))); + disableReportingAtLine(reportingConfig, comment.loc.start.line + 1, Object.keys(parseListConfig(value))); } } } diff --git a/tests/lib/linter.js b/tests/lib/linter.js index 4acc46b3f02..3df876c333f 100644 --- a/tests/lib/linter.js +++ b/tests/lib/linter.js @@ -2105,10 +2105,10 @@ describe("eslint", () => { assert.equal(messages[0].ruleId, "no-alert"); }); - it("should report a violation", () => { + it("should not report a violation", () => { const code = [ - "alert('test'); // eslint-disable-line no-alert", - "alert('test'); /*eslint-disable-line no-alert*/" // here + "alert('test'); /* eslint-disable-line no-alert */", + "/*eslint-disable-line no-alert*/ alert('test');" ].join("\n"); const config = { rules: { @@ -2118,9 +2118,7 @@ describe("eslint", () => { const messages = linter.verify(code, config, filename); - assert.equal(messages.length, 1); - - assert.equal(messages[0].ruleId, "no-alert"); + assert.equal(messages.length, 0); }); it("should not report a violation", () => {