Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug - Certain static member names cannot be used due to non overridable member names on the constructor function type. #5396

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/compiler/checker.ts
Expand Up @@ -201,6 +201,13 @@ namespace ts {
}
};

const reservedStaticPropertyNames: { [key: string]: string } = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map<string>

length: "length",
arguments: "arguments",
caller: "caller",
name: "name"
};

const JsxNames = {
JSX: "JSX",
IntrinsicElements: "IntrinsicElements",
Expand Down Expand Up @@ -15082,6 +15089,9 @@ namespace ts {
break;

case SyntaxKind.StaticKeyword:
if (node.kind === SyntaxKind.Identifier && reservedStaticPropertyNames[node.symbol.name]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes static declarations with name "toString" for instance illegal, if you are using a map, use ts.hasProperty to check if the property exist on the object it self and not its prototype chain.

return grammarErrorOnNode(node, Diagnostics._0_cannot_be_used_as_a_static_member_identifier, node.symbol.name);
}
if (flags & NodeFlags.Static) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "static");
}
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -1724,6 +1724,10 @@
"category": "Error",
"code": 2657
},
"'{0}' cannot be used as a static member identifier.": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a lit bit more information here; preferably something that will point that classes constructors are inherently functions. something like:

Duplicate identifier 'name'. Class constructor functions implicitly have a property with the same name

"category":"Error",
"code": 2658
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
"code": 4000
Expand Down
42 changes: 42 additions & 0 deletions tests/baselines/reference/reservedNameWithStaticProperty.js
@@ -0,0 +1,42 @@
//// [reservedNameWithStaticProperty.ts]
class Length {
static length: string = "pizza";
}

class Caller {
static caller: string = "potatos";
}

class Arguments {
static arguments: string = "tomatoes"
}

class Name {
static name: string = "name"
}

//// [reservedNameWithStaticProperty.js]
var Length = (function () {
function Length() {
}
Length.length = "pizza";
return Length;
})();
var Caller = (function () {
function Caller() {
}
Caller.caller = "potatos";
return Caller;
})();
var Arguments = (function () {
function Arguments() {
}
Arguments.arguments = "tomatoes";
return Arguments;
})();
var Name = (function () {
function Name() {
}
Name.name = "name";
return Name;
})();
28 changes: 28 additions & 0 deletions tests/baselines/reference/reservedNameWithStaticProperty.symbols
@@ -0,0 +1,28 @@
=== tests/cases/compiler/reservedNameWithStaticProperty.ts ===
class Length {
>Length : Symbol(Length, Decl(reservedNameWithStaticProperty.ts, 0, 0))

static length: string = "pizza";
>length : Symbol(Length.length, Decl(reservedNameWithStaticProperty.ts, 0, 14))
}

class Caller {
>Caller : Symbol(Caller, Decl(reservedNameWithStaticProperty.ts, 2, 1))

static caller: string = "potatos";
>caller : Symbol(Caller.caller, Decl(reservedNameWithStaticProperty.ts, 4, 14))
}

class Arguments {
>Arguments : Symbol(Arguments, Decl(reservedNameWithStaticProperty.ts, 6, 1))

static arguments: string = "tomatoes"
>arguments : Symbol(Arguments.arguments, Decl(reservedNameWithStaticProperty.ts, 8, 17))
}

class Name {
>Name : Symbol(Name, Decl(reservedNameWithStaticProperty.ts, 10, 1))

static name: string = "name"
>name : Symbol(Name.name, Decl(reservedNameWithStaticProperty.ts, 12, 12))
}
32 changes: 32 additions & 0 deletions tests/baselines/reference/reservedNameWithStaticProperty.types
@@ -0,0 +1,32 @@
=== tests/cases/compiler/reservedNameWithStaticProperty.ts ===
class Length {
>Length : Length

static length: string = "pizza";
>length : string
>"pizza" : string
}

class Caller {
>Caller : Caller

static caller: string = "potatos";
>caller : string
>"potatos" : string
}

class Arguments {
>Arguments : Arguments

static arguments: string = "tomatoes"
>arguments : string
>"tomatoes" : string
}

class Name {
>Name : Name

static name: string = "name"
>name : string
>"name" : string
}
@@ -1,43 +1,43 @@
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(12,1): error TS2322: Type 'C' is not assignable to type 'A'.
Property 'name' is missing in type 'C'.
Property 'beep' is missing in type 'C'.
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(13,1): error TS2322: Type 'typeof B' is not assignable to type 'A'.
Property 'name' is missing in type 'typeof B'.
Property 'beep' is missing in type 'typeof B'.
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(16,5): error TS2322: Type 'C' is not assignable to type 'B'.
Property 'name' is missing in type 'C'.
Property 'beep' is missing in type 'C'.
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(17,1): error TS2322: Type 'typeof B' is not assignable to type 'B'.
Property 'name' is missing in type 'typeof B'.
Property 'beep' is missing in type 'typeof B'.


==== tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts (4 errors) ====
interface A {
name();
beep();
}
class B {
public name() { }
public beep() { }
}
class C {
public static name() { }
public static beep() { }
}

var a: A = new B();
a = new C(); // error name is missing
a = new C(); // error beep is missing
~
!!! error TS2322: Type 'C' is not assignable to type 'A'.
!!! error TS2322: Property 'name' is missing in type 'C'.
a = B; // error name is missing
!!! error TS2322: Property 'beep' is missing in type 'C'.
a = B; // error beep is missing
~
!!! error TS2322: Type 'typeof B' is not assignable to type 'A'.
!!! error TS2322: Property 'name' is missing in type 'typeof B'.
!!! error TS2322: Property 'beep' is missing in type 'typeof B'.
a = C;

var b: B = new C(); // error name is missing
~
!!! error TS2322: Type 'C' is not assignable to type 'B'.
!!! error TS2322: Property 'name' is missing in type 'C'.
b = B; // error name is missing
!!! error TS2322: Property 'beep' is missing in type 'C'.
b = B; // error beep is missing
~
!!! error TS2322: Type 'typeof B' is not assignable to type 'B'.
!!! error TS2322: Property 'name' is missing in type 'typeof B'.
!!! error TS2322: Property 'beep' is missing in type 'typeof B'.
b = C;
b = a;

Expand Down
@@ -1,21 +1,21 @@
//// [staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts]
interface A {
name();
beep();
}
class B {
public name() { }
public beep() { }
}
class C {
public static name() { }
public static beep() { }
}

var a: A = new B();
a = new C(); // error name is missing
a = B; // error name is missing
a = new C(); // error beep is missing
a = B; // error beep is missing
a = C;

var b: B = new C(); // error name is missing
b = B; // error name is missing
b = B; // error beep is missing
b = C;
b = a;

Expand All @@ -29,21 +29,21 @@ c = a;
var B = (function () {
function B() {
}
B.prototype.name = function () { };
B.prototype.beep = function () { };
return B;
})();
var C = (function () {
function C() {
}
C.name = function () { };
C.beep = function () { };
return C;
})();
var a = new B();
a = new C(); // error name is missing
a = B; // error name is missing
a = new C(); // error beep is missing
a = B; // error beep is missing
a = C;
var b = new C(); // error name is missing
b = B; // error name is missing
b = B; // error beep is missing
b = C;
b = a;
var c = new B();
Expand Down
15 changes: 15 additions & 0 deletions tests/cases/compiler/reservedNameWithStaticProperty.ts
@@ -0,0 +1,15 @@
class Length {
static length: string = "pizza";
}

class Caller {
static caller: string = "potatos";
}

class Arguments {
static arguments: string = "tomatoes"
}

class Name {
static name: string = "name"
}
@@ -1,20 +1,20 @@
interface A {
name();
beep();
}
class B {
public name() { }
public beep() { }
}
class C {
public static name() { }
public static beep() { }
}

var a: A = new B();
a = new C(); // error name is missing
a = B; // error name is missing
a = new C(); // error beep is missing
a = B; // error beep is missing
a = C;

var b: B = new C(); // error name is missing
b = B; // error name is missing
b = B; // error beep is missing
b = C;
b = a;

Expand Down