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.
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
Hence we could throw at prop.start.
| 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.
What if we instead record the traillingCommaAttachedNodeStart? So we can always throw at the node.
There was a problem hiding this comment.
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) { |
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
parseExpressionbecause, for example, in[[...a,]] = []parseExpressionis finished after parsing the inner array.