Skip to content

Commit fa2a0fc

Browse files
authored
Issue "'{0}' declarations can only be declared inside a block." for block-scoped variables in presence of parse errors (#61824)
1 parent 1e24945 commit fa2a0fc

5 files changed

+328
-2
lines changed

src/compiler/checker.ts

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

0 commit comments

Comments
 (0)