Improve label reference diagnostics#63472
Open
MukundaKatta wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates TypeScript’s grammar checking for break/continue with labels so that referencing a label declared later in the same function reports a targeted diagnostic (with related info pointing at the label declaration), while preserving existing diagnostics for other invalid label scenarios.
Changes:
- Added new compiler test + baselines covering “label declared later”, missing labels, non-enclosing labels, and cross-function label jumps.
- Introduced a new diagnostic (TS18001) and wired it into
break/continuegrammar checking with related info on the label declaration. - Added helper functions in the checker to detect “label declared later in same function” vs cross-function label containment.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/cases/compiler/labelReferenceDiagnostics.ts | New test cases for improved label reference diagnostics. |
| tests/baselines/reference/labelReferenceDiagnostics.types | Baseline update for type display for the new test. |
| tests/baselines/reference/labelReferenceDiagnostics.symbols | Baseline update for symbol display for the new test. |
| tests/baselines/reference/labelReferenceDiagnostics.js | Baseline update for JS emit for the new test. |
| tests/baselines/reference/labelReferenceDiagnostics.errors.txt | Baseline update to validate the new targeted diagnostic + related info. |
| src/compiler/diagnosticMessages.json | Adds the new TS18001 diagnostic message. |
| src/compiler/checker.ts | Implements the new diagnostic behavior and related-info reporting for later-declared labels. |
Comments suppressed due to low confidence (2)
src/compiler/checker.ts:1
- The
label.pos > node.poscomparison is trivia-sensitive (posincludes leading comments/whitespace). This can misclassify cases where the label’s leading trivia starts earlier than the jump statement even though the label token is later. Compare using token starts instead (e.g.,getStart(sourceFile)/getTokenPosOfNode) for both the jump and the label identifier so the diagnostic triggers based on actual syntax positions.
src/compiler/checker.ts:1 - The containment check mixes manual
pos/endrange comparisons (also trivia-sensitive) with label matching. Using an existing range helper (e.g.,rangeContainsRange/containsRange) and comparing viagetStart/getEnd(or token positions) would make the intent clearer and reduce the risk of subtle off-by-trivia issues when future refactors introduce comments/formatting changes.
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.
Fixes #30408.
This updates break/continue grammar checking so a labeled jump that references a label declared later in the same function scope reports a targeted diagnostic instead of the generic function-boundary error. The new error includes a related span pointing at the label declaration.
The test coverage also keeps the existing behavior for missing, non-enclosing, and cross-function labels.
Validation:
npx -p node@22 -c './node_modules/.bin/hereby runtests --tests=labelReferenceDiagnostics'\n-npx -p node@22 -c 'npm run build:compiler'