Skip to content

Commit

Permalink
Better error for disallowed trailing commas/parameters after rest ele…
Browse files Browse the repository at this point in the history
…ments (#9046)

* handle disordered rest parameter in function expressions

* remove spaces [lint]

* polish function parameters validation

* add test with arrow function and comma after rest parameter [babel-parser]
  • Loading branch information
morozRed authored and nicolo-ribaudo committed Nov 21, 2018
1 parent 61c1c77 commit 445b141
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 11 deletions.
12 changes: 7 additions & 5 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -1113,11 +1113,13 @@ export default class ExpressionParser extends LValParser {
),
);

if (this.match(tt.comma) && this.lookahead().type === tt.parenR) {
this.raise(
this.state.start,
"A trailing comma is not permitted after the rest element",
);
if (this.match(tt.comma)) {
const nextTokenType = this.lookahead().type;
const errorMessage =
nextTokenType === tt.parenR
? "A trailing comma is not permitted after the rest element"
: "Rest parameter must be last formal parameter";
this.raise(this.state.start, errorMessage);
}

break;
Expand Down
15 changes: 14 additions & 1 deletion packages/babel-parser/src/parser/lval.js
Expand Up @@ -258,7 +258,20 @@ export default class LValParser extends NodeUtils {
break;
} else if (this.match(tt.ellipsis)) {
elts.push(this.parseAssignableListItemTypes(this.parseRest()));
this.expect(close);
if (
this.state.inFunction &&
this.state.inParameters &&
this.match(tt.comma)
) {
const nextTokenType = this.lookahead().type;
const errorMessage =
nextTokenType === tt.parenR
? "A trailing comma is not permitted after the rest element"
: "Rest parameter must be last formal parameter";
this.raise(this.state.start, errorMessage);
} else {
this.expect(close);
}
break;
} else {
const decorators = [];
Expand Down
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \")\" (1:18)"
"throws": "Rest parameter must be last formal parameter (1:18)"
}
@@ -0,0 +1,6 @@
function foo (
first,
...second,
third,
) {
};
@@ -0,0 +1,4 @@
{
"throws": "Rest parameter must be last formal parameter (3:13)"
}

@@ -0,0 +1 @@
(...rest,) => {}
@@ -0,0 +1,3 @@
{
"throws": "A trailing comma is not permitted after the rest element (1:8)"
}
@@ -0,0 +1,5 @@
(
first,
...second,
third
) => {};
@@ -0,0 +1,4 @@
{
"throws": "Rest parameter must be last formal parameter (3:13)"
}

@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \")\" (1:18)"
"throws": "Rest parameter must be last formal parameter (1:18)"
}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \")\" (1:5)"
"throws": "Rest parameter must be last formal parameter (1:5)"
}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \")\" (1:5)"
"throws": "Rest parameter must be last formal parameter (1:5)"
}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \")\" (1:18)"
"throws": "Rest parameter must be last formal parameter (1:18)"
}

0 comments on commit 445b141

Please sign in to comment.