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

Update: Allow eslint-disable-line to be wrapper in BlockComment #8780

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 19 additions & 4 deletions lib/linter.js
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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)));
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions tests/lib/linter.js
Expand Up @@ -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: {
Expand All @@ -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", () => {
Expand Down