Skip to content

Issue "'{0}' declarations can only be declared inside a block." for block-scoped variables in presence of parse errors #61824

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

Merged

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Jun 6, 2025

fixes #61734

@Copilot Copilot AI review requested due to automatic review settings June 6, 2025 18:47
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Jun 6, 2025
@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Jun 6, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes inconsistent handling of block-scoped declarations in the presence of parse errors by switching from grammarErrorOnNode to directly adding diagnostics, and adds tests to verify the TS1156 error for const, let, using, and await using declarations outside blocks.

  • Replace grammarErrorOnNode calls with diagnostics.add(createDiagnosticForNode(...)) in the checker.
  • Introduce a new compiler-test file covering parse-error scenarios for block-scoped declarations.
  • Update baselines for types, symbols, and error outputs to reflect the new diagnostic behavior.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/cases/compiler/disallowedBlockScopedInPresenceOfParseErrors1.ts New test cases for invalid block-scoped declarations with parse errors
tests/baselines/reference/disallowedBlockScopedInPresenceOfParseErrors1.types Updated baseline types output
tests/baselines/reference/disallowedBlockScopedInPresenceOfParseErrors1.symbols Updated baseline symbol output
tests/baselines/reference/disallowedBlockScopedInPresenceOfParseErrors1.errors.txt Updated baseline error messages
src/compiler/checker.ts Swapped grammarErrorOnNode return into a direct diagnostics.add call
Comments suppressed due to low confidence (1)

src/compiler/checker.ts:52839

  • The original code returned immediately after calling grammarErrorOnNode, short-circuiting further checks. Removing the return changes control flow and may allow unintended processing after the diagnostic. Consider restoring the return to maintain the original behavior.
diagnostics.add(createDiagnosticForNode(node, Diagnostics._0_declarations_can_only_be_declared_inside_a_block, keyword));

…lock-scoped variables in presence of parse errors
@Andarist Andarist force-pushed the fix/block-scoped-error-when-parse-errors branch from daad9bc to 0c24774 Compare June 6, 2025 18:48
@github-project-automation github-project-automation bot moved this from Not started to Needs merge in PR Backlog Jun 6, 2025
@jakebailey jakebailey merged commit fa2a0fc into microsoft:main Jun 6, 2025
32 checks passed
@github-project-automation github-project-automation bot moved this from Needs merge to Done in PR Backlog Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

"'const' declarations can only be declared inside a block" should be issued more aggressively
3 participants