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

Fix destructuring with holes in assign pattern #14240

Merged
merged 4 commits into from Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions packages/babel-plugin-transform-destructuring/src/index.ts
Expand Up @@ -114,6 +114,10 @@ export default declare((api, options) => {
),
);
} else {
if (this.kind === "const" && init === null) {
init = this.scope.buildUndefinedNode();
}

node = t.variableDeclaration(this.kind, [
t.variableDeclarator(id, t.cloneNode(init)),
]);
Expand Down Expand Up @@ -158,6 +162,14 @@ export default declare((api, options) => {
}

pushAssignmentPattern({ left, right }, valueRef) {
// handle array init hole
// const [x = 42] = [,];
// -> const x = 42;
if (valueRef === null) {
this.nodes.push(this.buildVariableAssignment(left, right));
return;
}

// we need to assign the current value of the assignment to avoid evaluating
// it more than once
const tempId = this.scope.generateUidIdentifierBasedOnNode(valueRef);
Expand Down
@@ -0,0 +1,45 @@
let [x = 23] = [,];
expect(x).toEqual(23);

const [y = 24, z] = [, 42];
expect(y).toEqual(24);
expect(z).toEqual(42);

function* foo() {
yield 1;
yield 2;
}

let bar = foo();

const [a = bar.next().value, b] = [, bar.next().value];

expect(a).toEqual(2);
expect(b).toEqual(1);

const arr = [c = 42] = [,];
expect(c).toEqual(42);
expect(arr).toEqual([,]);

var iterCount = 0;

for (const [x = 23] = [,]; iterCount < 1; ) {
expect(x).toEqual(23);
// another statement

iterCount += 1;
}

expect(iterCount).toEqual(1);

const [...d] = [,];
const [...{ 0: e }] = [,];

expect(d).toEqual([,]);
expect(e).toEqual(undefined);

const [f] = [,];
expect(f).toEqual(undefined);

let [g] = [,];
expect(g).toEqual(undefined);
@@ -0,0 +1,45 @@
let [x = 23] = [,];
expect(x).toEqual(23);

const [y = 24, z] = [, 42];
expect(y).toEqual(24);
expect(z).toEqual(42);

function* foo() {
yield 1;
yield 2;
}

let bar = foo();

const [a = bar.next().value, b] = [, bar.next().value];

expect(a).toEqual(2);
expect(b).toEqual(1);

const arr = [c = 42] = [,];
expect(c).toEqual(42);
expect(arr).toEqual([,]);

var iterCount = 0;

for (const [x = 23] = [,]; iterCount < 1; ) {
expect(x).toEqual(23);
// another statement

iterCount += 1;
}

expect(iterCount).toEqual(1);

const [...d] = [,];
const [...{ 0: e }] = [,];

expect(d).toEqual([,]);
expect(e).toEqual(undefined);

const [f] = [,];
expect(f).toEqual(undefined);

let [g] = [,];
expect(g).toEqual(undefined);
@@ -0,0 +1,3 @@
{
"plugins": ["transform-destructuring"]
}
@@ -0,0 +1,41 @@
var _ref2, _ref2$;

let x = 23;
expect(x).toEqual(23);
const y = 24,
z = 42;
expect(y).toEqual(24);
expect(z).toEqual(42);

function* foo() {
yield 1;
yield 2;
}

let bar = foo();
const _ref = [, bar.next().value],
_ref$ = _ref[0],
a = _ref$ === void 0 ? bar.next().value : _ref$,
b = _ref[1];
expect(a).toEqual(2);
expect(b).toEqual(1);
const arr = (_ref2 = [,], _ref2$ = _ref2[0], c = _ref2$ === void 0 ? 42 : _ref2$, _ref2);
expect(c).toEqual(42);
expect(arr).toEqual([,]);
var iterCount = 0;

for (const x = 23; iterCount < 1;) {
expect(x).toEqual(23); // another statement

iterCount += 1;
}

expect(iterCount).toEqual(1);
const d = [,];
const e = [,][0];
expect(d).toEqual([,]);
expect(e).toEqual(undefined);
const f = void 0;
expect(f).toEqual(undefined);
let g;
expect(g).toEqual(undefined);