Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Sep 14, 2016
1 parent e9178a5 commit 0a0e68d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/compiler/checker.ts
Expand Up @@ -14220,6 +14220,8 @@ namespace ts {
checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarProperty(node) || checkGrammarComputedPropertyName(node.name);

checkVariableLikeDeclaration(node);

checkNameOfStaticMethodOrPropertyInClass(node);
}

function checkMethodDeclaration(node: MethodDeclaration) {
Expand All @@ -14234,6 +14236,21 @@ namespace ts {
if (getModifierFlags(node) & ModifierFlags.Abstract && node.body) {
error(node, Diagnostics.Method_0_cannot_have_an_implementation_because_it_is_marked_abstract, declarationNameToString(node.name));
}

checkNameOfStaticMethodOrPropertyInClass(node);
}

function checkNameOfStaticMethodOrPropertyInClass(node: ClassElement) {
if (node.flags & NodeFlags.Static) {
const name = (node.name as Identifier).text;
switch (name) {
case "length":
case "name":
case "arguments":
case "caller":
error(node, Diagnostics._0_is_not_allowed_to_be_used_as_a_name_of_a_static_property_or_method_in_a_class, name);
}
}
}

function checkConstructorDeclaration(node: ConstructorDeclaration) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -1959,6 +1959,10 @@
"category": "Error",
"code": 2695
},
"'{0}' is not allowed to be used as a name of a static property or method in a class.": {
"category": "Error",
"code": 2696
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
@@ -1,3 +1,4 @@
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(8,19): error TS2689: 'name' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(12,1): error TS2322: Type 'C' is not assignable to type 'A'.
Property 'name' is missing in type 'C'.
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(13,1): error TS2322: Type 'typeof B' is not assignable to type 'A'.
Expand All @@ -8,7 +9,7 @@ tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.
Property 'name' is missing in type 'typeof B'.


==== tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts (4 errors) ====
==== tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts (5 errors) ====
interface A {
name();
}
Expand All @@ -17,6 +18,8 @@ tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.
}
class C {
public static name() { }
~~~~
!!! error TS2689: 'name' is not allowed to be used as a name of a static property or method in a class.
}

var a: A = new B();
Expand Down

0 comments on commit 0a0e68d

Please sign in to comment.