mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 20:14:01 -06:00
Disallow refering to static property in computed property name
This commit is contained in:
parent
7ee587c43f
commit
56839604da
@ -5696,6 +5696,29 @@ module ts {
|
||||
if (!links.resolvedType) {
|
||||
links.resolvedType = checkExpression(node.expression);
|
||||
|
||||
// Disallow using static property in computedPropertyName because classDeclaration is binded lexically in ES6
|
||||
// and its static property assignment will be emitted after classDeclaration.
|
||||
// Therefore, using static property inside computedPropertyName will cause use-before-definition
|
||||
// Example:
|
||||
// * TypeScript
|
||||
// class C {
|
||||
// static p = 10;
|
||||
// [C.p]() {}
|
||||
// }
|
||||
// * JavaScript
|
||||
// class C {
|
||||
// [C.p]() {} // Use before definition error
|
||||
// }
|
||||
// C.p = 10;
|
||||
if (languageVersion >= ScriptTarget.ES6 && links.resolvedSymbol) {
|
||||
var declarations = links.resolvedSymbol.declarations;
|
||||
forEach(declarations, (declaration) => {
|
||||
if (declaration.flags & NodeFlags.Static) {
|
||||
error(node, Diagnostics.A_computed_property_name_cannot_reference_a_static_property);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// This will allow types number, string, symbol or any. It will also allow enums, the unknown
|
||||
// type, and any union of these types (like string | number).
|
||||
if (!allConstituentTypesHaveKind(links.resolvedType, TypeFlags.Any | TypeFlags.NumberLike | TypeFlags.StringLike | TypeFlags.ESSymbol)) {
|
||||
|
||||
@ -157,6 +157,7 @@ module ts {
|
||||
Catch_clause_variable_cannot_have_an_initializer: { code: 1197, category: DiagnosticCategory.Error, key: "Catch clause variable cannot have an initializer." },
|
||||
An_extended_Unicode_escape_value_must_be_between_0x0_and_0x10FFFF_inclusive: { code: 1198, category: DiagnosticCategory.Error, key: "An extended Unicode escape value must be between 0x0 and 0x10FFFF inclusive." },
|
||||
Unterminated_Unicode_escape_sequence: { code: 1199, category: DiagnosticCategory.Error, key: "Unterminated Unicode escape sequence." },
|
||||
A_computed_property_name_cannot_reference_a_static_property: { code: 1200, category: DiagnosticCategory.Error, key: "A computed property name cannot reference a static property" },
|
||||
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
|
||||
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
|
||||
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },
|
||||
|
||||
@ -619,6 +619,10 @@
|
||||
"category": "Error",
|
||||
"code": 1199
|
||||
},
|
||||
"A computed property name cannot reference a static property": {
|
||||
"category": "Error",
|
||||
"code": 1200
|
||||
},
|
||||
"Duplicate identifier '{0}'.": {
|
||||
"category": "Error",
|
||||
"code": 2300
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts(3,9): error TS1200: A computed property name cannot reference a static property
|
||||
tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts(6,9): error TS1200: A computed property name cannot reference a static property
|
||||
tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts(9,5): error TS1200: A computed property name cannot reference a static property
|
||||
|
||||
|
||||
==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts (3 errors) ====
|
||||
class C {
|
||||
static staticProp = 10;
|
||||
get [C.staticProp]() {
|
||||
~~~~~~~~~~~~~~
|
||||
!!! error TS1200: A computed property name cannot reference a static property
|
||||
return "hello";
|
||||
}
|
||||
set [C.staticProp](x: string) {
|
||||
~~~~~~~~~~~~~~
|
||||
!!! error TS1200: A computed property name cannot reference a static property
|
||||
var y = x;
|
||||
}
|
||||
[C.staticProp]() { }
|
||||
~~~~~~~~~~~~~~
|
||||
!!! error TS1200: A computed property name cannot reference a static property
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
//// [computedPropertyNamesWithStaticProperty.ts]
|
||||
class C {
|
||||
static staticProp = 10;
|
||||
get [C.staticProp]() {
|
||||
return "hello";
|
||||
}
|
||||
set [C.staticProp](x: string) {
|
||||
var y = x;
|
||||
}
|
||||
[C.staticProp]() { }
|
||||
}
|
||||
|
||||
//// [computedPropertyNamesWithStaticProperty.js]
|
||||
class C {
|
||||
constructor() {
|
||||
}
|
||||
get [C.staticProp]() {
|
||||
return "hello";
|
||||
}
|
||||
set [C.staticProp](x) {
|
||||
var y = x;
|
||||
}
|
||||
[C.staticProp]() {
|
||||
}
|
||||
}
|
||||
C.staticProp = 10;
|
||||
@ -0,0 +1,11 @@
|
||||
// @target: es6
|
||||
class C {
|
||||
static staticProp = 10;
|
||||
get [C.staticProp]() {
|
||||
return "hello";
|
||||
}
|
||||
set [C.staticProp](x: string) {
|
||||
var y = x;
|
||||
}
|
||||
[C.staticProp]() { }
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user