Skip to content

Commit 49be51d

Browse files
author
Josh Goldberg
committed
Added more helpful syntax error for enum member commas
Switches the error message emitted by the parser to the more specific _"An enum member name must be followed by a ',' or '='."_ when the expected comma doesn't follow the member.
1 parent 73a2146 commit 49be51d

File tree

7 files changed

+165
-2
lines changed

7 files changed

+165
-2
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2125,7 +2125,7 @@ namespace ts {
21252125

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

21302130
// If the token was a semicolon, and the caller allows that, then skip it and
21312131
// continue. This ensures we get back on track and don't result in tons of
@@ -2168,6 +2168,10 @@ namespace ts {
21682168
return result;
21692169
}
21702170

2171+
function getExpectedCommaDiagnostic(kind: ParsingContext) {
2172+
return kind === ParsingContext.EnumMembers ? Diagnostics.An_enum_member_name_must_be_followed_by_a_or : undefined;
2173+
}
2174+
21712175
interface MissingList<T extends Node> extends NodeArray<T> {
21722176
isMissingList: true;
21732177
}

tests/baselines/reference/enumErrors.errors.txt

Lines changed: 43 additions & 1 deletion
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

Lines changed: 34 additions & 0 deletions
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

Lines changed: 33 additions & 0 deletions
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

Lines changed: 34 additions & 0 deletions
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/cases/conformance/enums/enumErrors.ts

Lines changed: 12 additions & 0 deletions
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)