Skip to content

Commit

Permalink
Fix: no-unreachable reporting EmptyStatements (fixes #9081)
Browse files Browse the repository at this point in the history
no-unreachable will only report if there is at least one non-EmptyStatement node in the range
  • Loading branch information
platinumazure committed Aug 7, 2017
1 parent a113cd3 commit bff1b0c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/rules/no-unreachable.js
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
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
}]
}
]
});

0 comments on commit bff1b0c

Please sign in to comment.