Skip to content

Commit

Permalink
extended to computed property
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Jun 19, 2016
1 parent 47ab99f commit 6d94928
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 12 deletions.
17 changes: 11 additions & 6 deletions src/compiler/checker.ts
Expand Up @@ -13349,12 +13349,10 @@ namespace ts {
}

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

return false;
Expand Down Expand Up @@ -18483,6 +18481,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
Expand Up @@ -2,13 +2,17 @@ tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(3,5): error TS2690: 'le
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(4,5): error TS2690: 'name' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(5,5): error TS2690: 'arguments' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(6,5): error TS2690: 'caller' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(10,12): error TS2690: 'length' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(11,12): error TS2690: 'name' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(12,12): error TS2690: 'arguments' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(13,12): error TS2690: 'caller' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(8,9): error TS2690: 'length' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(9,9): error TS2690: 'name' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(10,9): error TS2690: 'arguments' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(11,9): error TS2690: 'caller' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(16,12): error TS2690: 'length' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(17,12): error TS2690: 'name' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(18,12): error TS2690: 'arguments' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(19,12): error TS2690: 'caller' is not allowed to be used as a name of a static property or method in a class.


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

class static_property {
static length = 1;
Expand All @@ -23,6 +27,20 @@ tests/cases/compiler/checkGrammarStaticMemberName_es5.ts(13,12): error TS2690: '
static caller = 1;
~~~~~~~~~~~~~~~~~~
!!! error TS2690: 'caller' is not allowed to be used as a name of a static property or method in a class.
static foo = {
["length"]: 1,
~~~~~~~~~~
!!! error TS2690: 'length' is not allowed to be used as a name of a static property or method in a class.
["name"]: 1,
~~~~~~~~
!!! error TS2690: 'name' is not allowed to be used as a name of a static property or method in a class.
["arguments"]: 1,
~~~~~~~~~~~~~
!!! error TS2690: 'arguments' is not allowed to be used as a name of a static property or method in a class.
["caller"]: 1,
~~~~~~~~~~
!!! error TS2690: 'caller' is not allowed to be used as a name of a static property or method in a class.
};
}

class static_method {
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/checkGrammarStaticMemberName_es5.js
Expand Up @@ -5,6 +5,12 @@ class static_property {
static name = 1;
static arguments = 1;
static caller = 1;
static foo = {
["length"]: 1,
["name"]: 1,
["arguments"]: 1,
["caller"]: 1,
};
}

class static_method {
Expand All @@ -23,7 +29,15 @@ var static_property = (function () {
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() {
Expand Down
Expand Up @@ -2,9 +2,13 @@ tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(3,5): error TS2690: 'le
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(4,5): error TS2690: 'name' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(5,5): error TS2690: 'arguments' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(6,5): error TS2690: 'caller' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(8,9): error TS2690: 'length' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(9,9): error TS2690: 'name' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(10,9): error TS2690: 'arguments' is not allowed to be used as a name of a static property or method in a class.
tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(11,9): error TS2690: 'caller' is not allowed to be used as a name of a static property or method in a class.


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

class static_property {
static length = 1;
Expand All @@ -19,6 +23,20 @@ tests/cases/compiler/checkGrammarStaticMemberName_es6.ts(6,5): error TS2690: 'ca
static caller = 1;
~~~~~~~~~~~~~~~~~~
!!! error TS2690: 'caller' is not allowed to be used as a name of a static property or method in a class.
static foo = {
["length"]: 1,
~~~~~~~~~~
!!! error TS2690: 'length' is not allowed to be used as a name of a static property or method in a class.
["name"]: 1,
~~~~~~~~
!!! error TS2690: 'name' is not allowed to be used as a name of a static property or method in a class.
["arguments"]: 1,
~~~~~~~~~~~~~
!!! error TS2690: 'arguments' is not allowed to be used as a name of a static property or method in a class.
["caller"]: 1,
~~~~~~~~~~
!!! error TS2690: 'caller' is not allowed to be used as a name of a static property or method in a class.
};
}

class static_method {
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/checkGrammarStaticMemberName_es6.js
Expand Up @@ -5,6 +5,12 @@ class static_property {
static name = 1;
static arguments = 1;
static caller = 1;
static foo = {
["length"]: 1,
["name"]: 1,
["arguments"]: 1,
["caller"]: 1,
};
}

class static_method {
Expand All @@ -22,6 +28,12 @@ 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; }
Expand Down
6 changes: 6 additions & 0 deletions tests/cases/compiler/checkGrammarStaticMemberName_es5.ts
Expand Up @@ -5,6 +5,12 @@ class static_property {
static name = 1;
static arguments = 1;
static caller = 1;
static foo = {
["length"]: 1,
["name"]: 1,
["arguments"]: 1,
["caller"]: 1,
};
}

class static_method {
Expand Down
6 changes: 6 additions & 0 deletions tests/cases/compiler/checkGrammarStaticMemberName_es6.ts
Expand Up @@ -5,6 +5,12 @@ class static_property {
static name = 1;
static arguments = 1;
static caller = 1;
static foo = {
["length"]: 1,
["name"]: 1,
["arguments"]: 1,
["caller"]: 1,
};
}

class static_method {
Expand Down

0 comments on commit 6d94928

Please sign in to comment.