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:
committed by
Nicolò Ribaudo
parent
61c1c77a28
commit
445b14148e
@@ -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;
|
||||
|
||||
@@ -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 = [];
|
||||
|
||||
Reference in New Issue
Block a user