Skip to content

Commit 7c44b44

Browse files
authored
Port 'Issue "'{0}' declarations can only be declared inside a block." for block-scoped variables in presence of parse errors' (#1816)
1 parent 37034f6 commit 7c44b44

File tree

3 files changed

+15
-59
lines changed

3 files changed

+15
-59
lines changed

internal/checker/grammarchecks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (c *Checker) grammarErrorAtPos(nodeForSourceFile *ast.Node, start int, leng
3737
func (c *Checker) grammarErrorOnNode(node *ast.Node, message *diagnostics.Message, args ...any) bool {
3838
sourceFile := ast.GetSourceFileOfNode(node)
3939
if !c.hasParseDiagnostics(sourceFile) {
40-
c.diagnostics.Add(NewDiagnosticForNode(node, message, args...))
40+
c.error(node, message, args...)
4141
return true
4242
}
4343
return false
@@ -1846,7 +1846,7 @@ func (c *Checker) checkGrammarForDisallowedBlockScopedVariableStatement(node *as
18461846
default:
18471847
panic("Unknown BlockScope flag")
18481848
}
1849-
return c.grammarErrorOnNode(node.AsNode(), diagnostics.X_0_declarations_can_only_be_declared_inside_a_block, keyword)
1849+
c.error(node.AsNode(), diagnostics.X_0_declarations_can_only_be_declared_inside_a_block, keyword)
18501850
}
18511851
}
18521852

testdata/baselines/reference/submodule/compiler/disallowedBlockScopedInPresenceOfParseErrors1.errors.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1+
disallowedBlockScopedInPresenceOfParseErrors1.ts(5,5): error TS1156: 'const' declarations can only be declared inside a block.
12
disallowedBlockScopedInPresenceOfParseErrors1.ts(6,17): error TS2454: Variable 'e' is used before being assigned.
23
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.
35
disallowedBlockScopedInPresenceOfParseErrors1.ts(13,17): error TS2454: Variable 'e' is used before being assigned.
46
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.
58
disallowedBlockScopedInPresenceOfParseErrors1.ts(22,17): error TS2454: Variable 'e' is used before being assigned.
69
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.
711
disallowedBlockScopedInPresenceOfParseErrors1.ts(31,17): error TS2454: Variable 'e' is used before being assigned.
812
disallowedBlockScopedInPresenceOfParseErrors1.ts(33,1): error TS1128: Declaration or statement expected.
913

1014

11-
==== disallowedBlockScopedInPresenceOfParseErrors1.ts (8 errors) ====
15+
==== disallowedBlockScopedInPresenceOfParseErrors1.ts (12 errors) ====
1216
// https://github.com/microsoft/TypeScript/issues/61734
1317

1418
function f1() {
1519
if (1 > 0)
1620
const e = 3;
21+
~~~~~~~~~~~~
22+
!!! error TS1156: 'const' declarations can only be declared inside a block.
1723
console.log(e);
1824
~
1925
!!! error TS2454: Variable 'e' is used before being assigned.
@@ -25,6 +31,8 @@ disallowedBlockScopedInPresenceOfParseErrors1.ts(33,1): error TS1128: Declaratio
2531
function f2() {
2632
if (1 > 0)
2733
let e = 3;
34+
~~~~~~~~~~
35+
!!! error TS1156: 'let' declarations can only be declared inside a block.
2836
console.log(e);
2937
~
3038
!!! error TS2454: Variable 'e' is used before being assigned.
@@ -38,6 +46,8 @@ disallowedBlockScopedInPresenceOfParseErrors1.ts(33,1): error TS1128: Declaratio
3846
function f3() {
3947
if (1 > 0)
4048
using e = resource;
49+
~~~~~~~~~~~~~~~~~~~
50+
!!! error TS1156: 'using' declarations can only be declared inside a block.
4151
console.log(e);
4252
~
4353
!!! error TS2454: Variable 'e' is used before being assigned.
@@ -51,6 +61,8 @@ disallowedBlockScopedInPresenceOfParseErrors1.ts(33,1): error TS1128: Declaratio
5161
async function f4() {
5262
if (1 > 0)
5363
await using e = asyncResource;
64+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65+
!!! error TS1156: 'await using' declarations can only be declared inside a block.
5466
console.log(e);
5567
~
5668
!!! error TS2454: Variable 'e' is used before being assigned.

testdata/baselines/reference/submodule/compiler/disallowedBlockScopedInPresenceOfParseErrors1.errors.txt.diff

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)