-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Detect usage of block-scoped variables from earlier case blocks #47160
Conversation
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.
Looks good, just thinking about ways we can give some more context to the error when possible.
const usageCaseBlock = getAncestor(node, SyntaxKind.CaseClause); | ||
if (usageCaseBlock) { | ||
if (usageCaseBlock.pos > symbol.valueDeclaration.pos) { | ||
error(node, Diagnostics.Variable_0_is_declared_in_a_prior_case_block, symbolToString(symbol)); |
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.
Add a "'0' is declared here" related span to the valueDeclaration
.
@@ -3361,6 +3361,11 @@ | |||
"category": "Error", | |||
"code": 2836 | |||
}, | |||
"Variable '{0}' is declared in a prior case block.": { |
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.
case needs to be quoted
"Variable '{0}' is declared in a prior case block.": { | |
"Variable '{0}' is declared in a prior 'case' block.": { | |
It's tough to give some more context, but I think it could be helpful. Maybe something like
"Variable '{0}' is declared in a prior case block.": { | |
"Variable '{0}' is declared in a prior 'case' block and cannot be referenced if that declaration does not execute.": { |
@typescript-bot user test this |
Heya @RyanCavanaugh, I've started to run the parallelized community code test suite on this PR at e223f1b. You can monitor the build here. |
The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master. |
@typescript-bot test this |
Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at e223f1b. You can monitor the build here. |
FWIW, despite technically being a false positive, I find the sample code in the OP to be... somewhat suspicious, to put it lightly. Fallthrough is a thing, but |
@RyanCavanaugh do you want to come back to this? It's been sitting for a long time. |
I'm not super keen on this one to be honest. |
Fixes #19503
Note that this has the possibility to incur a false positive: