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: no-unreachable reporting EmptyStatements (fixes #9081) #9084

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/rules/no-unreachable.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ConsecutiveRange {
this.sourceCode = sourceCode;
this.startNode = null;
this.endNode = null;
this.hasNonEmptyStatement = false;
}

/**
Expand Down Expand Up @@ -83,6 +84,10 @@ class ConsecutiveRange {
*/
merge(node) {
this.endNode = node;

if (node.type !== "EmptyStatement") {
this.hasNonEmptyStatement = true;
}
}

/**
Expand All @@ -92,6 +97,8 @@ class ConsecutiveRange {
*/
reset(node) {
this.startNode = this.endNode = node;

this.hasNonEmptyStatement = (node && node.type !== "EmptyStatement");
}
}

Expand Down Expand Up @@ -147,7 +154,7 @@ module.exports = {

// Report the current range since this statement is reachable or is
// not consecutive to the current range.
if (!range.isEmpty) {
if (!range.isEmpty && range.hasNonEmptyStatement) {
context.report({
message: "Unreachable code.",
loc: range.location,
Expand Down
58 changes: 57 additions & 1 deletion tests/lib/rules/no-unreachable.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ ruleTester.run("no-unreachable", rule, {
"function foo() { var x = 1; for (x in {}) { return; } x = 2; }",
"function foo() { var x = 1; try { return; } finally { x = 2; } }",
"function foo() { var x = 1; for (;;) { if (x) break; } x = 2; }",
"A: { break A; } foo()"
"A: { break A; } foo()",

// EmptyStatements should not be reported (see rule "no-empty")
// https://github.com/eslint/eslint/issues/9081
"switch (foo) { default: throw new Error(); };",
"function a() { if (foo) { return; } else { return; }; }",
"function foo() { return x; ; }",
"switch (foo) { default: throw new Error(); };;;;",
"function a() { if (foo) { return; } else { return; };;;; }",
"function foo() { return x; ;;;; }"
],
invalid: [
{ code: "function foo() { return x; var x = 1; }", errors: [{ message: "Unreachable code.", type: "VariableDeclaration" }] },
Expand Down Expand Up @@ -185,6 +194,53 @@ ruleTester.run("no-unreachable", rule, {
endColumn: 25
}
]
},

// EmptyStatements should not be reported (see rule "no-empty")
// https://github.com/eslint/eslint/issues/9081
{
code: "switch (foo) { default: throw new Error(); }; bar();",
errors: [{
message: "Unreachable code.",
type: "EmptyStatement",
line: 1,
column: 45,
endLine: 1,
endColumn: 53
}]
},
{
code: "function a() { if (foo) { return; } else { return; }; bar(); }",
errors: [{
message: "Unreachable code.",
type: "EmptyStatement",
line: 1,
column: 53,
endLine: 1,
endColumn: 61
}]
},
{
code: "function foo() { return x; ; bar(); }",
errors: [{
message: "Unreachable code.",
type: "EmptyStatement",
line: 1,
column: 28,
endLine: 1,
endColumn: 36
}]
},
{
code: "function foo() { return x; ;;;;; bar(); ;; }",
errors: [{
message: "Unreachable code.",
type: "EmptyStatement",
line: 1,
column: 28,
endLine: 1,
endColumn: 43
}]
}
]
});