fix: skip block-scoped variable check for label identifiers#63365
Closed
Ankitajainkuniya wants to merge 2 commits intomicrosoft:mainfrom
Closed
fix: skip block-scoped variable check for label identifiers#63365Ankitajainkuniya wants to merge 2 commits intomicrosoft:mainfrom
Ankitajainkuniya wants to merge 2 commits intomicrosoft:mainfrom
Conversation
Labels and variables occupy separate namespaces in JavaScript/TypeScript. When a label name coincides with a later let/const variable declaration, the compiler incorrectly reports "Block-scoped variable used before its declaration" on the label identifier or break/continue target. This adds a guard in checkResolvedBlockScopedVariable to skip the check when the error location is the label of a LabeledStatement, BreakStatement, or ContinueStatement, since these reference labels, not variables. Fixes microsoft#30408
Member
|
@Ankitajainkuniya what agent did you use to write this? |
Author
@RyanCavanaugh I used OpenAI as a coding assistant for parts of this, with my own review and modifications. The architecture decisions and product logic are mine the AI helped with implementation speed. I'm building several open-source projects with this workflow: github.com/Ankitajainkuniya |
Member
|
Please have your workflow ingest https://github.com/microsoft/TypeScript/blob/main/CONTRIBUTING.md before proceeding. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #30408
Labels and variables occupy separate namespaces in JavaScript/TypeScript. When a label name coincides with a later
let/constvariable declaration, the compiler incorrectly reports "Block-scoped variable 'foo' used before its declaration" on the label.Repro
Before: Error TS2448 on
foo:andbreak foo— "Block-scoped variable 'foo' used before its declaration"After: No error (correct behavior —
foo:is a label, not a variable reference)Fix
Added a guard in
checkResolvedBlockScopedVariable(checker.ts) that skips the "used before declaration" check when the identifier is:labelof aLabeledStatement(e.g.,foo: while (...))labelof aBreakStatementorContinueStatement(e.g.,break foo)Regression Test
Normal "used before declaration" errors still fire correctly:
Test Case
Added
tests/cases/conformance/statements/labeledStatements/labeledStatementWithBlockScopedVariable.tscovering:letvariable withbreakconstvariable withcontinue