Skip to content

Commit aa10919

Browse files
author
王文璐
committed
allow string concat in enum member declaration
1 parent d0721e4 commit aa10919

9 files changed

+320
-22
lines changed

src/compiler/checker.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5001,9 +5001,6 @@ namespace ts {
50015001
for (const declaration of symbol.declarations) {
50025002
if (declaration.kind === SyntaxKind.EnumDeclaration) {
50035003
for (const member of (<EnumDeclaration>declaration).members) {
5004-
if (member.initializer && member.initializer.kind === SyntaxKind.StringLiteral) {
5005-
return links.enumKind = EnumKind.Literal;
5006-
}
50075004
if (!isLiteralEnumMember(member)) {
50085005
hasNonLiteralMember = true;
50095006
}
@@ -22962,6 +22959,9 @@ namespace ts {
2296222959
case SyntaxKind.AsteriskAsteriskToken: return left ** right;
2296322960
}
2296422961
}
22962+
else if (typeof left === "string" && typeof right === "string" && (<BinaryExpression>expr).operatorToken.kind === SyntaxKind.PlusToken) {
22963+
return left + right;
22964+
}
2296522965
break;
2296622966
case SyntaxKind.StringLiteral:
2296722967
return (<StringLiteral>expr).text;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
tests/cases/conformance/enums/enumConstantMemberWithString.ts(4,9): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
2+
tests/cases/conformance/enums/enumConstantMemberWithString.ts(4,15): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
3+
tests/cases/conformance/enums/enumConstantMemberWithString.ts(5,9): error TS2322: Type 'string' is not assignable to type 'T1'.
4+
5+
6+
==== tests/cases/conformance/enums/enumConstantMemberWithString.ts (3 errors) ====
7+
enum T1 {
8+
a = "1",
9+
b = "1" + "2",
10+
c = "a" - "a",
11+
~~~
12+
!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
13+
~~~
14+
!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
15+
d = "a" + 1
16+
~~~~~~~
17+
!!! error TS2322: Type 'string' is not assignable to type 'T1'.
18+
}
19+
20+
enum T2 {
21+
a = "1",
22+
b = "1" + "2"
23+
}
24+
25+
enum T3 {
26+
a = "1",
27+
b = "1" + "2",
28+
c = 1,
29+
d = 1 + 2
30+
}
31+
32+
enum T4 {
33+
a = "1"
34+
}
35+
36+
enum T5 {
37+
a = "1" + "2"
38+
}
39+
40+
declare enum T6 {
41+
a = "1",
42+
b = "1" + "2"
43+
}
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//// [enumConstantMemberWithString.ts]
2+
enum T1 {
3+
a = "1",
4+
b = "1" + "2",
5+
c = "a" - "a",
6+
d = "a" + 1
7+
}
8+
9+
enum T2 {
10+
a = "1",
11+
b = "1" + "2"
12+
}
13+
14+
enum T3 {
15+
a = "1",
16+
b = "1" + "2",
17+
c = 1,
18+
d = 1 + 2
19+
}
20+
21+
enum T4 {
22+
a = "1"
23+
}
24+
25+
enum T5 {
26+
a = "1" + "2"
27+
}
28+
29+
declare enum T6 {
30+
a = "1",
31+
b = "1" + "2"
32+
}
33+
34+
35+
//// [enumConstantMemberWithString.js]
36+
var T1;
37+
(function (T1) {
38+
T1["a"] = "1";
39+
T1["b"] = "12";
40+
T1[T1["c"] = "a" - "a"] = "c";
41+
T1[T1["d"] = "a" + 1] = "d";
42+
})(T1 || (T1 = {}));
43+
var T2;
44+
(function (T2) {
45+
T2["a"] = "1";
46+
T2["b"] = "12";
47+
})(T2 || (T2 = {}));
48+
var T3;
49+
(function (T3) {
50+
T3["a"] = "1";
51+
T3["b"] = "12";
52+
T3[T3["c"] = 1] = "c";
53+
T3[T3["d"] = 3] = "d";
54+
})(T3 || (T3 = {}));
55+
var T4;
56+
(function (T4) {
57+
T4["a"] = "1";
58+
})(T4 || (T4 = {}));
59+
var T5;
60+
(function (T5) {
61+
T5["a"] = "12";
62+
})(T5 || (T5 = {}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
=== tests/cases/conformance/enums/enumConstantMemberWithString.ts ===
2+
enum T1 {
3+
>T1 : Symbol(T1, Decl(enumConstantMemberWithString.ts, 0, 0))
4+
5+
a = "1",
6+
>a : Symbol(T1.a, Decl(enumConstantMemberWithString.ts, 0, 9))
7+
8+
b = "1" + "2",
9+
>b : Symbol(T1.b, Decl(enumConstantMemberWithString.ts, 1, 12))
10+
11+
c = "a" - "a",
12+
>c : Symbol(T1.c, Decl(enumConstantMemberWithString.ts, 2, 18))
13+
14+
d = "a" + 1
15+
>d : Symbol(T1.d, Decl(enumConstantMemberWithString.ts, 3, 18))
16+
}
17+
18+
enum T2 {
19+
>T2 : Symbol(T2, Decl(enumConstantMemberWithString.ts, 5, 1))
20+
21+
a = "1",
22+
>a : Symbol(T2.a, Decl(enumConstantMemberWithString.ts, 7, 9))
23+
24+
b = "1" + "2"
25+
>b : Symbol(T2.b, Decl(enumConstantMemberWithString.ts, 8, 12))
26+
}
27+
28+
enum T3 {
29+
>T3 : Symbol(T3, Decl(enumConstantMemberWithString.ts, 10, 1))
30+
31+
a = "1",
32+
>a : Symbol(T3.a, Decl(enumConstantMemberWithString.ts, 12, 9))
33+
34+
b = "1" + "2",
35+
>b : Symbol(T3.b, Decl(enumConstantMemberWithString.ts, 13, 12))
36+
37+
c = 1,
38+
>c : Symbol(T3.c, Decl(enumConstantMemberWithString.ts, 14, 18))
39+
40+
d = 1 + 2
41+
>d : Symbol(T3.d, Decl(enumConstantMemberWithString.ts, 15, 10))
42+
}
43+
44+
enum T4 {
45+
>T4 : Symbol(T4, Decl(enumConstantMemberWithString.ts, 17, 1))
46+
47+
a = "1"
48+
>a : Symbol(T4.a, Decl(enumConstantMemberWithString.ts, 19, 9))
49+
}
50+
51+
enum T5 {
52+
>T5 : Symbol(T5, Decl(enumConstantMemberWithString.ts, 21, 1))
53+
54+
a = "1" + "2"
55+
>a : Symbol(T5.a, Decl(enumConstantMemberWithString.ts, 23, 9))
56+
}
57+
58+
declare enum T6 {
59+
>T6 : Symbol(T6, Decl(enumConstantMemberWithString.ts, 25, 1))
60+
61+
a = "1",
62+
>a : Symbol(T6.a, Decl(enumConstantMemberWithString.ts, 27, 17))
63+
64+
b = "1" + "2"
65+
>b : Symbol(T6.b, Decl(enumConstantMemberWithString.ts, 28, 12))
66+
}
67+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
=== tests/cases/conformance/enums/enumConstantMemberWithString.ts ===
2+
enum T1 {
3+
>T1 : T1
4+
5+
a = "1",
6+
>a : T1
7+
>"1" : "1"
8+
9+
b = "1" + "2",
10+
>b : T1
11+
>"1" + "2" : string
12+
>"1" : "1"
13+
>"2" : "2"
14+
15+
c = "a" - "a",
16+
>c : T1
17+
>"a" - "a" : number
18+
>"a" : "a"
19+
>"a" : "a"
20+
21+
d = "a" + 1
22+
>d : T1
23+
>"a" + 1 : string
24+
>"a" : "a"
25+
>1 : 1
26+
}
27+
28+
enum T2 {
29+
>T2 : T2
30+
31+
a = "1",
32+
>a : T2
33+
>"1" : "1"
34+
35+
b = "1" + "2"
36+
>b : T2
37+
>"1" + "2" : string
38+
>"1" : "1"
39+
>"2" : "2"
40+
}
41+
42+
enum T3 {
43+
>T3 : T3
44+
45+
a = "1",
46+
>a : T3
47+
>"1" : "1"
48+
49+
b = "1" + "2",
50+
>b : T3
51+
>"1" + "2" : string
52+
>"1" : "1"
53+
>"2" : "2"
54+
55+
c = 1,
56+
>c : T3
57+
>1 : 1
58+
59+
d = 1 + 2
60+
>d : T3
61+
>1 + 2 : number
62+
>1 : 1
63+
>2 : 2
64+
}
65+
66+
enum T4 {
67+
>T4 : T4
68+
69+
a = "1"
70+
>a : T4
71+
>"1" : "1"
72+
}
73+
74+
enum T5 {
75+
>T5 : T5
76+
77+
a = "1" + "2"
78+
>a : T5
79+
>"1" + "2" : string
80+
>"1" : "1"
81+
>"2" : "2"
82+
}
83+
84+
declare enum T6 {
85+
>T6 : T6
86+
87+
a = "1",
88+
>a : T6
89+
>"1" : "1"
90+
91+
b = "1" + "2"
92+
>b : T6
93+
>"1" + "2" : string
94+
>"1" : "1"
95+
>"2" : "2"
96+
}
97+

tests/baselines/reference/enumErrors.errors.txt

+7-10
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ tests/cases/conformance/enums/enumErrors.ts(26,9): error TS2322: Type 'true' is
77
tests/cases/conformance/enums/enumErrors.ts(27,9): error TS2322: Type 'Date' is not assignable to type 'E11'.
88
tests/cases/conformance/enums/enumErrors.ts(28,9): error TS2304: Cannot find name 'window'.
99
tests/cases/conformance/enums/enumErrors.ts(29,9): error TS2322: Type '{}' is not assignable to type 'E11'.
10-
tests/cases/conformance/enums/enumErrors.ts(35,9): error TS2553: Computed values are not permitted in an enum with string valued members.
11-
tests/cases/conformance/enums/enumErrors.ts(36,9): error TS2553: Computed values are not permitted in an enum with string valued members.
12-
tests/cases/conformance/enums/enumErrors.ts(37,9): error TS2553: Computed values are not permitted in an enum with string valued members.
13-
tests/cases/conformance/enums/enumErrors.ts(38,9): error TS2553: Computed values are not permitted in an enum with string valued members.
10+
tests/cases/conformance/enums/enumErrors.ts(35,9): error TS2322: Type 'Date' is not assignable to type 'E12'.
11+
tests/cases/conformance/enums/enumErrors.ts(36,9): error TS2304: Cannot find name 'window'.
12+
tests/cases/conformance/enums/enumErrors.ts(37,9): error TS2322: Type '{}' is not assignable to type 'E12'.
1413

1514

16-
==== tests/cases/conformance/enums/enumErrors.ts (13 errors) ====
15+
==== tests/cases/conformance/enums/enumErrors.ts (12 errors) ====
1716
// Enum named with PredefinedTypes
1817
enum any { }
1918
~~~
@@ -68,15 +67,13 @@ tests/cases/conformance/enums/enumErrors.ts(38,9): error TS2553: Computed values
6867
A = '',
6968
B = new Date(),
7069
~~~~~~~~~~
71-
!!! error TS2553: Computed values are not permitted in an enum with string valued members.
70+
!!! error TS2322: Type 'Date' is not assignable to type 'E12'.
7271
C = window,
7372
~~~~~~
74-
!!! error TS2553: Computed values are not permitted in an enum with string valued members.
73+
!!! error TS2304: Cannot find name 'window'.
7574
D = {},
7675
~~
77-
!!! error TS2553: Computed values are not permitted in an enum with string valued members.
76+
!!! error TS2322: Type '{}' is not assignable to type 'E12'.
7877
E = 1 + 1,
79-
~~~~~
80-
!!! error TS2553: Computed values are not permitted in an enum with string valued members.
8178
}
8279

tests/baselines/reference/enumErrors.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ var E11;
8383
var E12;
8484
(function (E12) {
8585
E12["A"] = "";
86-
E12[E12["B"] = 0] = "B";
87-
E12[E12["C"] = 0] = "C";
88-
E12[E12["D"] = 0] = "D";
89-
E12[E12["E"] = 0] = "E";
86+
E12[E12["B"] = new Date()] = "B";
87+
E12[E12["C"] = window] = "C";
88+
E12[E12["D"] = {}] = "D";
89+
E12[E12["E"] = 2] = "E";
9090
})(E12 || (E12 = {}));

tests/baselines/reference/enumErrors.types

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,24 @@ enum E12 {
7979
>E12 : E12
8080

8181
A = '',
82-
>A : E12.A
82+
>A : E12
8383
>'' : ""
8484

8585
B = new Date(),
86-
>B : E12.B
86+
>B : E12
8787
>new Date() : Date
8888
>Date : DateConstructor
8989

9090
C = window,
91-
>C : E12.B
91+
>C : E12
9292
>window : any
9393

9494
D = {},
95-
>D : E12.B
95+
>D : E12
9696
>{} : {}
9797

9898
E = 1 + 1,
99-
>E : E12.B
99+
>E : E12
100100
>1 + 1 : number
101101
>1 : 1
102102
>1 : 1

0 commit comments

Comments
 (0)