Better error for disallowed trailing commas/parameters after rest elements (#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]
This commit is contained in:
Grigory Moroz
2018-11-21 07:49:36 +01:00
committed by Nicolò Ribaudo
parent 61c1c77a28
commit 445b14148e
13 changed files with 49 additions and 11 deletions

View File

@@ -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;

View File

@@ -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 = [];