Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ import {
isCallLikeOrFunctionLikeExpression,
isCallOrNewExpression,
isCallSignatureDeclaration,
isCaseOrDefaultClause,
isCatchClause,
isCatchClauseVariableDeclaration,
isCatchClauseVariableDeclarationOrBindingElement,
Expand Down Expand Up @@ -53081,6 +53082,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_an_await_using_declaration,
);
}
else if (isVariableStatement(declarationList.parent) && isCaseOrDefaultClause(declarationList.parent.parent)) {
return grammarErrorOnNode(
declarationList,
blockScopeFlags === NodeFlags.Using ?
Diagnostics.using_declarations_are_not_allowed_in_case_or_default_clauses_unless_contained_within_a_block :
Diagnostics.await_using_declarations_are_not_allowed_in_case_or_default_clauses_unless_contained_within_a_block,
);
}

if (declarationList.flags & NodeFlags.Ambient) {
return grammarErrorOnNode(
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,14 @@
"category": "Error",
"code": 1546
},
"'using' declarations are not allowed in 'case' or 'default' clauses unless contained within a block.": {
"category": "Error",
"code": 1547
},
"'await using' declarations are not allowed in 'case' or 'default' clauses unless contained within a block.": {
"category": "Error",
"code": 1548
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
Expand Down
82 changes: 0 additions & 82 deletions src/compiler/transformers/esnext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
BindingElement,
Block,
Bundle,
CaseOrDefaultClause,
chainBundle,
ClassDeclaration,
Debug,
Expand All @@ -25,7 +24,6 @@ import {
isArray,
isBindingPattern,
isBlock,
isCaseClause,
isCustomPrologue,
isExpression,
isGeneratedIdentifier,
Expand All @@ -49,7 +47,6 @@ import {
skipOuterExpressions,
SourceFile,
Statement,
SwitchStatement,
SyntaxKind,
TransformationContext,
TransformFlags,
Expand Down Expand Up @@ -123,9 +120,6 @@ export function transformESNext(context: TransformationContext): (x: SourceFile
case SyntaxKind.ForOfStatement:
return visitForOfStatement(node as ForOfStatement);

case SyntaxKind.SwitchStatement:
return visitSwitchStatement(node as SwitchStatement);

default:
return visitEachChild(node, visitor, context);
}
Expand Down Expand Up @@ -339,72 +333,6 @@ export function transformESNext(context: TransformationContext): (x: SourceFile
return visitEachChild(node, visitor, context);
}

function visitCaseOrDefaultClause(node: CaseOrDefaultClause, envBinding: Identifier) {
if (getUsingKindOfStatements(node.statements) !== UsingKind.None) {
if (isCaseClause(node)) {
return factory.updateCaseClause(
node,
visitNode(node.expression, visitor, isExpression),
transformUsingDeclarations(node.statements, /*start*/ 0, node.statements.length, envBinding, /*topLevelStatements*/ undefined),
);
}
else {
return factory.updateDefaultClause(
node,
transformUsingDeclarations(node.statements, /*start*/ 0, node.statements.length, envBinding, /*topLevelStatements*/ undefined),
);
}
}
return visitEachChild(node, visitor, context);
}

function visitSwitchStatement(node: SwitchStatement) {
// given:
//
// switch (expr) {
// case expr:
// using res = expr;
// }
//
// produces:
//
// const env_1 = { stack: [], error: void 0, hasError: false };
// try {
// switch(expr) {
// case expr:
// const res = __addDisposableResource(env_1, expr, false);
// }
// }
// catch (e_1) {
// env_1.error = e_1;
// env_1.hasError = true;
// }
// finally {
// __disposeResources(env_1);
// }
//
const usingKind = getUsingKindOfCaseOrDefaultClauses(node.caseBlock.clauses);
if (usingKind) {
const envBinding = createEnvBinding();
return createDownlevelUsingStatements(
[
factory.updateSwitchStatement(
node,
visitNode(node.expression, visitor, isExpression),
factory.updateCaseBlock(
node.caseBlock,
node.caseBlock.clauses.map(clause => visitCaseOrDefaultClause(clause, envBinding)),
),
),
],
envBinding,
usingKind === UsingKind.Async,
);
}

return visitEachChild(node, visitor, context);
}

/**
* Transform `using` declarations in a statement list.
*/
Expand Down Expand Up @@ -870,13 +798,3 @@ function getUsingKindOfStatements(statements: readonly Statement[]): UsingKind {
}
return result;
}

function getUsingKindOfCaseOrDefaultClauses(clauses: readonly CaseOrDefaultClause[]): UsingKind {
let result = UsingKind.None;
for (const clause of clauses) {
const usingKind = getUsingKindOfStatements(clause.statements);
if (usingKind === UsingKind.Async) return UsingKind.Async;
if (usingKind > result) result = usingKind;
}
return result;
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
awaitUsingDeclarations.1.ts(1,1): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(36,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(41,9): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(45,9): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(52,13): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(57,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(60,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(63,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(67,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(70,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(46,9): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(51,9): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(58,13): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(63,13): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(68,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(71,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(74,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(79,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(78,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(81,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(85,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(90,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(94,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(96,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(101,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
awaitUsingDeclarations.1.ts(105,5): error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.


==== awaitUsingDeclarations.1.ts (15 errors) ====
==== awaitUsingDeclarations.1.ts (17 errors) ====
await using d1 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
Expand Down Expand Up @@ -59,85 +61,100 @@ awaitUsingDeclarations.1.ts(94,5): error TS2854: Top-level 'await using' stateme
}

switch (Math.random()) {
case 0:
case 0: {
await using d20 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
break;
}

case 1:
case 1: {
await using d21 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
break;
}

default: {
await using d22 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
}
}

if (true)
switch (0) {
case 0:
await using d22 = { async [Symbol.asyncDispose]() {} };
case 0: {
await using d23 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
break;
}

default: {
await using d24 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
}
}

try {
await using d23 = { async [Symbol.asyncDispose]() {} };
await using d25 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
}
catch {
await using d24 = { async [Symbol.asyncDispose]() {} };
await using d26 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
}
finally {
await using d25 = { async [Symbol.asyncDispose]() {} };
await using d27 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
}

if (true) {
await using d26 = { async [Symbol.asyncDispose]() {} };
await using d28 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
}
else {
await using d27 = { async [Symbol.asyncDispose]() {} };
await using d29 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
}

while (true) {
await using d28 = { async [Symbol.asyncDispose]() {} };
await using d30 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
break;
}

do {
await using d29 = { async [Symbol.asyncDispose]() {} };
await using d31 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
break;
}
while (true);

for (;;) {
await using d30 = { async [Symbol.asyncDispose]() {} };
await using d32 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
break;
}

for (const x in {}) {
await using d31 = { async [Symbol.asyncDispose]() {} };
await using d33 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
}

for (const x of []) {
await using d32 = { async [Symbol.asyncDispose]() {} };
await using d34 = { async [Symbol.asyncDispose]() {} };
~~~~~
!!! error TS2854: Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
}
Expand Down
Loading