Skip to content

Commit b0f56dd

Browse files
committedApr 3, 2022
fix(22682): Fail on class declaration used in statement position for ES2015 or higher
1 parent fd4943b commit b0f56dd

7 files changed

+74
-1
lines changed
 

‎src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -43614,6 +43614,9 @@ namespace ts {
4361443614
}
4361543615

4361643616
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+
}
4361743620
const file = getSourceFileOfNode(node);
4361843621
return checkGrammarClassDeclarationHeritageClauses(node) ||
4361943622
checkGrammarTypeParameterList(node.typeParameters, file);

‎src/compiler/diagnosticMessages.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,10 @@
14691469
"category": "Message",
14701470
"code": 1476
14711471
},
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+
},
14731476
"The types of '{0}' are incompatible between these types.": {
14741477
"category": "Error",
14751478
"code": 2200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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

Comments
 (0)
Failed to load comments.