Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a better error message for disallowed trailing commas/additional parameters after rest elements in function params #9046

Merged
merged 4 commits into from Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -1120,6 +1120,17 @@ export default class ExpressionParser extends LValParser {
);
}

if (
this.match(tt.comma) &&
(this.lookahead().type === tt.name ||
this.lookahead().type === tt.ellipsis)
xtuc marked this conversation as resolved.
Show resolved Hide resolved
) {
this.raise(
this.state.start,
"Rest parameter must be last formal parameter",
Copy link
Member

@xtuc xtuc Nov 20, 2018

Choose a reason for hiding this comment

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

Thanks for your PR, It looks good to me.

At first the error mesage looked odd to me, but it's actually V8's one 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@xtuc thanks! I'm glad that my first PR was not bad 😃 Yeah, I decided to get this message from V8.

);
}

break;
} else {
exprList.push(
Expand Down
14 changes: 13 additions & 1 deletion packages/babel-parser/src/parser/lval.js
Expand Up @@ -258,7 +258,19 @@ 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.lookahead().type === tt.name ||
this.lookahead().type === tt.ellipsis)
) {
this.raise(
this.state.start,
"Rest parameter must be last formal parameter",
);
} 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,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)"
}