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
Trailing comma after rest - The final fix #10491
Trailing comma after rest - The final fix #10491
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11700/ |
4b34713
to
2830984
Compare
prop.type === "RestElement" && | ||
node.extra?.trailingComma | ||
) { | ||
this.raiseRestNotLast(node.extra.trailingComma); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By raising error at comma position we are not really consistent with where this error is throw in other situation, i.e. in toAssignableObjectExpressionProp
.
babel/packages/babel-parser/src/parser/lval.js
Lines 132 to 134 in 11fa246
} else if (prop.type === "SpreadElement" && !isLast) { | |
this.raiseRestNotLast(prop.start); | |
} else { |
Hence we could throw at prop.start
.
@@ -1488,7 +1489,11 @@ export default class ExpressionParser extends LValParser { | |||
first = false; | |||
} else { | |||
this.expect(tt.comma); | |||
if (this.eat(tt.braceR)) break; | |||
if (this.match(tt.braceR)) { | |||
this.addExtra(node, "trailingComma", this.state.lastTokStart); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we instead record the traillingCommaAttachedNodeStart
? So we can always throw at the node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to make trailingComma
a boolean, and then throw at props[props.length - 1].start
@JLHwung I ended up fixing the error location in a different way.
|
if (this.state.commaAfterSpreadAt > -1) { | ||
this.raiseRestNotLast(this.state.commaAfterSpreadAt); | ||
// TODO: Use lookaheadCharCode after that https://github.com/babel/babel/pull/10371 is merged | ||
if (this.lookahead().type == close) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6d78f91
to
c7b0d4a
Compare
Tracking a comma after a spread/rest element is ticky, because we don't know when we should reset the state. We can't do it in
parseExpression
because, for example, in[[...a,]] = []
parseExpression
is finished after parsing the inner array.