Skip to content

Commit daad9bc

Browse files
committed
Issue "'{0}' declarations can only be declared inside a block." for block-scoped variables in presence of parse errors
1 parent ac03ba4 commit daad9bc

5 files changed

+319
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52836,7 +52836,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
5283652836
blockScopeKind === NodeFlags.Using ? "using" :
5283752837
blockScopeKind === NodeFlags.AwaitUsing ? "await using" :
5283852838
Debug.fail("Unknown BlockScope flag");
52839-
return grammarErrorOnNode(node, Diagnostics._0_declarations_can_only_be_declared_inside_a_block, keyword);
52839+
diagnostics.add(createDiagnosticForNode(node, Diagnostics._0_declarations_can_only_be_declared_inside_a_block, keyword));
5284052840
}
5284152841
}
5284252842
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
disallowedBlockScopedInPresenceOfParseErrors1.ts(3,5): error TS1156: 'const' declarations can only be declared inside a block.
2+
disallowedBlockScopedInPresenceOfParseErrors1.ts(4,17): error TS2454: Variable 'e' is used before being assigned.
3+
disallowedBlockScopedInPresenceOfParseErrors1.ts(6,1): error TS1128: Declaration or statement expected.
4+
disallowedBlockScopedInPresenceOfParseErrors1.ts(10,5): error TS1156: 'let' declarations can only be declared inside a block.
5+
disallowedBlockScopedInPresenceOfParseErrors1.ts(11,17): error TS2454: Variable 'e' is used before being assigned.
6+
disallowedBlockScopedInPresenceOfParseErrors1.ts(13,1): error TS1128: Declaration or statement expected.
7+
disallowedBlockScopedInPresenceOfParseErrors1.ts(19,5): error TS1156: 'using' declarations can only be declared inside a block.
8+
disallowedBlockScopedInPresenceOfParseErrors1.ts(20,17): error TS2454: Variable 'e' is used before being assigned.
9+
disallowedBlockScopedInPresenceOfParseErrors1.ts(22,1): error TS1128: Declaration or statement expected.
10+
disallowedBlockScopedInPresenceOfParseErrors1.ts(28,5): error TS1156: 'await using' declarations can only be declared inside a block.
11+
disallowedBlockScopedInPresenceOfParseErrors1.ts(29,17): error TS2454: Variable 'e' is used before being assigned.
12+
disallowedBlockScopedInPresenceOfParseErrors1.ts(31,1): error TS1128: Declaration or statement expected.
13+
14+
15+
==== disallowedBlockScopedInPresenceOfParseErrors1.ts (12 errors) ====
16+
function f1() {
17+
if (1 > 0)
18+
const e = 3;
19+
~~~~~~~~~~~~
20+
!!! error TS1156: 'const' declarations can only be declared inside a block.
21+
console.log(e);
22+
~
23+
!!! error TS2454: Variable 'e' is used before being assigned.
24+
}
25+
}
26+
~
27+
!!! error TS1128: Declaration or statement expected.
28+
29+
function f2() {
30+
if (1 > 0)
31+
let e = 3;
32+
~~~~~~~~~~
33+
!!! error TS1156: 'let' declarations can only be declared inside a block.
34+
console.log(e);
35+
~
36+
!!! error TS2454: Variable 'e' is used before being assigned.
37+
}
38+
}
39+
~
40+
!!! error TS1128: Declaration or statement expected.
41+
42+
declare const resource: Disposable
43+
44+
function f3() {
45+
if (1 > 0)
46+
using e = resource;
47+
~~~~~~~~~~~~~~~~~~~
48+
!!! error TS1156: 'using' declarations can only be declared inside a block.
49+
console.log(e);
50+
~
51+
!!! error TS2454: Variable 'e' is used before being assigned.
52+
}
53+
}
54+
~
55+
!!! error TS1128: Declaration or statement expected.
56+
57+
declare const asyncResource: AsyncDisposable
58+
59+
async function f4() {
60+
if (1 > 0)
61+
await using e = resource;
62+
~~~~~~~~~~~~~~~~~~~~~~~~~
63+
!!! error TS1156: 'await using' declarations can only be declared inside a block.
64+
console.log(e);
65+
~
66+
!!! error TS2454: Variable 'e' is used before being assigned.
67+
}
68+
}
69+
~
70+
!!! error TS1128: Declaration or statement expected.
71+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//// [tests/cases/compiler/disallowedBlockScopedInPresenceOfParseErrors1.ts] ////
2+
3+
=== disallowedBlockScopedInPresenceOfParseErrors1.ts ===
4+
function f1() {
5+
>f1 : Symbol(f1, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 0, 0))
6+
7+
if (1 > 0)
8+
const e = 3;
9+
>e : Symbol(e, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 2, 9))
10+
11+
console.log(e);
12+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
13+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
14+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
15+
>e : Symbol(e, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 2, 9))
16+
}
17+
}
18+
19+
function f2() {
20+
>f2 : Symbol(f2, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 5, 1))
21+
22+
if (1 > 0)
23+
let e = 3;
24+
>e : Symbol(e, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 9, 7))
25+
26+
console.log(e);
27+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
28+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
29+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
30+
>e : Symbol(e, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 9, 7))
31+
}
32+
}
33+
34+
declare const resource: Disposable
35+
>resource : Symbol(resource, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 14, 13))
36+
>Disposable : Symbol(Disposable, Decl(lib.esnext.disposable.d.ts, --, --))
37+
38+
function f3() {
39+
>f3 : Symbol(f3, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 14, 34))
40+
41+
if (1 > 0)
42+
using e = resource;
43+
>e : Symbol(e, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 18, 9))
44+
>resource : Symbol(resource, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 14, 13))
45+
46+
console.log(e);
47+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
48+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
49+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
50+
>e : Symbol(e, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 18, 9))
51+
}
52+
}
53+
54+
declare const asyncResource: AsyncDisposable
55+
>asyncResource : Symbol(asyncResource, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 23, 13))
56+
>AsyncDisposable : Symbol(AsyncDisposable, Decl(lib.esnext.disposable.d.ts, --, --))
57+
58+
async function f4() {
59+
>f4 : Symbol(f4, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 23, 44))
60+
61+
if (1 > 0)
62+
await using e = resource;
63+
>e : Symbol(e, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 27, 15))
64+
>resource : Symbol(resource, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 14, 13))
65+
66+
console.log(e);
67+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
68+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
69+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
70+
>e : Symbol(e, Decl(disallowedBlockScopedInPresenceOfParseErrors1.ts, 27, 15))
71+
}
72+
}
73+
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
//// [tests/cases/compiler/disallowedBlockScopedInPresenceOfParseErrors1.ts] ////
2+
3+
=== disallowedBlockScopedInPresenceOfParseErrors1.ts ===
4+
function f1() {
5+
>f1 : () => void
6+
> : ^^^^^^^^^^
7+
8+
if (1 > 0)
9+
>1 > 0 : boolean
10+
> : ^^^^^^^
11+
>1 : 1
12+
> : ^
13+
>0 : 0
14+
> : ^
15+
16+
const e = 3;
17+
>e : 3
18+
> : ^
19+
>3 : 3
20+
> : ^
21+
22+
console.log(e);
23+
>console.log(e) : void
24+
> : ^^^^
25+
>console.log : (...data: any[]) => void
26+
> : ^^^^ ^^ ^^^^^
27+
>console : Console
28+
> : ^^^^^^^
29+
>log : (...data: any[]) => void
30+
> : ^^^^ ^^ ^^^^^
31+
>e : 3
32+
> : ^
33+
}
34+
}
35+
36+
function f2() {
37+
>f2 : () => void
38+
> : ^^^^^^^^^^
39+
40+
if (1 > 0)
41+
>1 > 0 : boolean
42+
> : ^^^^^^^
43+
>1 : 1
44+
> : ^
45+
>0 : 0
46+
> : ^
47+
48+
let e = 3;
49+
>e : number
50+
> : ^^^^^^
51+
>3 : 3
52+
> : ^
53+
54+
console.log(e);
55+
>console.log(e) : void
56+
> : ^^^^
57+
>console.log : (...data: any[]) => void
58+
> : ^^^^ ^^ ^^^^^
59+
>console : Console
60+
> : ^^^^^^^
61+
>log : (...data: any[]) => void
62+
> : ^^^^ ^^ ^^^^^
63+
>e : number
64+
> : ^^^^^^
65+
}
66+
}
67+
68+
declare const resource: Disposable
69+
>resource : Disposable
70+
> : ^^^^^^^^^^
71+
72+
function f3() {
73+
>f3 : () => void
74+
> : ^^^^^^^^^^
75+
76+
if (1 > 0)
77+
>1 > 0 : boolean
78+
> : ^^^^^^^
79+
>1 : 1
80+
> : ^
81+
>0 : 0
82+
> : ^
83+
84+
using e = resource;
85+
>e : Disposable
86+
> : ^^^^^^^^^^
87+
>resource : Disposable
88+
> : ^^^^^^^^^^
89+
90+
console.log(e);
91+
>console.log(e) : void
92+
> : ^^^^
93+
>console.log : (...data: any[]) => void
94+
> : ^^^^ ^^ ^^^^^
95+
>console : Console
96+
> : ^^^^^^^
97+
>log : (...data: any[]) => void
98+
> : ^^^^ ^^ ^^^^^
99+
>e : Disposable
100+
> : ^^^^^^^^^^
101+
}
102+
}
103+
104+
declare const asyncResource: AsyncDisposable
105+
>asyncResource : AsyncDisposable
106+
> : ^^^^^^^^^^^^^^^
107+
108+
async function f4() {
109+
>f4 : () => Promise<void>
110+
> : ^^^^^^^^^^^^^^^^^^^
111+
112+
if (1 > 0)
113+
>1 > 0 : boolean
114+
> : ^^^^^^^
115+
>1 : 1
116+
> : ^
117+
>0 : 0
118+
> : ^
119+
120+
await using e = resource;
121+
>e : Disposable
122+
> : ^^^^^^^^^^
123+
>resource : Disposable
124+
> : ^^^^^^^^^^
125+
126+
console.log(e);
127+
>console.log(e) : void
128+
> : ^^^^
129+
>console.log : (...data: any[]) => void
130+
> : ^^^^ ^^ ^^^^^
131+
>console : Console
132+
> : ^^^^^^^
133+
>log : (...data: any[]) => void
134+
> : ^^^^ ^^ ^^^^^
135+
>e : Disposable
136+
> : ^^^^^^^^^^
137+
}
138+
}
139+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// @strict: true
2+
// @target: esnext
3+
// @noEmit: true
4+
5+
function f1() {
6+
if (1 > 0)
7+
const e = 3;
8+
console.log(e);
9+
}
10+
}
11+
12+
function f2() {
13+
if (1 > 0)
14+
let e = 3;
15+
console.log(e);
16+
}
17+
}
18+
19+
declare const resource: Disposable
20+
21+
function f3() {
22+
if (1 > 0)
23+
using e = resource;
24+
console.log(e);
25+
}
26+
}
27+
28+
declare const asyncResource: AsyncDisposable
29+
30+
async function f4() {
31+
if (1 > 0)
32+
await using e = resource;
33+
console.log(e);
34+
}
35+
}

0 commit comments

Comments
 (0)