You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(parser): reject using/await using in switch case/default clauses
Fixes#62708
The ECMAScript spec was updated to disallow using and await using
declarations directly in switch case/default clauses. This makes
resource declaration count statically knowable.
Added parser validation that checks parsing context using bitfields
to distinguish direct nesting (error) from nested blocks (valid).
Block-wrapped declarations remain allowed.
Test coverage:
- usingDeclarations.1.ts: 3 errors caught
- awaitUsingDeclarations.1.ts: 3 errors caught
- New comprehensive tests: error cases + edge cases
- Full suite: 2087 tests passing
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.
2
-
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.
3
-
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.
4
-
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.
5
-
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.
6
-
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.
7
-
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.
8
-
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.
9
-
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.
10
-
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.
11
-
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.
12
-
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.
13
-
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.
14
-
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.
15
-
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.
1
+
awaitUsingDeclarations.1.ts(41,9): error TS95198: A 'await using' declaration cannot be placed within a 'case' or 'default' clause.
2
+
awaitUsingDeclarations.1.ts(45,9): error TS95198: A 'await using' declaration cannot be placed within a 'case' or 'default' clause.
3
+
awaitUsingDeclarations.1.ts(52,13): error TS95198: A 'await using' declaration cannot be placed within a 'case' or 'default' clause.
16
4
17
5
18
-
==== awaitUsingDeclarations.1.ts (15 errors) ====
6
+
==== awaitUsingDeclarations.1.ts (3 errors) ====
19
7
await using d1 = { async [Symbol.asyncDispose]() {} };
20
-
~~~~~
21
-
!!! 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.
22
8
23
9
async function af() {
24
10
await using d3 = { async [Symbol.asyncDispose]() {} };
await using d19 = { async [Symbol.asyncDispose]() {} };
57
-
~~~~~
58
-
!!! 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.
59
43
}
60
44
61
45
switch (Math.random()) {
62
46
case 0:
63
47
await using d20 = { async [Symbol.asyncDispose]() {} };
64
48
~~~~~
65
-
!!! 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.
49
+
!!! error TS95198: A 'await using' declaration cannot be placed within a 'case' or 'default' clause.
66
50
break;
67
51
68
52
case 1:
69
53
await using d21 = { async [Symbol.asyncDispose]() {} };
70
54
~~~~~
71
-
!!! 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.
55
+
!!! error TS95198: A 'await using' declaration cannot be placed within a 'case' or 'default' clause.
await using d22 = { async [Symbol.asyncDispose]() {} };
79
63
~~~~~
80
-
!!! 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.
64
+
!!! error TS95198: A 'await using' declaration cannot be placed within a 'case' or 'default' clause.
81
65
break;
82
66
}
83
67
84
68
try {
85
69
await using d23 = { async [Symbol.asyncDispose]() {} };
86
-
~~~~~
87
-
!!! 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.
88
70
}
89
71
catch {
90
72
await using d24 = { async [Symbol.asyncDispose]() {} };
91
-
~~~~~
92
-
!!! 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.
93
73
}
94
74
finally {
95
75
await using d25 = { async [Symbol.asyncDispose]() {} };
96
-
~~~~~
97
-
!!! 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.
98
76
}
99
77
100
78
if (true) {
101
79
await using d26 = { async [Symbol.asyncDispose]() {} };
102
-
~~~~~
103
-
!!! 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.
104
80
}
105
81
else {
106
82
await using d27 = { async [Symbol.asyncDispose]() {} };
107
-
~~~~~
108
-
!!! 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.
109
83
}
110
84
111
85
while (true) {
112
86
await using d28 = { async [Symbol.asyncDispose]() {} };
113
-
~~~~~
114
-
!!! 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.
115
87
break;
116
88
}
117
89
118
90
do {
119
91
await using d29 = { async [Symbol.asyncDispose]() {} };
120
-
~~~~~
121
-
!!! 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.
122
92
break;
123
93
}
124
94
while (true);
125
95
126
96
for (;;) {
127
97
await using d30 = { async [Symbol.asyncDispose]() {} };
128
-
~~~~~
129
-
!!! 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.
130
98
break;
131
99
}
132
100
133
101
for (const x in {}) {
134
102
await using d31 = { async [Symbol.asyncDispose]() {} };
135
-
~~~~~
136
-
!!! 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.
137
103
}
138
104
139
105
for (const x of []) {
140
106
await using d32 = { async [Symbol.asyncDispose]() {} };
141
-
~~~~~
142
-
!!! 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.
0 commit comments