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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/babel-plugin-transform-destructuring/src/index.js
Expand Up @@ -83,7 +83,11 @@ export default declare((api, options) => {

if (op) {
node = t.expressionStatement(
t.assignmentExpression(op, id, t.cloneNode(init)),
t.assignmentExpression(
op,
id,
t.cloneNode(init) || this.scope.buildUndefinedNode(),
),
);
} else {
node = t.variableDeclaration(this.kind, [
Expand Down
Expand Up @@ -11,3 +11,7 @@ var [a, b] = [...foo, bar];
var [a, b] = [foo(), bar];
var [a, b] = [clazz.foo(), bar];
var [a, b] = [clazz.foo, bar];
var [a, b] = [, 2];
[a, b] = [1, 2];
[a, b] = [, 2];
; // Avoid completion record special case
Copy link
Member

Choose a reason for hiding this comment

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

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

eval("0;;;;")

Can you use something like 0; instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

This can/should be handled in another PR

Expand Up @@ -34,3 +34,10 @@ var _ref7 = [clazz.foo(), bar],
var _ref8 = [clazz.foo, bar],
a = _ref8[0],
b = _ref8[1];
var a,
b = 2;
a = 1;
b = 2;
a = void 0;
b = 2;
; // Avoid completion record special case