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 reportUnusedFallthroughComment
option to no-fallthrough rule
#18188
Conversation
✅ Deploy Preview for docs-eslint canceled.
|
|
||
::: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we can add a correct section here with an example having a comment that doesn't permit fallthrough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense! Added
const isSwitchExitReachable = isAnySegmentReachable(currentCodePathSegments); | ||
const isFallthrough = isSwitchExitReachable && (node.consequent.length > 0 || (!allowEmptyCase && hasBlankLinesBetween(node, nextToken))) && | ||
node.parent.cases.at(-1) !== node; | ||
|
||
previousCase = { | ||
node, | ||
isSwitchExitReachable, | ||
isFallthrough | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The state can leak across multiple switches this way:
/* eslint no-fallthrough: ["error", { "reportUnusedFallthroughComment": true }] */
switch(foo){
case 1:
doSomething();
break;
}
function f() {
switch(foo){
// falls through comment false positive
case 1:
if (a) {
throw new Error();
} else if (b) {
break;
} else {
return;
}
case 2:
break;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh dear. And I thought I was being all clever by adding those test cases 🤦
Fixed!
reportUnusedFallthroughComment
option to no-fallthrough rule
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one typo in the docs to fix and a couple of tests I think we should add, then LGTM.
{ | ||
code: ` | ||
switch(foo){ | ||
case 1: | ||
doSomething(); | ||
break; | ||
// falls through | ||
case 2: doSomething(); | ||
}`, | ||
options: [{ reportUnusedFallthroughComment: true }], | ||
errors: [ | ||
{ | ||
line: 6, | ||
messageId: "unusedFallthroughComment" | ||
} | ||
] | ||
}, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add a test where a fallthrough comment in a block is reported, for example:
{
code: `
switch (foo) {
case 0: {
a();
break;
// falls through
}
case 1:
b();
}`,
options: [{ reportUnusedFallthroughComment: true }],
errors: [
{
line: 6,
messageId: "unusedFallthroughComment"
}
]
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense! added
{ | ||
code: ` | ||
switch(foo){ | ||
case 1: | ||
doSomething(); | ||
break; | ||
// just a comment | ||
case 2: doSomething(); | ||
} | ||
`, | ||
options: [{ reportUnusedFallthroughComment: true }] | ||
|
||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add a valid test with a fallthrough comment, for example:
{
code: `
switch(foo){
case 1:
doSomething();
// falls through
case 2: doSomething();
}
`,
options: [{ reportUnusedFallthroughComment: true }]
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense! added
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks! Leaving open for @Tanujkanti4441 to verify.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks.
Thanks for contributing! |
Thanks! This was fun! 🙂 |
fixes #18182
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
#18182
What changes did you make? (Give an overview)
Add option (disabled by default) to report unused fallthrough comments in no-fallthrough.
Is there anything you'd like reviewers to focus on?
Changes are pretty straightforward I think.
PS this is my first PR to ESLint! very exciting