mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-04-17 01:49:41 -05:00
explicitly disallow using in ambient contexts (#61781)
This commit is contained in:
@@ -52856,17 +52856,28 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
|
||||
const blockScopeFlags = declarationList.flags & NodeFlags.BlockScoped;
|
||||
if ((blockScopeFlags === NodeFlags.Using || blockScopeFlags === NodeFlags.AwaitUsing) && isForInStatement(declarationList.parent)) {
|
||||
return grammarErrorOnNode(
|
||||
declarationList,
|
||||
blockScopeFlags === NodeFlags.Using ?
|
||||
Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_using_declaration :
|
||||
Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_an_await_using_declaration,
|
||||
);
|
||||
}
|
||||
if (blockScopeFlags === NodeFlags.Using || blockScopeFlags === NodeFlags.AwaitUsing) {
|
||||
if (isForInStatement(declarationList.parent)) {
|
||||
return grammarErrorOnNode(
|
||||
declarationList,
|
||||
blockScopeFlags === NodeFlags.Using ?
|
||||
Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_using_declaration :
|
||||
Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_an_await_using_declaration,
|
||||
);
|
||||
}
|
||||
|
||||
if (blockScopeFlags === NodeFlags.AwaitUsing) {
|
||||
return checkAwaitGrammar(declarationList);
|
||||
if (declarationList.flags & NodeFlags.Ambient) {
|
||||
return grammarErrorOnNode(
|
||||
declarationList,
|
||||
blockScopeFlags === NodeFlags.Using ?
|
||||
Diagnostics.using_declarations_are_not_allowed_in_ambient_contexts :
|
||||
Diagnostics.await_using_declarations_are_not_allowed_in_ambient_contexts,
|
||||
);
|
||||
}
|
||||
|
||||
if (blockScopeFlags === NodeFlags.AwaitUsing) {
|
||||
return checkAwaitGrammar(declarationList);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -1837,6 +1837,14 @@
|
||||
"category": "Error",
|
||||
"code": 1544
|
||||
},
|
||||
"'using' declarations are not allowed in ambient contexts.": {
|
||||
"category": "Error",
|
||||
"code": 1545
|
||||
},
|
||||
"'await using' declarations are not allowed in ambient contexts.": {
|
||||
"category": "Error",
|
||||
"code": 1546
|
||||
},
|
||||
|
||||
"The types of '{0}' are incompatible between these types.": {
|
||||
"category": "Error",
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
awaitUsingDeclarations.16.ts(2,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
|
||||
awaitUsingDeclarations.16.ts(3,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
|
||||
awaitUsingDeclarations.16.ts(6,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
|
||||
awaitUsingDeclarations.16.ts(7,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
|
||||
|
||||
|
||||
==== awaitUsingDeclarations.16.ts (4 errors) ====
|
||||
declare namespace N {
|
||||
await using x: { [Symbol.asyncDispose](): Promise<void> };
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
|
||||
await using y: null;
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
|
||||
}
|
||||
declare module 'M' {
|
||||
await using x: { [Symbol.asyncDispose](): Promise<void> };
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
|
||||
await using y: null;
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
|
||||
}
|
||||
|
||||
14
tests/baselines/reference/awaitUsingDeclarations.16.js
Normal file
14
tests/baselines/reference/awaitUsingDeclarations.16.js
Normal file
@@ -0,0 +1,14 @@
|
||||
//// [tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarations.16.ts] ////
|
||||
|
||||
//// [awaitUsingDeclarations.16.ts]
|
||||
declare namespace N {
|
||||
await using x: { [Symbol.asyncDispose](): Promise<void> };
|
||||
await using y: null;
|
||||
}
|
||||
declare module 'M' {
|
||||
await using x: { [Symbol.asyncDispose](): Promise<void> };
|
||||
await using y: null;
|
||||
}
|
||||
|
||||
|
||||
//// [awaitUsingDeclarations.16.js]
|
||||
24
tests/baselines/reference/usingDeclarations.16.errors.txt
Normal file
24
tests/baselines/reference/usingDeclarations.16.errors.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
usingDeclarations.16.ts(2,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
|
||||
usingDeclarations.16.ts(3,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
|
||||
usingDeclarations.16.ts(6,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
|
||||
usingDeclarations.16.ts(7,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
|
||||
|
||||
|
||||
==== usingDeclarations.16.ts (4 errors) ====
|
||||
declare namespace N {
|
||||
using x: { [Symbol.dispose](): void };
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
|
||||
using y: null;
|
||||
~~~~~~~~~~~~~
|
||||
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
|
||||
}
|
||||
declare module 'M' {
|
||||
using x: { [Symbol.dispose](): void };
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
|
||||
using y: null;
|
||||
~~~~~~~~~~~~~
|
||||
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
|
||||
}
|
||||
|
||||
14
tests/baselines/reference/usingDeclarations.16.js
Normal file
14
tests/baselines/reference/usingDeclarations.16.js
Normal file
@@ -0,0 +1,14 @@
|
||||
//// [tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarations.16.ts] ////
|
||||
|
||||
//// [usingDeclarations.16.ts]
|
||||
declare namespace N {
|
||||
using x: { [Symbol.dispose](): void };
|
||||
using y: null;
|
||||
}
|
||||
declare module 'M' {
|
||||
using x: { [Symbol.dispose](): void };
|
||||
using y: null;
|
||||
}
|
||||
|
||||
|
||||
//// [usingDeclarations.16.js]
|
||||
@@ -0,0 +1,13 @@
|
||||
// @target: esnext
|
||||
// @module: esnext
|
||||
// @lib: esnext
|
||||
// @noTypesAndSymbols: true
|
||||
|
||||
declare namespace N {
|
||||
await using x: { [Symbol.asyncDispose](): Promise<void> };
|
||||
await using y: null;
|
||||
}
|
||||
declare module 'M' {
|
||||
await using x: { [Symbol.asyncDispose](): Promise<void> };
|
||||
await using y: null;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// @target: esnext
|
||||
// @module: esnext
|
||||
// @lib: esnext
|
||||
// @noTypesAndSymbols: true
|
||||
|
||||
declare namespace N {
|
||||
using x: { [Symbol.dispose](): void };
|
||||
using y: null;
|
||||
}
|
||||
declare module 'M' {
|
||||
using x: { [Symbol.dispose](): void };
|
||||
using y: null;
|
||||
}
|
||||
Reference in New Issue
Block a user