From 6d9492867640e6e87321787e737ac9153a6ed9c4 Mon Sep 17 00:00:00 2001 From: york yao Date: Sun, 19 Jun 2016 08:04:27 +0800 Subject: [PATCH] extended to computed property --- src/compiler/checker.ts | 17 +++++++---- ...heckGrammarStaticMemberName_es5.errors.txt | 28 +++++++++++++++---- .../checkGrammarStaticMemberName_es5.js | 14 ++++++++++ ...heckGrammarStaticMemberName_es6.errors.txt | 20 ++++++++++++- .../checkGrammarStaticMemberName_es6.js | 12 ++++++++ .../checkGrammarStaticMemberName_es5.ts | 6 ++++ .../checkGrammarStaticMemberName_es6.ts | 6 ++++ 7 files changed, 91 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8d3a8e84f5fde..9d3b28ca21dcf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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; @@ -18483,6 +18481,13 @@ namespace ts { if (computedPropertyName.expression.kind === SyntaxKind.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) { diff --git a/tests/baselines/reference/checkGrammarStaticMemberName_es5.errors.txt b/tests/baselines/reference/checkGrammarStaticMemberName_es5.errors.txt index b520b096c88f0..870af8039a108 100644 --- a/tests/baselines/reference/checkGrammarStaticMemberName_es5.errors.txt +++ b/tests/baselines/reference/checkGrammarStaticMemberName_es5.errors.txt @@ -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; @@ -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 { diff --git a/tests/baselines/reference/checkGrammarStaticMemberName_es5.js b/tests/baselines/reference/checkGrammarStaticMemberName_es5.js index 370d5a7bae550..5735f94995869 100644 --- a/tests/baselines/reference/checkGrammarStaticMemberName_es5.js +++ b/tests/baselines/reference/checkGrammarStaticMemberName_es5.js @@ -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 { @@ -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() { diff --git a/tests/baselines/reference/checkGrammarStaticMemberName_es6.errors.txt b/tests/baselines/reference/checkGrammarStaticMemberName_es6.errors.txt index 63317d1f147f4..9d4c3972dbf3b 100644 --- a/tests/baselines/reference/checkGrammarStaticMemberName_es6.errors.txt +++ b/tests/baselines/reference/checkGrammarStaticMemberName_es6.errors.txt @@ -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; @@ -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 { diff --git a/tests/baselines/reference/checkGrammarStaticMemberName_es6.js b/tests/baselines/reference/checkGrammarStaticMemberName_es6.js index a13709cfe7daf..5dc0854c8b4e7 100644 --- a/tests/baselines/reference/checkGrammarStaticMemberName_es6.js +++ b/tests/baselines/reference/checkGrammarStaticMemberName_es6.js @@ -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 { @@ -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; } diff --git a/tests/cases/compiler/checkGrammarStaticMemberName_es5.ts b/tests/cases/compiler/checkGrammarStaticMemberName_es5.ts index 53e02edfd895e..4103154b4d904 100644 --- a/tests/cases/compiler/checkGrammarStaticMemberName_es5.ts +++ b/tests/cases/compiler/checkGrammarStaticMemberName_es5.ts @@ -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 { diff --git a/tests/cases/compiler/checkGrammarStaticMemberName_es6.ts b/tests/cases/compiler/checkGrammarStaticMemberName_es6.ts index c3052096b5fea..0bb98ecd945b7 100644 --- a/tests/cases/compiler/checkGrammarStaticMemberName_es6.ts +++ b/tests/cases/compiler/checkGrammarStaticMemberName_es6.ts @@ -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 {