From 26e89f2bf638b24ef77b7c53bf130f0679057a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 11 Feb 2022 15:45:11 -0500 Subject: [PATCH] apply #14240 fix --- .../src/util.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-transform-destructuring/src/util.ts b/packages/babel-plugin-transform-destructuring/src/util.ts index 597055091e4a..60b295efcd67 100644 --- a/packages/babel-plugin-transform-destructuring/src/util.ts +++ b/packages/babel-plugin-transform-destructuring/src/util.ts @@ -122,8 +122,16 @@ export class DestructuringTransformer { ), ); } 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), ]); } @@ -170,6 +178,14 @@ export class DestructuringTransformer { { left, right }: t.AssignmentPattern, valueRef: t.Expression, ) { + // 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);