Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26093,6 +26093,9 @@ namespace ts {
if (i !== (parameterCount - 1)) {
return grammarErrorOnNode(parameter.dotDotDotToken, Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list);
}
if (parameters.hasTrailingComma) {
return grammarErrorOnNode(parameter.dotDotDotToken, Diagnostics.A_rest_parameter_may_not_have_a_trailing_comma);
Copy link
Member

@DanielRosenwasser DanielRosenwasser Mar 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that this is the wrong span to error on. Either error on the comma, or the parameter itself.

}

if (isBindingPattern(parameter.name)) {
return grammarErrorOnNode(parameter.name, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"category": "Error",
"code": 1012
},
"A rest parameter may not have a trailing comma.": {
"category": "Error",
"code": 1013
},
"A rest parameter must be last in a parameter list.": {
"category": "Error",
"code": 1014
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts(5,13): error TS1013: A rest parameter may not have a trailing comma.


==== tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts (1 errors) ====
function f1(x,) {}

f1(1,);

function f2(...args,) {}
~~~
!!! error TS1013: A rest parameter may not have a trailing comma.

f2(...[],);

// Not confused by overloads
declare function f3(x, ): number;
declare function f3(x, y,): string;

<number>f3(1,);
<string>f3(1, 2,);

// Works for constructors too
class X {
constructor(a,) { }
// See trailingCommasInGetter.ts
set x(value,) { }
}
interface Y {
new(x,);
(x,);
}

new X(1,);