Skip to content

Commit 9b963fd

Browse files
authored
Port "Disallow type and interface declarations in statements with blockless bodies" (#1037)
1 parent df4e903 commit 9b963fd

File tree

5 files changed

+43
-64
lines changed

5 files changed

+43
-64
lines changed

internal/checker/checker.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4704,6 +4704,9 @@ func (c *Checker) checkInterfaceDeclaration(node *ast.Node) {
47044704
if !c.checkGrammarModifiers(node) {
47054705
c.checkGrammarInterfaceDeclaration(node.AsInterfaceDeclaration())
47064706
}
4707+
if !c.containerAllowsBlockScopedVariable(node.Parent) {
4708+
c.grammarErrorOnNode(node, diagnostics.X_0_declarations_can_only_be_declared_inside_a_block, "interface")
4709+
}
47074710
c.checkTypeParameters(node.TypeParameters())
47084711
c.checkTypeNameIsReserved(node.Name(), diagnostics.Interface_name_cannot_be_0)
47094712
c.checkExportsOnMergedDeclarations(node)
@@ -6425,6 +6428,9 @@ func (c *Checker) checkTypeAliasDeclaration(node *ast.Node) {
64256428
// Grammar checking
64266429
c.checkGrammarModifiers(node)
64276430
c.checkTypeNameIsReserved(node.Name(), diagnostics.Type_alias_name_cannot_be_0)
6431+
if !c.containerAllowsBlockScopedVariable(node.Parent) {
6432+
c.grammarErrorOnNode(node, diagnostics.X_0_declarations_can_only_be_declared_inside_a_block, "type")
6433+
}
64286434
c.checkExportsOnMergedDeclarations(node)
64296435

64306436
typeNode := node.AsTypeAliasDeclaration().Type
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
typeAliasDeclarationEmit3.ts(3,14): error TS1156: 'type' declarations can only be declared inside a block.
2+
typeAliasDeclarationEmit3.ts(9,14): error TS1156: 'type' declarations can only be declared inside a block.
3+
typeAliasDeclarationEmit3.ts(15,14): error TS1156: 'type' declarations can only be declared inside a block.
4+
5+
6+
==== typeAliasDeclarationEmit3.ts (3 errors) ====
7+
function f1(): void {
8+
for (let i = 0; i < 1; i++)
9+
type foo = [];
10+
~~~
11+
!!! error TS1156: 'type' declarations can only be declared inside a block.
12+
console.log('f1');
13+
}
14+
15+
function f2(): void {
16+
while (true)
17+
type foo = [];
18+
~~~
19+
!!! error TS1156: 'type' declarations can only be declared inside a block.
20+
console.log('f2');
21+
}
22+
23+
function f3(): void {
24+
if (true)
25+
type foo = [];
26+
~~~
27+
!!! error TS1156: 'type' declarations can only be declared inside a block.
28+
console.log('f3');
29+
}
30+

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

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

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
typeInterfaceDeclarationsInBlockStatements1.ts(4,18): error TS1156: 'type' declarations can only be declared inside a block.
12
typeInterfaceDeclarationsInBlockStatements1.ts(12,21): error TS2304: Cannot find name 's'.
3+
typeInterfaceDeclarationsInBlockStatements1.ts(17,15): error TS1156: 'interface' declarations can only be declared inside a block.
24
typeInterfaceDeclarationsInBlockStatements1.ts(29,21): error TS2304: Cannot find name 's'.
35

46

5-
==== typeInterfaceDeclarationsInBlockStatements1.ts (2 errors) ====
7+
==== typeInterfaceDeclarationsInBlockStatements1.ts (4 errors) ====
68
// https://github.com/microsoft/TypeScript/issues/60175
79

810
function f1() {
911
if (true) type s = string;
12+
~
13+
!!! error TS1156: 'type' declarations can only be declared inside a block.
1014
console.log("" as s);
1115
}
1216

@@ -22,6 +26,8 @@ typeInterfaceDeclarationsInBlockStatements1.ts(29,21): error TS2304: Cannot find
2226
function f3() {
2327
if (true)
2428
interface s {
29+
~
30+
!!! error TS1156: 'interface' declarations can only be declared inside a block.
2531
length: number;
2632
}
2733
console.log("" as s);

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

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

0 commit comments

Comments
 (0)