From c2d2702cf93f372f7d8e1359111daafeed9fd695 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sun, 25 Sep 2016 01:08:53 +0200 Subject: [PATCH] Fix destructuring evaluation with call expressions (#4552) * Fix destructuring evaluation with call expressions Do not optimize destructions with callExpressions, as the call might change the value of a variable that we are assigning to. Fixes #4054 * Also deopt on member expressions members expressions might be getters who have side effects --- .../src/index.js | 6 ++++++ .../destructuring/array-unpack-optimisation/actual.js | 3 +++ .../destructuring/array-unpack-optimisation/expected.js | 9 +++++++++ 3 files changed, 18 insertions(+) diff --git a/packages/babel-plugin-transform-es2015-destructuring/src/index.js b/packages/babel-plugin-transform-es2015-destructuring/src/index.js index 95533c4618..0a80489ac9 100644 --- a/packages/babel-plugin-transform-es2015-destructuring/src/index.js +++ b/packages/babel-plugin-transform-es2015-destructuring/src/index.js @@ -221,6 +221,12 @@ export default function ({ types: t }) { for (let elem of (arr.elements: Array)) { // deopt on spread elements if (t.isSpreadElement(elem)) return false; + + // deopt call expressions as they might change values of LHS variables + if (t.isCallExpression(elem)) return false; + + // deopt on member expressions as they may be getter/setters and have side-effects + if (t.isMemberExpression(elem)) return false; } // deopt on reference to left side identifiers diff --git a/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/array-unpack-optimisation/actual.js b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/array-unpack-optimisation/actual.js index 0d02d7f7ba..87493c5fd5 100644 --- a/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/array-unpack-optimisation/actual.js +++ b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/array-unpack-optimisation/actual.js @@ -8,3 +8,6 @@ var [[a, b]] = [[1, 2, 3]]; var [a, b] = [a, b]; [a[0], a[1]] = [a[1], a[0]]; var [a, b] = [...foo, bar]; +var [a, b] = [foo(), bar]; +var [a, b] = [clazz.foo(), bar]; +var [a, b] = [clazz.foo, bar]; diff --git a/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/array-unpack-optimisation/expected.js b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/array-unpack-optimisation/expected.js index e6b45f91cb..e26edabdaf 100644 --- a/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/array-unpack-optimisation/expected.js +++ b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/array-unpack-optimisation/expected.js @@ -25,3 +25,12 @@ var _ref5 = [].concat(babelHelpers.toConsumableArray(foo), [bar]); var a = _ref5[0]; var b = _ref5[1]; +var _ref6 = [foo(), bar]; +var a = _ref6[0]; +var b = _ref6[1]; +var _ref7 = [clazz.foo(), bar]; +var a = _ref7[0]; +var b = _ref7[1]; +var _ref8 = [clazz.foo, bar]; +var a = _ref8[0]; +var b = _ref8[1];