Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ namespace ts {
}
};

const reservedStaticPropertyNames: { [key: string]: string } = {

Copy link
Copy Markdown
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
Copy Markdown
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
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,10 @@
"category": "Error",
"code": 2657
},
"'{0}' cannot be used as a static member identifier.": {

Copy link
Copy Markdown
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -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