diff --git a/lib/6to5/transformation/transformers/es6/destructuring.js b/lib/6to5/transformation/transformers/es6/destructuring.js index 7e26902dd3..ba3cd553df 100644 --- a/lib/6to5/transformation/transformers/es6/destructuring.js +++ b/lib/6to5/transformation/transformers/es6/destructuring.js @@ -179,21 +179,40 @@ Destructuring.prototype.init = function (pattern, parentId) { exports.ForInStatement = exports.ForOfStatement = function (node, parent, scope, file) { - var declar = node.left; - if (!t.isVariableDeclaration(declar)) return; + var left = node.left; - var pattern = declar.declarations[0].id; + if (t.isPattern(left)) { + // for ({ length: k } in { abc: 3 }); + + var temp = scope.generateUidIdentifier("ref"); + + node.left = t.variableDeclaration("var", [ + t.variableDeclarator(temp) + ]); + + t.ensureBlock(node); + + node.body.body.unshift(t.variableDeclaration("var", [ + t.variableDeclarator(left, temp) + ])); + + return; + } + + if (!t.isVariableDeclaration(left)) return; + + var pattern = left.declarations[0].id; if (!t.isPattern(pattern)) return; var key = scope.generateUidIdentifier("ref"); - node.left = t.variableDeclaration(declar.kind, [ + node.left = t.variableDeclaration(left.kind, [ t.variableDeclarator(key, null) ]); var nodes = []; var destructuring = new Destructuring({ - kind: declar.kind, + kind: left.kind, file: file, scope: scope, nodes: nodes @@ -248,7 +267,7 @@ exports.CatchClause = function (node, parent, scope, file) { var nodes = []; var destructuring = new Destructuring({ - kind: "var", + kind: "let", file: file, scope: scope, nodes: nodes @@ -256,6 +275,8 @@ exports.CatchClause = function (node, parent, scope, file) { destructuring.init(pattern, ref); node.body.body = nodes.concat(node.body.body); + + return node; }; exports.ExpressionStatement = function (node, parent, scope, file) {