-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
feb4a17
feat: (no-fallthrough) Report unused fallthrough comments
kirkwaiblinger 459d94c
add space
kirkwaiblinger dcf8409
add a few test cases to ensure state doesn't leak across multiple swi…
kirkwaiblinger a976b0e
add correct case in docs
kirkwaiblinger 6f067b0
fix leaked state
kirkwaiblinger 7289e59
Fix docs typo
kirkwaiblinger afed1bf
add some test coverage
kirkwaiblinger File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,23 +48,27 @@ function isFallThroughComment(comment, fallthroughCommentPattern) { | |
* @param {ASTNode} subsequentCase The case after caseWhichFallsThrough. | ||
* @param {RuleContext} context A rule context which stores comments. | ||
* @param {RegExp} fallthroughCommentPattern A pattern to match comment to. | ||
* @returns {boolean} `true` if the case has a valid fallthrough comment. | ||
* @returns {null | object} the comment if the case has a valid fallthrough comment, otherwise null | ||
*/ | ||
function hasFallthroughComment(caseWhichFallsThrough, subsequentCase, context, fallthroughCommentPattern) { | ||
function getFallthroughComment(caseWhichFallsThrough, subsequentCase, context, fallthroughCommentPattern) { | ||
const sourceCode = context.sourceCode; | ||
|
||
if (caseWhichFallsThrough.consequent.length === 1 && caseWhichFallsThrough.consequent[0].type === "BlockStatement") { | ||
const trailingCloseBrace = sourceCode.getLastToken(caseWhichFallsThrough.consequent[0]); | ||
const commentInBlock = sourceCode.getCommentsBefore(trailingCloseBrace).pop(); | ||
|
||
if (commentInBlock && isFallThroughComment(commentInBlock.value, fallthroughCommentPattern)) { | ||
return true; | ||
return commentInBlock; | ||
} | ||
} | ||
|
||
const comment = sourceCode.getCommentsBefore(subsequentCase).pop(); | ||
|
||
return Boolean(comment && isFallThroughComment(comment.value, fallthroughCommentPattern)); | ||
if (comment && isFallThroughComment(comment.value, fallthroughCommentPattern)) { | ||
return comment; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
|
@@ -103,12 +107,17 @@ module.exports = { | |
allowEmptyCase: { | ||
type: "boolean", | ||
default: false | ||
}, | ||
reportUnusedFallthroughComment: { | ||
type: "boolean", | ||
default: false | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
], | ||
messages: { | ||
unusedFallthroughComment: "Found a comment that would permit fallthrough, but case cannot fall through.", | ||
case: "Expected a 'break' statement before 'case'.", | ||
default: "Expected a 'break' statement before 'default'." | ||
} | ||
|
@@ -120,12 +129,13 @@ module.exports = { | |
let currentCodePathSegments = new Set(); | ||
const sourceCode = context.sourceCode; | ||
const allowEmptyCase = options.allowEmptyCase || false; | ||
const reportUnusedFallthroughComment = options.reportUnusedFallthroughComment || false; | ||
|
||
/* | ||
* We need to use leading comments of the next SwitchCase node because | ||
* trailing comments is wrong if semicolons are omitted. | ||
*/ | ||
let fallthroughCase = null; | ||
let previousCase = null; | ||
let fallthroughCommentPattern = null; | ||
|
||
if (options.commentPattern) { | ||
|
@@ -168,13 +178,24 @@ module.exports = { | |
* And reports the previous fallthrough node if that does not exist. | ||
*/ | ||
|
||
if (fallthroughCase && (!hasFallthroughComment(fallthroughCase, node, context, fallthroughCommentPattern))) { | ||
context.report({ | ||
messageId: node.test ? "case" : "default", | ||
node | ||
}); | ||
if (previousCase) { | ||
const previousCaseFallthroughComment = getFallthroughComment(previousCase.node, node, context, fallthroughCommentPattern); | ||
|
||
if (previousCase.isFallthrough && !(previousCaseFallthroughComment)) { | ||
context.report({ | ||
messageId: node.test ? "case" : "default", | ||
node | ||
}); | ||
} else if (reportUnusedFallthroughComment && !previousCase.isSwitchExitReachable && previousCaseFallthroughComment) { | ||
context.report({ | ||
messageId: "unusedFallthroughComment", | ||
node: previousCaseFallthroughComment | ||
}); | ||
} | ||
|
||
|
||
} | ||
fallthroughCase = null; | ||
previousCase = null; | ||
}, | ||
|
||
"SwitchCase:exit"(node) { | ||
|
@@ -185,11 +206,16 @@ module.exports = { | |
* `break`, `return`, or `throw` are unreachable. | ||
* And allows empty cases and the last case. | ||
*/ | ||
if (isAnySegmentReachable(currentCodePathSegments) && | ||
(node.consequent.length > 0 || (!allowEmptyCase && hasBlankLinesBetween(node, nextToken))) && | ||
node.parent.cases.at(-1) !== node) { | ||
fallthroughCase = node; | ||
} | ||
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 | ||
}; | ||
Comment on lines
+208
to
+216
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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! |
||
|
||
} | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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