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

Trailing comma after rest - The final fix #10491

Merged

Conversation

nicolo-ribaudo
Copy link
Member

Q                       A
Fixed Issues?
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

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.

@nicolo-ribaudo nicolo-ribaudo added PR: Spec Compliance 👓 A type of pull request used for our changelog categories pkg: parser labels Sep 24, 2019
@nicolo-ribaudo nicolo-ribaudo changed the title Traling comma after rest - The final fix Trailing comma after rest - The final fix Sep 24, 2019
@babel-bot
Copy link
Collaborator

babel-bot commented Sep 24, 2019

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11700/

@nicolo-ribaudo nicolo-ribaudo force-pushed the traling-comma-after-rest-final-fix branch from 4b34713 to 2830984 Compare September 24, 2019 22:07
@JLHwung JLHwung self-requested a review September 25, 2019 00:38
prop.type === "RestElement" &&
node.extra?.trailingComma
) {
this.raiseRestNotLast(node.extra.trailingComma);
Copy link
Contributor

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.

} 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);
Copy link
Contributor

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.

Copy link
Member Author

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

@nicolo-ribaudo
Copy link
Member Author

@JLHwung I ended up fixing the error location in a different way.
It now throws two different errors:

  1. Rest element must be last element, pointing to the rest element
  2. Unexpected trailing comma after rest element, pointing to the trailing comma

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) {
Copy link
Member Author

Choose a reason for hiding this comment

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

@nicolo-ribaudo nicolo-ribaudo force-pushed the traling-comma-after-rest-final-fix branch from 6d78f91 to c7b0d4a Compare October 8, 2019 17:26
@nicolo-ribaudo nicolo-ribaudo merged commit 34937f1 into babel:master Oct 8, 2019
@nicolo-ribaudo nicolo-ribaudo deleted the traling-comma-after-rest-final-fix branch October 8, 2019 21:08
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 7, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Jan 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Spec Compliance 👓 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants