Skip to content

Commit 1e24945

Browse files
authored
explicitly disallow using in ambient contexts (#61781)
1 parent 652ed7f commit 1e24945

File tree

8 files changed

+131
-10
lines changed

8 files changed

+131
-10
lines changed

src/compiler/checker.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52856,17 +52856,28 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
5285652856
}
5285752857

5285852858
const blockScopeFlags = declarationList.flags & NodeFlags.BlockScoped;
52859-
if ((blockScopeFlags === NodeFlags.Using || blockScopeFlags === NodeFlags.AwaitUsing) && isForInStatement(declarationList.parent)) {
52860-
return grammarErrorOnNode(
52861-
declarationList,
52862-
blockScopeFlags === NodeFlags.Using ?
52863-
Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_using_declaration :
52864-
Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_an_await_using_declaration,
52865-
);
52866-
}
52859+
if (blockScopeFlags === NodeFlags.Using || blockScopeFlags === NodeFlags.AwaitUsing) {
52860+
if (isForInStatement(declarationList.parent)) {
52861+
return grammarErrorOnNode(
52862+
declarationList,
52863+
blockScopeFlags === NodeFlags.Using ?
52864+
Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_using_declaration :
52865+
Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_an_await_using_declaration,
52866+
);
52867+
}
5286752868

52868-
if (blockScopeFlags === NodeFlags.AwaitUsing) {
52869-
return checkAwaitGrammar(declarationList);
52869+
if (declarationList.flags & NodeFlags.Ambient) {
52870+
return grammarErrorOnNode(
52871+
declarationList,
52872+
blockScopeFlags === NodeFlags.Using ?
52873+
Diagnostics.using_declarations_are_not_allowed_in_ambient_contexts :
52874+
Diagnostics.await_using_declarations_are_not_allowed_in_ambient_contexts,
52875+
);
52876+
}
52877+
52878+
if (blockScopeFlags === NodeFlags.AwaitUsing) {
52879+
return checkAwaitGrammar(declarationList);
52880+
}
5287052881
}
5287152882

5287252883
return false;

src/compiler/diagnosticMessages.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,14 @@
18371837
"category": "Error",
18381838
"code": 1544
18391839
},
1840+
"'using' declarations are not allowed in ambient contexts.": {
1841+
"category": "Error",
1842+
"code": 1545
1843+
},
1844+
"'await using' declarations are not allowed in ambient contexts.": {
1845+
"category": "Error",
1846+
"code": 1546
1847+
},
18401848

18411849
"The types of '{0}' are incompatible between these types.": {
18421850
"category": "Error",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
awaitUsingDeclarations.16.ts(2,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
2+
awaitUsingDeclarations.16.ts(3,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
3+
awaitUsingDeclarations.16.ts(6,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
4+
awaitUsingDeclarations.16.ts(7,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
5+
6+
7+
==== awaitUsingDeclarations.16.ts (4 errors) ====
8+
declare namespace N {
9+
await using x: { [Symbol.asyncDispose](): Promise<void> };
10+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
12+
await using y: null;
13+
~~~~~~~~~~~~~~~~~~~
14+
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
15+
}
16+
declare module 'M' {
17+
await using x: { [Symbol.asyncDispose](): Promise<void> };
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
20+
await using y: null;
21+
~~~~~~~~~~~~~~~~~~~
22+
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
23+
}
24+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarations.16.ts] ////
2+
3+
//// [awaitUsingDeclarations.16.ts]
4+
declare namespace N {
5+
await using x: { [Symbol.asyncDispose](): Promise<void> };
6+
await using y: null;
7+
}
8+
declare module 'M' {
9+
await using x: { [Symbol.asyncDispose](): Promise<void> };
10+
await using y: null;
11+
}
12+
13+
14+
//// [awaitUsingDeclarations.16.js]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
usingDeclarations.16.ts(2,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
2+
usingDeclarations.16.ts(3,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
3+
usingDeclarations.16.ts(6,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
4+
usingDeclarations.16.ts(7,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
5+
6+
7+
==== usingDeclarations.16.ts (4 errors) ====
8+
declare namespace N {
9+
using x: { [Symbol.dispose](): void };
10+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
12+
using y: null;
13+
~~~~~~~~~~~~~
14+
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
15+
}
16+
declare module 'M' {
17+
using x: { [Symbol.dispose](): void };
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
20+
using y: null;
21+
~~~~~~~~~~~~~
22+
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
23+
}
24+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarations.16.ts] ////
2+
3+
//// [usingDeclarations.16.ts]
4+
declare namespace N {
5+
using x: { [Symbol.dispose](): void };
6+
using y: null;
7+
}
8+
declare module 'M' {
9+
using x: { [Symbol.dispose](): void };
10+
using y: null;
11+
}
12+
13+
14+
//// [usingDeclarations.16.js]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @target: esnext
2+
// @module: esnext
3+
// @lib: esnext
4+
// @noTypesAndSymbols: true
5+
6+
declare namespace N {
7+
await using x: { [Symbol.asyncDispose](): Promise<void> };
8+
await using y: null;
9+
}
10+
declare module 'M' {
11+
await using x: { [Symbol.asyncDispose](): Promise<void> };
12+
await using y: null;
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @target: esnext
2+
// @module: esnext
3+
// @lib: esnext
4+
// @noTypesAndSymbols: true
5+
6+
declare namespace N {
7+
using x: { [Symbol.dispose](): void };
8+
using y: null;
9+
}
10+
declare module 'M' {
11+
using x: { [Symbol.dispose](): void };
12+
using y: null;
13+
}

0 commit comments

Comments
 (0)