diff --git a/packages/babel-plugin-transform-destructuring/src/index.js b/packages/babel-plugin-transform-destructuring/src/index.js index 34cd6ef7a9c4..88057b7ad0bc 100644 --- a/packages/babel-plugin-transform-destructuring/src/index.js +++ b/packages/babel-plugin-transform-destructuring/src/index.js @@ -111,43 +111,40 @@ export default declare((api, options) => { } } - pushAssignmentPattern(pattern, valueRef) { + pushAssignmentPattern({ left, right }, valueRef) { // we need to assign the current value of the assignment to avoid evaluating // it more than once + const tempId = this.scope.generateUidIdentifierBasedOnNode(valueRef); - const tempValueRef = this.scope.generateUidBasedOnNode(valueRef); - - const declar = t.variableDeclaration("var", [ - t.variableDeclarator(t.identifier(tempValueRef), valueRef), - ]); - declar._blockHoist = this.blockHoist; - this.nodes.push(declar); - - // + this.nodes.push(this.buildVariableDeclaration(tempId, valueRef)); const tempConditional = t.conditionalExpression( t.binaryExpression( "===", - t.identifier(tempValueRef), + t.cloneNode(tempId), this.scope.buildUndefinedNode(), ), - pattern.right, - t.identifier(tempValueRef), + right, + t.cloneNode(tempId), ); - const left = pattern.left; if (t.isPattern(left)) { - const tempValueDefault = t.expressionStatement( - t.assignmentExpression( - "=", - t.identifier(tempValueRef), - tempConditional, - ), - ); - tempValueDefault._blockHoist = this.blockHoist; + let patternId; + let node; + + if (this.kind === "const") { + patternId = this.scope.generateUidIdentifier(tempId.name); + node = this.buildVariableDeclaration(patternId, tempConditional); + } else { + patternId = tempId; + + node = t.expressionStatement( + t.assignmentExpression("=", t.cloneNode(tempId), tempConditional), + ); + } - this.nodes.push(tempValueDefault); - this.push(left, t.identifier(tempValueRef)); + this.nodes.push(node); + this.push(left, patternId); } else { this.nodes.push(this.buildVariableAssignment(left, tempConditional)); } diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/exec.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/exec.js new file mode 100644 index 000000000000..e05920b47f65 --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/exec.js @@ -0,0 +1,5 @@ +const getState = () => ({}); + +const { data: { courses: oldCourses = [] } = {} } = getState(); + +assert.deepEqual(oldCourses, []); diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/input.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/input.js new file mode 100644 index 000000000000..09e29d6f8b28 --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/input.js @@ -0,0 +1 @@ +const { data: { courses: oldCourses = [] } = {} } = getState(); diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/options.json b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/options.json new file mode 100644 index 000000000000..84e28c07f51e --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-destructuring"] +} diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/output.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/output.js new file mode 100644 index 000000000000..708ac3713300 --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/const/output.js @@ -0,0 +1,5 @@ +const _getState = getState(), + _getState$data = _getState.data, + _getState$data2 = _getState$data === void 0 ? {} : _getState$data, + _getState$data2$cours = _getState$data2.courses, + oldCourses = _getState$data2$cours === void 0 ? [] : _getState$data2$cours;