Skip to content

Commit

Permalink
Emit error on class fields named "constructor"
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Apr 26, 2019
1 parent 9f601ff commit e81fa21
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Expand Up @@ -31789,6 +31789,9 @@ namespace ts {

function checkGrammarProperty(node: PropertyDeclaration | PropertySignature) {
if (isClassLike(node.parent)) {
if (isStringLiteral(node.name) && node.name.text === "constructor") {
return grammarErrorOnNode(node.name, Diagnostics.Classes_may_not_have_a_field_named_constructor);
}
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)) {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -4958,5 +4958,9 @@
"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.": {
"category": "Error",
"code": 18005
},
"Classes may not have a field named 'constructor'.": {
"category": "Error",
"code": 18006
}
}
14 changes: 14 additions & 0 deletions tests/baselines/reference/propertyNamedConstructor.errors.txt
@@ -0,0 +1,14 @@
tests/cases/conformance/classes/propertyMemberDeclarations/propertyNamedConstructor.ts(2,3): error TS18006: Classes may not have a field named 'constructor'.


==== tests/cases/conformance/classes/propertyMemberDeclarations/propertyNamedConstructor.ts (1 errors) ====
class X1 {
"constructor" = 3; // Error
~~~~~~~~~~~~~
!!! error TS18006: Classes may not have a field named 'constructor'.
}

class X2 {
["constructor"] = 3;
}

23 changes: 23 additions & 0 deletions tests/baselines/reference/propertyNamedConstructor.js
@@ -0,0 +1,23 @@
//// [propertyNamedConstructor.ts]
class X1 {
"constructor" = 3; // Error
}

class X2 {
["constructor"] = 3;
}


//// [propertyNamedConstructor.js]
var X1 = /** @class */ (function () {
function X1() {
this["constructor"] = 3; // Error
}
return X1;
}());
var X2 = /** @class */ (function () {
function X2() {
this["constructor"] = 3;
}
return X2;
}());
16 changes: 16 additions & 0 deletions tests/baselines/reference/propertyNamedConstructor.symbols
@@ -0,0 +1,16 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/propertyNamedConstructor.ts ===
class X1 {
>X1 : Symbol(X1, Decl(propertyNamedConstructor.ts, 0, 0))

"constructor" = 3; // Error
>"constructor" : Symbol(X1["constructor"], Decl(propertyNamedConstructor.ts, 0, 10))
}

class X2 {
>X2 : Symbol(X2, Decl(propertyNamedConstructor.ts, 2, 1))

["constructor"] = 3;
>["constructor"] : Symbol(X2["constructor"], Decl(propertyNamedConstructor.ts, 4, 10))
>"constructor" : Symbol(X2["constructor"], Decl(propertyNamedConstructor.ts, 4, 10))
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/propertyNamedConstructor.types
@@ -0,0 +1,18 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/propertyNamedConstructor.ts ===
class X1 {
>X1 : X1

"constructor" = 3; // Error
>"constructor" : number
>3 : 3
}

class X2 {
>X2 : X2

["constructor"] = 3;
>["constructor"] : number
>"constructor" : "constructor"
>3 : 3
}

@@ -0,0 +1,7 @@
class X1 {
"constructor" = 3; // Error
}

class X2 {
["constructor"] = 3;
}

0 comments on commit e81fa21

Please sign in to comment.