Skip to content

Commit e81fa21

Browse files
committed
Emit error on class fields named "constructor"
1 parent 9f601ff commit e81fa21

File tree

7 files changed

+85
-0
lines changed

7 files changed

+85
-0
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31789,6 +31789,9 @@ namespace ts {
3178931789

3179031790
function checkGrammarProperty(node: PropertyDeclaration | PropertySignature) {
3179131791
if (isClassLike(node.parent)) {
31792+
if (isStringLiteral(node.name) && node.name.text === "constructor") {
31793+
return grammarErrorOnNode(node.name, Diagnostics.Classes_may_not_have_a_field_named_constructor);
31794+
}
3179231795
if (checkGrammarForInvalidDynamicName(node.name, Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type)) {
3179331796
return true;
3179431797
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4958,5 +4958,9 @@
49584958
"Quoted constructors have previously been interpreted as methods, which is incorrect. In TypeScript 3.6, they will be correctly parsed as constructors. In the meantime, consider using 'constructor()' to write a constructor, or '[\"constructor\"]()' to write a method.": {
49594959
"category": "Error",
49604960
"code": 18005
4961+
},
4962+
"Classes may not have a field named 'constructor'.": {
4963+
"category": "Error",
4964+
"code": 18006
49614965
}
49624966
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/conformance/classes/propertyMemberDeclarations/propertyNamedConstructor.ts(2,3): error TS18006: Classes may not have a field named 'constructor'.
2+
3+
4+
==== tests/cases/conformance/classes/propertyMemberDeclarations/propertyNamedConstructor.ts (1 errors) ====
5+
class X1 {
6+
"constructor" = 3; // Error
7+
~~~~~~~~~~~~~
8+
!!! error TS18006: Classes may not have a field named 'constructor'.
9+
}
10+
11+
class X2 {
12+
["constructor"] = 3;
13+
}
14+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [propertyNamedConstructor.ts]
2+
class X1 {
3+
"constructor" = 3; // Error
4+
}
5+
6+
class X2 {
7+
["constructor"] = 3;
8+
}
9+
10+
11+
//// [propertyNamedConstructor.js]
12+
var X1 = /** @class */ (function () {
13+
function X1() {
14+
this["constructor"] = 3; // Error
15+
}
16+
return X1;
17+
}());
18+
var X2 = /** @class */ (function () {
19+
function X2() {
20+
this["constructor"] = 3;
21+
}
22+
return X2;
23+
}());
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/conformance/classes/propertyMemberDeclarations/propertyNamedConstructor.ts ===
2+
class X1 {
3+
>X1 : Symbol(X1, Decl(propertyNamedConstructor.ts, 0, 0))
4+
5+
"constructor" = 3; // Error
6+
>"constructor" : Symbol(X1["constructor"], Decl(propertyNamedConstructor.ts, 0, 10))
7+
}
8+
9+
class X2 {
10+
>X2 : Symbol(X2, Decl(propertyNamedConstructor.ts, 2, 1))
11+
12+
["constructor"] = 3;
13+
>["constructor"] : Symbol(X2["constructor"], Decl(propertyNamedConstructor.ts, 4, 10))
14+
>"constructor" : Symbol(X2["constructor"], Decl(propertyNamedConstructor.ts, 4, 10))
15+
}
16+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/conformance/classes/propertyMemberDeclarations/propertyNamedConstructor.ts ===
2+
class X1 {
3+
>X1 : X1
4+
5+
"constructor" = 3; // Error
6+
>"constructor" : number
7+
>3 : 3
8+
}
9+
10+
class X2 {
11+
>X2 : X2
12+
13+
["constructor"] = 3;
14+
>["constructor"] : number
15+
>"constructor" : "constructor"
16+
>3 : 3
17+
}
18+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class X1 {
2+
"constructor" = 3; // Error
3+
}
4+
5+
class X2 {
6+
["constructor"] = 3;
7+
}

0 commit comments

Comments
 (0)