Skip to content

Commit 9950b6e

Browse files
authored
Improve error messages for computed class property names (#42675)
1 parent a2ed469 commit 9950b6e

25 files changed

+176
-176
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40782,7 +40782,7 @@ namespace ts {
4078240782
if (isStringLiteral(node.name) && node.name.text === "constructor") {
4078340783
return grammarErrorOnNode(node.name, Diagnostics.Classes_may_not_have_a_field_named_constructor);
4078440784
}
40785-
if (checkGrammarForInvalidDynamicName(node.name, Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type)) {
40785+
if (checkGrammarForInvalidDynamicName(node.name, Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_have_a_simple_literal_type_or_a_unique_symbol_type)) {
4078640786
return true;
4078740787
}
4078840788
if (languageVersion < ScriptTarget.ES2015 && isPrivateIdentifier(node.name)) {

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@
503503
"category": "Error",
504504
"code": 1165
505505
},
506-
"A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.": {
506+
"A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.": {
507507
"category": "Error",
508508
"code": 1166
509509
},

tests/baselines/reference/awaitAndYieldInProperty.errors.txt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(3,9): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
1+
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(3,9): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
22
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(3,21): error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
3-
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(4,16): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
3+
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(4,16): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
44
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(4,28): error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
5-
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(6,9): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
5+
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(6,9): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
66
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(6,21): error TS1163: A 'yield' expression is only allowed in a generator body.
7-
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(7,16): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
7+
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(7,16): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
88
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(7,28): error TS1163: A 'yield' expression is only allowed in a generator body.
9-
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(11,9): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
9+
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(11,9): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
1010
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(11,21): error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
11-
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(12,16): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
11+
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(12,16): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
1212
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(12,28): error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
13-
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(14,9): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
13+
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(14,9): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
1414
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(14,21): error TS1163: A 'yield' expression is only allowed in a generator body.
15-
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(15,16): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
15+
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(15,16): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
1616
tests/cases/conformance/classes/awaitAndYieldInProperty.ts(15,28): error TS1163: A 'yield' expression is only allowed in a generator body.
1717

1818

@@ -21,47 +21,47 @@ tests/cases/conformance/classes/awaitAndYieldInProperty.ts(15,28): error TS1163:
2121
class C {
2222
[await x] = await x;
2323
~~~~~~~~~
24-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
24+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
2525
~~~~~
2626
!!! error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
2727
static [await x] = await x;
2828
~~~~~~~~~
29-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
29+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
3030
~~~~~
3131
!!! error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
3232

3333
[yield 1] = yield 2;
3434
~~~~~~~~~
35-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
35+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
3636
~~~~~
3737
!!! error TS1163: A 'yield' expression is only allowed in a generator body.
3838
static [yield 3] = yield 4;
3939
~~~~~~~~~
40-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
40+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
4141
~~~~~
4242
!!! error TS1163: A 'yield' expression is only allowed in a generator body.
4343
}
4444

4545
return class {
4646
[await x] = await x;
4747
~~~~~~~~~
48-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
48+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
4949
~~~~~
5050
!!! error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
5151
static [await x] = await x;
5252
~~~~~~~~~
53-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
53+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
5454
~~~~~
5555
!!! error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
5656

5757
[yield 1] = yield 2;
5858
~~~~~~~~~
59-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
59+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
6060
~~~~~
6161
!!! error TS1163: A 'yield' expression is only allowed in a generator body.
6262
static [yield 3] = yield 4;
6363
~~~~~~~~~
64-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
64+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
6565
~~~~~
6666
!!! error TS1163: A 'yield' expression is only allowed in a generator body.
6767
}

tests/baselines/reference/capturedParametersInInitializers2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/compiler/capturedParametersInInitializers2.ts(3,20): error TS2373: Parameter 'y' cannot reference identifier 'x' declared after it.
22
tests/cases/compiler/capturedParametersInInitializers2.ts(4,14): error TS2373: Parameter 'y' cannot reference identifier 'x' declared after it.
33
tests/cases/compiler/capturedParametersInInitializers2.ts(6,10): error TS2373: Parameter 'y' cannot reference identifier 'z' declared after it.
4-
tests/cases/compiler/capturedParametersInInitializers2.ts(13,26): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
4+
tests/cases/compiler/capturedParametersInInitializers2.ts(13,26): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
55
tests/cases/compiler/capturedParametersInInitializers2.ts(13,27): error TS2373: Parameter 'y' cannot reference identifier 'x' declared after it.
66

77

@@ -26,7 +26,7 @@ tests/cases/compiler/capturedParametersInInitializers2.ts(13,27): error TS2373:
2626
}
2727
function foo2(y = class {[x] = x}, x = 1) {
2828
~~~
29-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
29+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
3030
~
3131
!!! error TS2373: Parameter 'y' cannot reference identifier 'x' declared after it.
3232
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(5,5): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
2-
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(6,5): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
3-
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(7,12): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
4-
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(8,5): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
5-
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(9,5): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
6-
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(12,5): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
7-
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(13,12): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
8-
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(15,12): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
1+
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(5,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
2+
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(6,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
3+
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(7,12): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
4+
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(8,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
5+
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(9,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
6+
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(12,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
7+
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(13,12): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
8+
tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(15,12): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
99

1010

1111
==== tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts (8 errors) ====
@@ -15,29 +15,29 @@ tests/cases/conformance/es6/computedProperties/computedPropertyNames12_ES5.ts(15
1515
class C {
1616
[s]: number;
1717
~~~
18-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
18+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
1919
[n] = n;
2020
~~~
21-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
21+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
2222
static [s + s]: string;
2323
~~~~~~~
24-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
24+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
2525
[s + n] = 2;
2626
~~~~~~~
27-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
27+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
2828
[+s]: typeof s;
2929
~~~~
30-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
30+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
3131
static [""]: number;
3232
[0]: number;
3333
[a]: number;
3434
~~~
35-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
35+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
3636
static [<any>true]: number;
3737
~~~~~~~~~~~
38-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
38+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
3939
[`hello bye`] = 0;
4040
static [`hello ${a} bye`] = 0
4141
~~~~~~~~~~~~~~~~~~
42-
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
42+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
4343
}

0 commit comments

Comments
 (0)