Skip to content

Commit

Permalink
merge commits
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Sep 14, 2016
1 parent e9178a5 commit 2315694
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 3 deletions.
40 changes: 38 additions & 2 deletions src/compiler/checker.ts
Expand Up @@ -14217,14 +14217,14 @@ namespace ts {

function checkPropertyDeclaration(node: PropertyDeclaration) {
// Grammar checking
checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarProperty(node) || checkGrammarComputedPropertyName(node.name);
checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarProperty(node) || checkGrammarComputedPropertyName(node.name) || checkGrammarStaticPropertyOrMethodName(node);

checkVariableLikeDeclaration(node);
}

function checkMethodDeclaration(node: MethodDeclaration) {
// Grammar checking
checkGrammarMethod(node) || checkGrammarComputedPropertyName(node.name);
checkGrammarMethod(node) || checkGrammarComputedPropertyName(node.name) || checkGrammarStaticPropertyOrMethodName(node);

// Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration
checkFunctionOrMethodDeclaration(node);
Expand All @@ -14236,6 +14236,35 @@ namespace ts {
}
}

function checkGrammarStaticPropertyOrMethodName(node: ClassElement) {
if (node.flags & NodeFlags.Static) {
if (node.kind === SyntaxKind.PropertyDeclaration) {
return checkGrammarStaticMemberName(node, ((node as ClassElement).name as Identifier).text);
}
else if (node.kind === SyntaxKind.MethodDeclaration) {
if (languageVersion < ScriptTarget.ES6) {
return checkGrammarStaticMemberName(node, ((node as ClassElement).name as Identifier).text);
}
}
}

return false;
}

function checkGrammarStaticMemberName(node: Node, name: string) {
// see: https://github.com/Microsoft/TypeScript/issues/442
const forbiddenNames = ["length", "name", "arguments", "caller"];
if (forbiddenNames.indexOf(name) !== -1) {
if (languageVersion < ScriptTarget.ES6) {
return grammarErrorOnNode(node, Diagnostics._0_is_not_allowed_to_be_used_as_a_name_of_a_static_property_or_method_in_a_class_for_target_es5_2696, name);
} else {
return grammarErrorOnNode(node, Diagnostics._0_is_not_allowed_to_be_used_as_a_name_of_a_static_property_in_a_class_for_target_es6_and_higher_2697, name);
}
}

return false;
}

function checkConstructorDeclaration(node: ConstructorDeclaration) {
// Grammar check on signature of constructor and modifier of the constructor is done in checkSignatureDeclaration function.
checkSignatureDeclaration(node);
Expand Down Expand Up @@ -19714,6 +19743,13 @@ namespace ts {
if (computedPropertyName.expression.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>computedPropertyName.expression).operatorToken.kind === SyntaxKind.CommaToken) {
return grammarErrorOnNode(computedPropertyName.expression, Diagnostics.A_comma_expression_is_not_allowed_in_a_computed_property_name);
}

if (computedPropertyName.expression.kind === SyntaxKind.StringLiteral) {
const member = computedPropertyName.parent.parent.parent;
if (member.flags & NodeFlags.Static && member.kind === SyntaxKind.PropertyDeclaration) {
return checkGrammarStaticMemberName(computedPropertyName, (computedPropertyName.expression as StringLiteral).text);
}
}
}

function checkGrammarForGenerator(node: FunctionLikeDeclaration) {
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -1959,6 +1959,14 @@
"category": "Error",
"code": 2695
},
"'{0}' is not allowed to be used as a name of a static property or method in a class for target ES5.": {
"category": "Error",
"code": 2696
},
"'{0}' is not allowed to be used as a name of a static property in a class for target ES6 and higher.": {
"category": "Error",
"code": 2697
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
@@ -0,0 +1,60 @@
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(3,5): error TS2696: 'length' is not allowed to be used as a name of a static property or method in a class for target ES5.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(4,5): error TS2696: 'name' is not allowed to be used as a name of a static property or method in a class for target ES5.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(5,5): error TS2696: 'arguments' is not allowed to be used as a name of a static property or method in a class for target ES5.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(6,5): error TS2696: 'caller' is not allowed to be used as a name of a static property or method in a class for target ES5.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(8,9): error TS2696: 'length' is not allowed to be used as a name of a static property or method in a class for target ES5.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(9,9): error TS2696: 'name' is not allowed to be used as a name of a static property or method in a class for target ES5.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(10,9): error TS2696: 'arguments' is not allowed to be used as a name of a static property or method in a class for target ES5.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(11,9): error TS2696: 'caller' is not allowed to be used as a name of a static property or method in a class for target ES5.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(16,12): error TS2696: 'length' is not allowed to be used as a name of a static property or method in a class for target ES5.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(17,12): error TS2696: 'name' is not allowed to be used as a name of a static property or method in a class for target ES5.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(18,12): error TS2696: 'arguments' is not allowed to be used as a name of a static property or method in a class for target ES5.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(19,12): error TS2696: 'caller' is not allowed to be used as a name of a static property or method in a class for target ES5.


==== tests/cases/compiler/checkGrammarStaticMemberName_es5.ts (12 errors) ====

class static_property {
static length = 1;
~~~~~~~~~~~~~~~~~~
!!! error TS2696: 'length' is not allowed to be used as a name of a static property or method in a class for target ES5.
static name = 1;
~~~~~~~~~~~~~~~~
!!! error TS2696: 'name' is not allowed to be used as a name of a static property or method in a class for target ES5.
static arguments = 1;
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2696: 'arguments' is not allowed to be used as a name of a static property or method in a class for target ES5.
static caller = 1;
~~~~~~~~~~~~~~~~~~
!!! error TS2696: 'caller' is not allowed to be used as a name of a static property or method in a class for target ES5.
static foo = {
["length"]: 1,
~~~~~~~~~~
!!! error TS2696: 'length' is not allowed to be used as a name of a static property or method in a class for target ES5.
["name"]: 1,
~~~~~~~~
!!! error TS2696: 'name' is not allowed to be used as a name of a static property or method in a class for target ES5.
["arguments"]: 1,
~~~~~~~~~~~~~
!!! error TS2696: 'arguments' is not allowed to be used as a name of a static property or method in a class for target ES5.
["caller"]: 1,
~~~~~~~~~~
!!! error TS2696: 'caller' is not allowed to be used as a name of a static property or method in a class for target ES5.
};
}

class static_method {
static length() { return 1; }
~~~~~~
!!! error TS2696: 'length' is not allowed to be used as a name of a static property or method in a class for target ES5.
static name() { return 1; }
~~~~
!!! error TS2696: 'name' is not allowed to be used as a name of a static property or method in a class for target ES5.
static arguments() { return 1; }
~~~~~~~~~
!!! error TS2696: 'arguments' is not allowed to be used as a name of a static property or method in a class for target ES5.
static caller() { return 1; }
~~~~~~
!!! error TS2696: 'caller' is not allowed to be used as a name of a static property or method in a class for target ES5.
}

50 changes: 50 additions & 0 deletions tests/baselines/reference/checkGrammarStaticMemberName_es5.js
@@ -0,0 +1,50 @@
//// [checkGrammarStaticMemberName_es5.ts]

class static_property {
static length = 1;
static name = 1;
static arguments = 1;
static caller = 1;
static foo = {
["length"]: 1,
["name"]: 1,
["arguments"]: 1,
["caller"]: 1,
};
}

class static_method {
static length() { return 1; }
static name() { return 1; }
static arguments() { return 1; }
static caller() { return 1; }
}


//// [checkGrammarStaticMemberName_es5.js]
var static_property = (function () {
function static_property() {
}
static_property.length = 1;
static_property.name = 1;
static_property.arguments = 1;
static_property.caller = 1;
static_property.foo = (_a = {},
_a["length"] = 1,
_a["name"] = 1,
_a["arguments"] = 1,
_a["caller"] = 1,
_a
);
return static_property;
var _a;
}());
var static_method = (function () {
function static_method() {
}
static_method.length = function () { return 1; };
static_method.name = function () { return 1; };
static_method.arguments = function () { return 1; };
static_method.caller = function () { return 1; };
return static_method;
}());
@@ -0,0 +1,48 @@
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(3,5): error TS2697: 'length' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(4,5): error TS2697: 'name' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(5,5): error TS2697: 'arguments' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(6,5): error TS2697: 'caller' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(8,9): error TS2697: 'length' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(9,9): error TS2697: 'name' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(10,9): error TS2697: 'arguments' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(11,9): error TS2697: 'caller' is not allowed to be used as a name of a static property in a class for target ES6 and higher.


==== tests/cases/compiler/checkGrammarStaticMemberName_es6.ts (8 errors) ====

class static_property {
static length = 1;
~~~~~~~~~~~~~~~~~~
!!! error TS2697: 'length' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
static name = 1;
~~~~~~~~~~~~~~~~
!!! error TS2697: 'name' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
static arguments = 1;
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2697: 'arguments' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
static caller = 1;
~~~~~~~~~~~~~~~~~~
!!! error TS2697: 'caller' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
static foo = {
["length"]: 1,
~~~~~~~~~~
!!! error TS2697: 'length' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
["name"]: 1,
~~~~~~~~
!!! error TS2697: 'name' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
["arguments"]: 1,
~~~~~~~~~~~~~
!!! error TS2697: 'arguments' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
["caller"]: 1,
~~~~~~~~~~
!!! error TS2697: 'caller' is not allowed to be used as a name of a static property in a class for target ES6 and higher.
};
}

class static_method {
static length() { return 1; }
static name() { return 1; }
static arguments() { return 1; }
static caller() { return 1; }
}

42 changes: 42 additions & 0 deletions tests/baselines/reference/checkGrammarStaticMemberName_es6.js
@@ -0,0 +1,42 @@
//// [checkGrammarStaticMemberName_es6.ts]

class static_property {
static length = 1;
static name = 1;
static arguments = 1;
static caller = 1;
static foo = {
["length"]: 1,
["name"]: 1,
["arguments"]: 1,
["caller"]: 1,
};
}

class static_method {
static length() { return 1; }
static name() { return 1; }
static arguments() { return 1; }
static caller() { return 1; }
}


//// [checkGrammarStaticMemberName_es6.js]
class static_property {
}
static_property.length = 1;
static_property.name = 1;
static_property.arguments = 1;
static_property.caller = 1;
static_property.foo = {
["length"]: 1,
["name"]: 1,
["arguments"]: 1,
["caller"]: 1,
};
class static_method {
static length() { return 1; }
static name() { return 1; }
static arguments() { return 1; }
static caller() { return 1; }
}
@@ -1,3 +1,4 @@
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(8,19): error TS2696: 'name' is not allowed to be used as a name of a static property or method in a class for target ES5.
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 TS2696: 'name' is not allowed to be used as a name of a static property or method in a class for target ES5.
}

var a: A = new B();
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/compiler/checkGrammarStaticMemberName_es5.ts
@@ -0,0 +1,21 @@
// @target: es5

class static_property {
static length = 1;
static name = 1;
static arguments = 1;
static caller = 1;
static foo = {
["length"]: 1,
["name"]: 1,
["arguments"]: 1,
["caller"]: 1,
};
}

class static_method {
static length() { return 1; }
static name() { return 1; }
static arguments() { return 1; }
static caller() { return 1; }
}
21 changes: 21 additions & 0 deletions tests/cases/compiler/checkGrammarStaticMemberName_es6.ts
@@ -0,0 +1,21 @@
// @target: es6

class static_property {
static length = 1;
static name = 1;
static arguments = 1;
static caller = 1;
static foo = {
["length"]: 1,
["name"]: 1,
["arguments"]: 1,
["caller"]: 1,
};
}

class static_method {
static length() { return 1; }
static name() { return 1; }
static arguments() { return 1; }
static caller() { return 1; }
}

0 comments on commit 2315694

Please sign in to comment.