diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 5731936e4978e..b12931c9a7684 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -8332,6 +8332,10 @@ "category": "Message", "code": 95197 }, + "A '{0}' declaration cannot be placed within a 'case' or 'default' clause.": { + "category": "Error", + "code": 95198 + }, "No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": { "category": "Error", diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 68133ac5f1e40..6053a12717662 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -7678,12 +7678,18 @@ namespace Parser { break; case SyntaxKind.UsingKeyword: flags |= NodeFlags.Using; + if (parsingContext === ParsingContext.SwitchClauseStatements) { + parseErrorAtCurrentToken(Diagnostics.A_0_declaration_cannot_be_placed_within_a_case_or_default_clause, "using"); + } break; case SyntaxKind.AwaitKeyword: if (!isAwaitUsingDeclaration()) { break; } flags |= NodeFlags.AwaitUsing; + if (parsingContext === ParsingContext.SwitchClauseStatements) { + parseErrorAtCurrentToken(Diagnostics.A_0_declaration_cannot_be_placed_within_a_case_or_default_clause, "await using"); + } nextToken(); break; default: diff --git a/tests/baselines/reference/usingDeclarations.1(target=esnext).errors.txt b/tests/baselines/reference/usingDeclarations.1(target=esnext).errors.txt new file mode 100644 index 0000000000000..7a043110be79c --- /dev/null +++ b/tests/baselines/reference/usingDeclarations.1(target=esnext).errors.txt @@ -0,0 +1,28 @@ +usingDeclarations.1.ts(100,9): error TS95198: A 'using' declaration cannot be placed within a 'case' or 'default' clause. +usingDeclarations.1.ts(104,9): error TS95198: A 'using' declaration cannot be placed within a 'case' or 'default' clause. +usingDeclarations.1.ts(111,13): error TS95198: A 'using' declaration cannot be placed within a 'case' or 'default' clause. + + +==== usingDeclarations.1.ts (3 errors) ==== + switch (Math.random()) { + case 0: + using d20 = { [Symbol.dispose]() {} }; + ~~~~~ +!!! error TS95198: A 'using' declaration cannot be placed within a 'case' or 'default' clause. + break; + + case 1: + using d21 = { [Symbol.dispose]() {} }; + ~~~~~ +!!! error TS95198: A 'using' declaration cannot be placed within a 'case' or 'default' clause. + break; + } + + if (true) + switch (0) { + case 0: + using d22 = { [Symbol.dispose]() {} }; + ~~~~~ +!!! error TS95198: A 'using' declaration cannot be placed within a 'case' or 'default' clause. + break; + }