Skip to content

Commit 0a32978

Browse files
author
Andy Hanson
committed
Error on rest parameter with trailing comma
1 parent c487a9d commit 0a32978

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26093,6 +26093,9 @@ namespace ts {
2609326093
if (i !== (parameterCount - 1)) {
2609426094
return grammarErrorOnNode(parameter.dotDotDotToken, Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list);
2609526095
}
26096+
if (parameters.hasTrailingComma) {
26097+
return grammarErrorOnNode(parameter.dotDotDotToken, Diagnostics.A_rest_parameter_may_not_have_a_trailing_comma);
26098+
}
2609626099

2609726100
if (isBindingPattern(parameter.name)) {
2609826101
return grammarErrorOnNode(parameter.name, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern);

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
"category": "Error",
2828
"code": 1012
2929
},
30+
"A rest parameter may not have a trailing comma.": {
31+
"category": "Error",
32+
"code": 1013
33+
},
3034
"A rest parameter must be last in a parameter list.": {
3135
"category": "Error",
3236
"code": 1014
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts(5,13): error TS1013: A rest parameter may not have a trailing comma.
2+
3+
4+
==== tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts (1 errors) ====
5+
function f1(x,) {}
6+
7+
f1(1,);
8+
9+
function f2(...args,) {}
10+
~~~
11+
!!! error TS1013: A rest parameter may not have a trailing comma.
12+
13+
f2(...[],);
14+
15+
// Not confused by overloads
16+
declare function f3(x, ): number;
17+
declare function f3(x, y,): string;
18+
19+
<number>f3(1,);
20+
<string>f3(1, 2,);
21+
22+
// Works for constructors too
23+
class X {
24+
constructor(a,) { }
25+
// See trailingCommasInGetter.ts
26+
set x(value,) { }
27+
}
28+
interface Y {
29+
new(x,);
30+
(x,);
31+
}
32+
33+
new X(1,);
34+

0 commit comments

Comments
 (0)