7 files changed +74
-1
lines changed Original file line number Diff line number Diff line change @@ -43614,6 +43614,9 @@ namespace ts {
43614
43614
}
43615
43615
43616
43616
function checkGrammarClassLikeDeclaration(node: ClassLikeDeclaration): boolean {
43617
+ if (node.parent && isIfStatement(node.parent)) {
43618
+ grammarErrorOnNode(node, Diagnostics.A_class_declaration_can_only_be_used_in_a_scope_where_other_statements_can_reference_it);
43619
+ }
43617
43620
const file = getSourceFileOfNode(node);
43618
43621
return checkGrammarClassDeclarationHeritageClauses(node) ||
43619
43622
checkGrammarTypeParameterList(node.typeParameters, file);
Original file line number Diff line number Diff line change 1469
1469
"category" : " Message" ,
1470
1470
"code" : 1476
1471
1471
},
1472
-
1472
+ "A class declaration can only be used in a scope where other statements can reference it." : {
1473
+ "category" : " Error" ,
1474
+ "code" : 1477
1475
+ },
1473
1476
"The types of '{0}' are incompatible between these types." : {
1474
1477
"category" : " Error" ,
1475
1478
"code" : 2200
Original file line number Diff line number Diff line change
1
+ tests/cases/compiler/scopeClassDeclarations.ts(4,11): error TS1477: A class declaration can only be used in a scope where other statements can reference it.
2
+
3
+
4
+ ==== tests/cases/compiler/scopeClassDeclarations.ts (1 errors) ====
5
+ let condition = false as boolean;
6
+
7
+ if (condition)
8
+ class C {} // runtime error if transpiled to ES2015 or above: Unexpected token class
9
+ ~
10
+ !!! error TS1477: A class declaration can only be used in a scope where other statements can reference it.
11
+
12
+ new C(); // runtime error if transpiled to ES5, because 'C' is undefined
13
+
Original file line number Diff line number Diff line change
1
+ //// [scopeClassDeclarations.ts]
2
+ let condition = false as boolean ;
3
+
4
+ if ( condition )
5
+ class C { } // runtime error if transpiled to ES2015 or above: Unexpected token class
6
+
7
+ new C ( ) ; // runtime error if transpiled to ES5, because 'C' is undefined
8
+
9
+
10
+ //// [scopeClassDeclarations.js]
11
+ var condition = false ;
12
+ if ( condition ) {
13
+ var C = /** @class */ ( function ( ) {
14
+ function C ( ) {
15
+ }
16
+ return C ;
17
+ } ( ) ) ;
18
+ } // runtime error if transpiled to ES2015 or above: Unexpected token class
19
+ new C ( ) ; // runtime error if transpiled to ES5, because 'C' is undefined
Original file line number Diff line number Diff line change
1
+ === tests/cases/compiler/scopeClassDeclarations.ts ===
2
+ let condition = false as boolean;
3
+ >condition : Symbol(condition, Decl(scopeClassDeclarations.ts, 0, 3))
4
+
5
+ if (condition)
6
+ >condition : Symbol(condition, Decl(scopeClassDeclarations.ts, 0, 3))
7
+
8
+ class C {} // runtime error if transpiled to ES2015 or above: Unexpected token class
9
+ >C : Symbol(C, Decl(scopeClassDeclarations.ts, 2, 14))
10
+
11
+ new C(); // runtime error if transpiled to ES5, because 'C' is undefined
12
+ >C : Symbol(C, Decl(scopeClassDeclarations.ts, 2, 14))
13
+
Original file line number Diff line number Diff line change
1
+ === tests/cases/compiler/scopeClassDeclarations.ts ===
2
+ let condition = false as boolean;
3
+ >condition : boolean
4
+ >false as boolean : boolean
5
+ >false : false
6
+
7
+ if (condition)
8
+ >condition : boolean
9
+
10
+ class C {} // runtime error if transpiled to ES2015 or above: Unexpected token class
11
+ >C : C
12
+
13
+ new C(); // runtime error if transpiled to ES5, because 'C' is undefined
14
+ >new C() : C
15
+ >C : typeof C
16
+
Original file line number Diff line number Diff line change
1
+ let condition = false as boolean ;
2
+
3
+ if ( condition )
4
+ class C { } // runtime error if transpiled to ES2015 or above: Unexpected token class
5
+
6
+ new C ( ) ; // runtime error if transpiled to ES5, because 'C' is undefined
0 commit comments