From 8a0317132e1c4a2b7a5b274424c55106cd628e9f Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 12 Apr 2015 20:59:17 -0700 Subject: [PATCH] deopt array unpack optimisation on member expressions - fixes #1241 --- .../transformation/transformers/es6/destructuring.js | 9 +++++++-- .../array-unpack-optimisation/actual.js | 1 + .../array-unpack-optimisation/expected.js | 4 ++++ .../es6.destructuring/member-expression/expected.js | 5 +++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/babel/transformation/transformers/es6/destructuring.js b/src/babel/transformation/transformers/es6/destructuring.js index 3d774451d7..f67714d4bf 100644 --- a/src/babel/transformation/transformers/es6/destructuring.js +++ b/src/babel/transformation/transformers/es6/destructuring.js @@ -403,9 +403,14 @@ class DestructuringTransformer { if (pattern.elements.length > arr.elements.length) return; if (pattern.elements.length < arr.elements.length && !hasRest(pattern)) return false; - // deopt on holes for (var i = 0; i < pattern.elements.length; i++) { - if (!pattern.elements[i]) return false; + var elem = pattern.elements[i]; + + // deopt on holes + if (!elem) return false; + + // deopt on member expressions + if (t.isMemberExpression(elem)) return false; } // deopt on reference to left side identifiers diff --git a/test/core/fixtures/transformation/es6.destructuring/array-unpack-optimisation/actual.js b/test/core/fixtures/transformation/es6.destructuring/array-unpack-optimisation/actual.js index 51454e64c4..98df044d89 100644 --- a/test/core/fixtures/transformation/es6.destructuring/array-unpack-optimisation/actual.js +++ b/test/core/fixtures/transformation/es6.destructuring/array-unpack-optimisation/actual.js @@ -8,3 +8,4 @@ var [[a, b, ...c]] = [[1, 2, 3, 4]]; var [a, b] = [1, 2, 3]; var [[a, b]] = [[1, 2, 3]]; var [a, b] = [a, b]; +[a[0], a[1]] = [a[1], a[0]]; diff --git a/test/core/fixtures/transformation/es6.destructuring/array-unpack-optimisation/expected.js b/test/core/fixtures/transformation/es6.destructuring/array-unpack-optimisation/expected.js index 1ba95f1de6..7179137806 100644 --- a/test/core/fixtures/transformation/es6.destructuring/array-unpack-optimisation/expected.js +++ b/test/core/fixtures/transformation/es6.destructuring/array-unpack-optimisation/expected.js @@ -22,3 +22,7 @@ var b = _ref2[1]; var _ref3 = [a, b]; var a = _ref3[0]; var b = _ref3[1]; +var _temp = [a[1], a[0]]; +a[0] = _temp[0]; +a[1] = _temp[1]; +_temp; diff --git a/test/core/fixtures/transformation/es6.destructuring/member-expression/expected.js b/test/core/fixtures/transformation/es6.destructuring/member-expression/expected.js index a61a5b6377..c371508b05 100644 --- a/test/core/fixtures/transformation/es6.destructuring/member-expression/expected.js +++ b/test/core/fixtures/transformation/es6.destructuring/member-expression/expected.js @@ -1,6 +1,7 @@ "use strict"; -foo.foo = 1; -foo.bar = 2; +var _ref = [1, 2]; +foo.foo = _ref[0]; +foo.bar = _ref[1]; ;