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

feat: add allowEmptyCase option to no-fallthrough rule #15887

Merged
merged 8 commits into from Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
29 changes: 27 additions & 2 deletions docs/src/rules/no-fallthrough.md
Expand Up @@ -163,9 +163,11 @@ Note that the last `case` statement in these examples does not cause a warning b

## Options

This rule accepts a single options argument:
This rule has an object option for exceptions:
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

* Set the `commentPattern` option to a regular expression string to change the test for intentional fallthrough comment
* Set the `commentPattern` option to a regular expression string to change the test for intentional fallthrough comment.

* Set the `allowEmptyCase` option to true to allow empty `case` intentionally.
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

### commentPattern

Expand Down Expand Up @@ -193,6 +195,29 @@ switch(foo) {
}
```

### allowEmptyCase

Examples for **correct** code for the `{ "allowEmptyCase": true }` option:
amareshsm marked this conversation as resolved.
Show resolved Hide resolved

```js
/* eslint no-fallthrough: ["error", { "allowEmptyCase": true }] */

switch(foo){
case 1:

case 2: doSomething();
}

switch(foo){
case 1:
/*
Put a message here
*/
case 2: doSomething();
}

```

## When Not To Use It

If you don't want to enforce that each `case` statement should end with a `throw`, `return`, `break`, or comment, then you can safely turn this rule off.
38 changes: 32 additions & 6 deletions lib/rules/no-fallthrough.js
Expand Up @@ -35,6 +35,22 @@ function hasFallthroughComment(caseWhichFallsThrough, subsequentCase, context, f
return Boolean(comment && fallthroughCommentPattern.test(comment.value));
}

/**
* Checks whether or not a given case is empty.
* @param {ASTNode} caseWhichFallsThrough SwitchCase node which falls through.
* @param {RuleContext} context A rule context which stores comments.
* @returns {boolean} `true` if the case is empty.
*/
function isEmptyCase(caseWhichFallsThrough, context) {
const sourceCode = context.getSourceCode();

if (caseWhichFallsThrough.consequent.length === 0 ||
(caseWhichFallsThrough.consequent.length === 1 && caseWhichFallsThrough.consequent[0].type === "EmptyStatement" && !sourceCode.getLastToken(caseWhichFallsThrough.consequent[0]).value === ";")) {
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
}

/**
* Checks whether or not a given code path segment is reachable.
* @param {CodePathSegment} segment A CodePathSegment to check.
Expand Down Expand Up @@ -76,6 +92,10 @@ module.exports = {
commentPattern: {
type: "string",
default: ""
},
allowEmptyCase: {
type: "boolean",
default: false
}
},
additionalProperties: false
Expand All @@ -91,6 +111,7 @@ module.exports = {
const options = context.options[0] || {};
let currentCodePath = null;
const sourceCode = context.getSourceCode();
const allowEmptyCase = options.allowEmptyCase || false;

/*
* We need to use leading comments of the next SwitchCase node because
Expand All @@ -104,7 +125,6 @@ module.exports = {
} else {
fallthroughCommentPattern = DEFAULT_FALLTHROUGH_COMMENT;
}

return {
onCodePathStart(codePath) {
currentCodePath = codePath;
Expand All @@ -118,19 +138,25 @@ module.exports = {
/*
* Checks whether or not there is a fallthrough comment.
* And reports the previous fallthrough node if that does not exist.
*
*
amareshsm marked this conversation as resolved.
Show resolved Hide resolved
*/
if (fallthroughCase && !hasFallthroughComment(fallthroughCase, node, context, fallthroughCommentPattern)) {
context.report({
messageId: node.test ? "case" : "default",
node
});

if (fallthroughCase && (!hasFallthroughComment(fallthroughCase, node, context, fallthroughCommentPattern))) {
if (!(allowEmptyCase && isEmptyCase(fallthroughCase, context))) {
context.report({
messageId: node.test ? "case" : "default",
node
});
}
}
fallthroughCase = null;
},

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


/*
* `reachable` meant fall through because statements preceded by
* `break`, `return`, or `throw` are unreachable.
Expand Down
61 changes: 61 additions & 0 deletions tests/lib/rules/no-fallthrough.js
Expand Up @@ -92,6 +92,14 @@ ruleTester.run("no-fallthrough", rule, {
options: [{
commentPattern: "break[\\s\\w]+omitted"
}]
},
{
code: "switch(foo) { case 0: \n\n\n case 1: b(); }",
options: [{ allowEmptyCase: true }]
},
{
code: "switch(foo) { case 0: \n /* with comments */ \n case 1: b(); }",
options: [{ allowEmptyCase: true }]
}
],
invalid: [
Expand Down Expand Up @@ -214,6 +222,59 @@ ruleTester.run("no-fallthrough", rule, {
column: 1
}
]
},
{
code: "switch(foo) { case 0: \n /* with comments */ \ncase 1: b(); }",
errors: [
{
messageId: "case",
type: "SwitchCase",
line: 3,
column: 1
}
]
},
{
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
code: "switch (a) {\n case 1: \n /* empty */ case 2: ; case 3: }",
options: [{ allowEmptyCase: true }],
errors: [
{
messageId: "case",
type: "SwitchCase",
line: 3,
column: 24
}
]
},
{
code: "switch (a) {\n case 1: \n ; case 2: }",
options: [{ allowEmptyCase: true }],
errors: [
{
messageId: "case",
type: "SwitchCase",
line: 3,
column: 4
}
]
},
{
code: "switch (a) {\n case 1: ; case 2: ; case 3: }",
options: [{ allowEmptyCase: true }],
errors: [
{
messageId: "case",
type: "SwitchCase",
line: 2,
column: 12
},
{
messageId: "case",
type: "SwitchCase",
line: 2,
column: 22
}
]
}
]
});