Skip to content

Commit

Permalink
Fix destructuring with holes in assign pattern (#14240)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Feb 11, 2022
1 parent e06c099 commit 6b4d466
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/babel-plugin-transform-destructuring/src/index.ts
Expand Up @@ -114,8 +114,16 @@ export default declare((api, options) => {
),
);
} else {
let nodeInit: t.Expression;

if (this.kind === "const" && init === null) {
nodeInit = this.scope.buildUndefinedNode();
} else {
nodeInit = t.cloneNode(init);
}

node = t.variableDeclaration(this.kind, [
t.variableDeclarator(id, t.cloneNode(init)),
t.variableDeclarator(id, nodeInit),
]);
}

Expand Down Expand Up @@ -158,6 +166,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);

0 comments on commit 6b4d466

Please sign in to comment.