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

Destructuring: Fix array unpacking assignments with holes on RHS #9412

Merged

Conversation

@motiz88
Copy link
Contributor

commented Jan 26, 2019

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

This fixes an issue where destructuring assignments eligible for the "array unpacking" optimization would fail to compile when the array literal on the right-hand side of the expression contained holes.

Example input:

[a, b] = [, 2];
; // Avoid completion record special case

The error message was Property right of AssignmentExpression expected node to be of a type ["Expression"] but instead got null.

Now the above code compiles to:

a = void 0;
b = 2;
;

This PR also adds a couple of related test cases that were missing, to ensure the change doesn't regress them:

  • Normal assignment expression with unpacking
  • Declaration with unpacking and a hole on RHS

@motiz88 motiz88 force-pushed the motiz88:fix-destructuring-assign-unpack-holes branch from 0ecb9ca to 741b722 Jan 26, 2019

@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Jan 26, 2019

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

1 similar comment
@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Jan 26, 2019

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

var [a, b] = [, 2];
[a, b] = [1, 2];
[a, b] = [, 2];
; // Avoid completion record special case

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Jan 26, 2019

Member

Not in this PR, but this optimization is wrong: ; keeps the previous completion value:

eval("0;;;;")

Can you use something like 0; instead?

This comment has been minimized.

Copy link
@motiz88

motiz88 Jan 26, 2019

Author Contributor

Yeah, I've just realised this on another PR. Ultimately it's a false negative in the isCompletionRecord check, which could be quite expensive to correct without memoising somehow.

I'll change this, but FWIW there are several other tests that currently use ; in this way and would need to change similarly.

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Mar 27, 2019

Member

This can/should be handled in another PR

@danez
danez approved these changes Mar 27, 2019

@nicolo-ribaudo nicolo-ribaudo merged commit f7bfc77 into babel:master Mar 27, 2019

4 checks passed

babel/repl REPL preview is available
Details
ci/circleci Your tests passed on CircleCI!
Details
codecov/project 80.77% (target 80%)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Mar 27, 2019

Thank you!

NMinhNguyen pushed a commit to NMinhNguyen/babel-plugin-transform-destructuring that referenced this pull request Aug 9, 2019
Destructuring: Fix array unpacking assignments with holes on RHS (bab…
…el#9412)

This fixes an issue where destructuring assignments eligible for the "array unpacking" optimization would fail to compile when the array literal on the right-hand side of the expression contained holes.

Example input:
```js
[a, b] = [, 2];
; // Avoid completion record special case
```

The error message was `Property right of AssignmentExpression expected node to be of a type ["Expression"] but instead got null`.

Now the above code compiles to:
```js
a = void 0;
b = 2;
;
```

This PR also adds a couple of related test cases that were missing, to ensure the change doesn't regress them:
* Normal assignment expression with unpacking
* Declaration with unpacking and a hole on RHS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.