Skip to content

Commit

Permalink
Merge pull request #5813 from eslint/issue4799
Browse files Browse the repository at this point in the history
Fix: `no-fallthrough` empty case with comment (fixes #5799)
  • Loading branch information
nzakas committed Apr 13, 2016
2 parents 16787e1 + cc14e43 commit 36d2f9a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/rules/no-fallthrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,24 @@ function isReachable(segment) {
return segment.reachable;
}

/**
* Checks whether a node and a token are separated by blank lines
* @param {ASTNode} node - The node to check
* @param {Token} token - The token to compare against
* @returns {boolean} `true` if there are blank lines between node and token
*/
function hasBlankLinesBetween(node, token) {
return token.loc.start.line > node.loc.end.line + 1;
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
var options = context.options[0] || {};
var currentCodePath = null;
var sourceCode = context.getSourceCode();

/*
* We need to use leading comments of the next SwitchCase node because
Expand Down Expand Up @@ -85,16 +96,16 @@ module.exports = function(context) {
},

"SwitchCase:exit": function(node) {
var nextToken = sourceCode.getTokenAfter(node);

/*
* `reachable` meant fall through because statements preceded by
* `break`, `return`, or `throw` are unreachable.
* And allows empty cases and the last case.
*/
if (currentCodePath.currentSegments.some(isReachable) &&
node.consequent.length > 0 &&
lodash.last(node.parent.cases) !== node
) {
(node.consequent.length > 0 || hasBlankLinesBetween(node, nextToken)) &&
lodash.last(node.parent.cases) !== node) {
fallthroughCase = node;
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/no-fallthrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ ruleTester.run("no-fallthrough", rule, {
"switch(foo) { case 0: case 1: a(); }",
"switch(foo) { case 0: case 1: a(); break; }",
"switch(foo) { case 0: case 1: break; }",
"switch(foo) { case 0:\n case 1: break; }",
"switch(foo) { case 0: // comment\n case 1: break; }",
"function foo() { switch(foo) { case 0: case 1: return; } }",
"function foo() { switch(foo) { case 0: {return;}\n case 1: {return;} } }",
"switch(foo) { case 0: case 1: {break;} }",
Expand Down Expand Up @@ -128,6 +130,14 @@ ruleTester.run("no-fallthrough", rule, {
code: "switch(foo) { case 0: do { break; } while (a); default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0:\n\n default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0:\n // comment\n default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: a(); /* falling through */ default: b() }",
errors: errorsDefault
Expand Down

0 comments on commit 36d2f9a

Please sign in to comment.