Skip to content

Reject using/await using declarations in switch case/default clauses#62731

Closed
PaulyBearCoding wants to merge 1 commit intomicrosoft:mainfrom
PaulyBearCoding:parser/using-switch-validation
Closed

Reject using/await using declarations in switch case/default clauses#62731
PaulyBearCoding wants to merge 1 commit intomicrosoft:mainfrom
PaulyBearCoding:parser/using-switch-validation

Conversation

@PaulyBearCoding
Copy link
Copy Markdown

Summary

Implements parser validation to reject using and await using declarations when directly nested within case or default clauses of switch statements, per updated ECMAScript spec.

Fixes #62708

Background

The ECMAScript spec was updated (rbuckton/ecma262#14) to disallow using declarations in switch case/default clauses. All major JS engines have agreed to implement this restriction.

Rationale:

  • Makes the number of resource declarations statically knowable
  • Simplifies spec (removes special cases in HasCallInTailPosition and HasUnterminatedUsingDeclaration)
  • Pattern is rarely used in practice ("obscure, at best" per spec authors)

Related implementations:

Changes

1. Parser Validation

  • Modified parseVariableDeclarationList() in src/compiler/parser.ts
  • Check if parsingContext === ParsingContext.SwitchClauseStatements
  • Report error TS95198 for both using and await using

2. Diagnostic Message

  • Added new error message (code 95198) in src/compiler/diagnosticMessages.json
  • Format: A '{0}' declaration cannot be placed within a 'case' or 'default' clause.

3. Test Baseline

  • Added error baseline for tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarations.1.ts
  • Lines 100, 104, 111 now correctly produce TS95198 errors

Valid Workaround

Users can wrap using declarations in a block statement:

// ❌ Error
switch (x) {
  case 0:
    using resource = ...;  // Error TS95198
    break;
}

// ✅ Valid
switch (x) {
  case 0: {
    using resource = ...;  // OK - wrapped in block
    break;
  }
}

Testing

  • Added error baseline for existing test case
  • Verified parser correctly rejects using in case clauses
  • Verified parser correctly rejects await using in case clauses
  • Verified parser correctly rejects using in default clauses
  • Verified valid code (block-wrapped, outside switch) still compiles

Checklist

  • Parser validation implemented
  • Diagnostic message added
  • Test baselines updated
  • Clear error message with helpful guidance
  • No false positives (valid code unaffected)

🤖 Generated with Claude Code

Fixes microsoft#62708

This change implements parser validation to reject `using` and `await using`
declarations when directly nested within `case` or `default` clauses of switch
statements, as per the updated ECMAScript spec.

The spec was updated to disallow this pattern (see rbuckton/ecma262#14) because:
- It makes the number of resource declarations statically knowable
- All major JS engines agreed to this restriction
- The pattern is rarely used in practice

Changes:
- Add new diagnostic (TS95198) for the error message
- Add validation in parseVariableDeclarationList() to check parsingContext
- Add error baseline for existing test case that now produces errors

Valid workaround: Wrap using declarations in a block statement:
  case 0: {
    using x = ...;  // OK
  }

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Nov 7, 2025
@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Nov 7, 2025
@github-project-automation github-project-automation bot moved this from Not started to Done in PR Backlog Nov 7, 2025
@PaulyBearCoding PaulyBearCoding deleted the parser/using-switch-validation branch November 7, 2025 07:33
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.

Should reject using / await using in a switch case / default clause

2 participants