Skip to content

Commit f9cc374

Browse files
author
Orta
authored
Merge pull request #33336 from JoshuaKGoldberg/enum-syntax-error-specifics
Added more helpful syntax error for enum member commas
2 parents e8fc62e + 4e1768c commit f9cc374

10 files changed

+175
-12
lines changed

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,10 @@
10351035
"category": "Error",
10361036
"code": 1356
10371037
},
1038+
"An enum member name must be followed by a ',', '=', or '}'.": {
1039+
"category": "Error",
1040+
"code": 1357
1041+
},
10381042

10391043
"Duplicate identifier '{0}'.": {
10401044
"category": "Error",

src/compiler/parser.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2121,7 +2121,7 @@ namespace ts {
21212121

21222122
// We didn't get a comma, and the list wasn't terminated, explicitly parse
21232123
// out a comma so we give a good error message.
2124-
parseExpected(SyntaxKind.CommaToken);
2124+
parseExpected(SyntaxKind.CommaToken, getExpectedCommaDiagnostic(kind));
21252125

21262126
// If the token was a semicolon, and the caller allows that, then skip it and
21272127
// continue. This ensures we get back on track and don't result in tons of
@@ -2164,6 +2164,10 @@ namespace ts {
21642164
return result;
21652165
}
21662166

2167+
function getExpectedCommaDiagnostic(kind: ParsingContext) {
2168+
return kind === ParsingContext.EnumMembers ? Diagnostics.An_enum_member_name_must_be_followed_by_a_or : undefined;
2169+
}
2170+
21672171
interface MissingList<T extends Node> extends NodeArray<T> {
21682172
isMissingList: true;
21692173
}

tests/baselines/reference/constEnum2.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/conformance/constEnums/constEnum2.ts(10,9): error TS2474: const enum member initializers can only contain literal values and other computed enum values.
22
tests/cases/conformance/constEnums/constEnum2.ts(11,9): error TS2474: const enum member initializers can only contain literal values and other computed enum values.
3-
tests/cases/conformance/constEnums/constEnum2.ts(12,5): error TS1005: ',' expected.
3+
tests/cases/conformance/constEnums/constEnum2.ts(12,5): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
44
tests/cases/conformance/constEnums/constEnum2.ts(12,9): error TS2474: const enum member initializers can only contain literal values and other computed enum values.
55

66

@@ -22,7 +22,7 @@ tests/cases/conformance/constEnums/constEnum2.ts(12,9): error TS2474: const enum
2222
!!! error TS2474: const enum member initializers can only contain literal values and other computed enum values.
2323
g = CONST,
2424
~
25-
!!! error TS1005: ',' expected.
25+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
2626
~~~~~
2727
!!! error TS2474: const enum member initializers can only contain literal values and other computed enum values.
2828
}

tests/baselines/reference/enumErrors.errors.txt

+43-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ tests/cases/conformance/enums/enumErrors.ts(35,9): error TS2553: Computed values
1111
tests/cases/conformance/enums/enumErrors.ts(36,9): error TS2553: Computed values are not permitted in an enum with string valued members.
1212
tests/cases/conformance/enums/enumErrors.ts(37,9): error TS2553: Computed values are not permitted in an enum with string valued members.
1313
tests/cases/conformance/enums/enumErrors.ts(38,9): error TS2553: Computed values are not permitted in an enum with string valued members.
14+
tests/cases/conformance/enums/enumErrors.ts(46,18): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
15+
tests/cases/conformance/enums/enumErrors.ts(47,24): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
16+
tests/cases/conformance/enums/enumErrors.ts(47,26): error TS2452: An enum member cannot have a numeric name.
17+
tests/cases/conformance/enums/enumErrors.ts(48,28): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
18+
tests/cases/conformance/enums/enumErrors.ts(48,30): error TS2452: An enum member cannot have a numeric name.
19+
tests/cases/conformance/enums/enumErrors.ts(48,31): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
20+
tests/cases/conformance/enums/enumErrors.ts(51,16): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
21+
tests/cases/conformance/enums/enumErrors.ts(51,22): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
22+
tests/cases/conformance/enums/enumErrors.ts(51,30): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
23+
tests/cases/conformance/enums/enumErrors.ts(51,33): error TS2452: An enum member cannot have a numeric name.
1424

1525

16-
==== tests/cases/conformance/enums/enumErrors.ts (13 errors) ====
26+
==== tests/cases/conformance/enums/enumErrors.ts (23 errors) ====
1727
// Enum named with PredefinedTypes
1828
enum any { }
1929
~~~
@@ -79,4 +89,36 @@ tests/cases/conformance/enums/enumErrors.ts(38,9): error TS2553: Computed values
7989
~~~~~
8090
!!! error TS2553: Computed values are not permitted in an enum with string valued members.
8191
}
92+
93+
// Enum with incorrect syntax
94+
enum E13 {
95+
postComma,
96+
postValueComma = 1,
97+
98+
postSemicolon;
99+
~
100+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
101+
postColonValueComma: 2,
102+
~
103+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
104+
~
105+
!!! error TS2452: An enum member cannot have a numeric name.
106+
postColonValueSemicolon: 3;
107+
~
108+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
109+
~
110+
!!! error TS2452: An enum member cannot have a numeric name.
111+
~
112+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
113+
};
114+
115+
enum E14 { a, b: any "hello" += 1, c, d}
116+
~
117+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
118+
~~~~~~~
119+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
120+
~~
121+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
122+
~
123+
!!! error TS2452: An enum member cannot have a numeric name.
82124

tests/baselines/reference/enumErrors.js

+34
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ enum E12 {
3838
D = {},
3939
E = 1 + 1,
4040
}
41+
42+
// Enum with incorrect syntax
43+
enum E13 {
44+
postComma,
45+
postValueComma = 1,
46+
47+
postSemicolon;
48+
postColonValueComma: 2,
49+
postColonValueSemicolon: 3;
50+
};
51+
52+
enum E14 { a, b: any "hello" += 1, c, d}
4153

4254

4355
//// [enumErrors.js]
@@ -88,3 +100,25 @@ var E12;
88100
E12[E12["D"] = 0] = "D";
89101
E12[E12["E"] = 0] = "E";
90102
})(E12 || (E12 = {}));
103+
// Enum with incorrect syntax
104+
var E13;
105+
(function (E13) {
106+
E13[E13["postComma"] = 0] = "postComma";
107+
E13[E13["postValueComma"] = 1] = "postValueComma";
108+
E13[E13["postSemicolon"] = 2] = "postSemicolon";
109+
E13[E13["postColonValueComma"] = 3] = "postColonValueComma";
110+
E13[E13[2] = 4] = 2;
111+
E13[E13["postColonValueSemicolon"] = 5] = "postColonValueSemicolon";
112+
E13[E13[3] = 6] = 3;
113+
})(E13 || (E13 = {}));
114+
;
115+
var E14;
116+
(function (E14) {
117+
E14[E14["a"] = 0] = "a";
118+
E14[E14["b"] = 1] = "b";
119+
E14[E14["any"] = 2] = "any";
120+
E14[E14["hello"] = 3] = "hello";
121+
E14[E14[1] = 4] = 1;
122+
E14[E14["c"] = 5] = "c";
123+
E14[E14["d"] = 6] = "d";
124+
})(E14 || (E14 = {}));

tests/baselines/reference/enumErrors.symbols

+33
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,36 @@ enum E12 {
9191
>E : Symbol(E12.E, Decl(enumErrors.ts, 36, 11))
9292
}
9393

94+
// Enum with incorrect syntax
95+
enum E13 {
96+
>E13 : Symbol(E13, Decl(enumErrors.ts, 38, 1))
97+
98+
postComma,
99+
>postComma : Symbol(E13.postComma, Decl(enumErrors.ts, 41, 10))
100+
101+
postValueComma = 1,
102+
>postValueComma : Symbol(E13.postValueComma, Decl(enumErrors.ts, 42, 14))
103+
104+
postSemicolon;
105+
>postSemicolon : Symbol(E13.postSemicolon, Decl(enumErrors.ts, 43, 23))
106+
107+
postColonValueComma: 2,
108+
>postColonValueComma : Symbol(E13.postColonValueComma, Decl(enumErrors.ts, 45, 18))
109+
>2 : Symbol(E13[2], Decl(enumErrors.ts, 46, 24))
110+
111+
postColonValueSemicolon: 3;
112+
>postColonValueSemicolon : Symbol(E13.postColonValueSemicolon, Decl(enumErrors.ts, 46, 27))
113+
>3 : Symbol(E13[3], Decl(enumErrors.ts, 47, 28))
114+
115+
};
116+
117+
enum E14 { a, b: any "hello" += 1, c, d}
118+
>E14 : Symbol(E14, Decl(enumErrors.ts, 48, 2))
119+
>a : Symbol(E14.a, Decl(enumErrors.ts, 50, 10))
120+
>b : Symbol(E14.b, Decl(enumErrors.ts, 50, 13))
121+
>any : Symbol(E14.any, Decl(enumErrors.ts, 50, 16))
122+
>"hello" : Symbol(E14["hello"], Decl(enumErrors.ts, 50, 20))
123+
>1 : Symbol(E14[1], Decl(enumErrors.ts, 50, 31))
124+
>c : Symbol(E14.c, Decl(enumErrors.ts, 50, 34))
125+
>d : Symbol(E14.d, Decl(enumErrors.ts, 50, 37))
126+

tests/baselines/reference/enumErrors.types

+34
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,37 @@ enum E12 {
102102
>1 : 1
103103
}
104104

105+
// Enum with incorrect syntax
106+
enum E13 {
107+
>E13 : E13
108+
109+
postComma,
110+
>postComma : E13.postComma
111+
112+
postValueComma = 1,
113+
>postValueComma : E13.postValueComma
114+
>1 : 1
115+
116+
postSemicolon;
117+
>postSemicolon : E13.postSemicolon
118+
119+
postColonValueComma: 2,
120+
>postColonValueComma : E13.postColonValueComma
121+
>2 : E13.2
122+
123+
postColonValueSemicolon: 3;
124+
>postColonValueSemicolon : E13.postColonValueSemicolon
125+
>3 : E13.3
126+
127+
};
128+
129+
enum E14 { a, b: any "hello" += 1, c, d}
130+
>E14 : E14
131+
>a : E14.a
132+
>b : E14.b
133+
>any : E14.any
134+
>"hello" : E14.hello
135+
>1 : E14.1
136+
>c : E14.c
137+
>d : E14.d
138+

tests/baselines/reference/parserComputedPropertyName30.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts(3,5): error TS1164: Computed property names are not allowed in enums.
22
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts(3,11): error TS2304: Cannot find name 'id'.
3-
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts(4,5): error TS1005: ',' expected.
3+
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts(4,5): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
44
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts(4,5): error TS1164: Computed property names are not allowed in enums.
55

66

@@ -14,7 +14,7 @@ tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedP
1414
!!! error TS2304: Cannot find name 'id'.
1515
[e2] = 1
1616
~
17-
!!! error TS1005: ',' expected.
17+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
1818
~~~~
1919
!!! error TS1164: Computed property names are not allowed in enums.
2020
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum5.ts(2,12): error TS1005: ',' expected.
1+
tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum5.ts(2,12): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
22
tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum5.ts(2,14): error TS2452: An enum member cannot have a numeric name.
3-
tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum5.ts(3,15): error TS1005: ',' expected.
3+
tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum5.ts(3,15): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
44
tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum5.ts(3,17): error TS2452: An enum member cannot have a numeric name.
5-
tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum5.ts(3,24): error TS1005: ',' expected.
5+
tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum5.ts(3,24): error TS1357: An enum member name must be followed by a ',', '=', or '}'.
66
tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum5.ts(3,26): error TS2452: An enum member cannot have a numeric name.
77

88

99
==== tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum5.ts (6 errors) ====
1010
enum E2 { a, }
1111
enum E3 { a: 1, }
1212
~
13-
!!! error TS1005: ',' expected.
13+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
1414
~
1515
!!! error TS2452: An enum member cannot have a numeric name.
1616
enum E1 { a, b: 1, c, d: 2 = 3 }
1717
~
18-
!!! error TS1005: ',' expected.
18+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
1919
~
2020
!!! error TS2452: An enum member cannot have a numeric name.
2121
~
22-
!!! error TS1005: ',' expected.
22+
!!! error TS1357: An enum member name must be followed by a ',', '=', or '}'.
2323
~
2424
!!! error TS2452: An enum member cannot have a numeric name.

tests/cases/conformance/enums/enumErrors.ts

+12
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,15 @@ enum E12 {
3737
D = {},
3838
E = 1 + 1,
3939
}
40+
41+
// Enum with incorrect syntax
42+
enum E13 {
43+
postComma,
44+
postValueComma = 1,
45+
46+
postSemicolon;
47+
postColonValueComma: 2,
48+
postColonValueSemicolon: 3;
49+
};
50+
51+
enum E14 { a, b: any "hello" += 1, c, d}

0 commit comments

Comments
 (0)