From 83487f22a7f5d421120315a9a322aee6453f4ba9 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 9 Oct 2014 22:41:12 +1100 Subject: [PATCH] support destructuring in For*Statements --- lib/6to5/transform.js | 2 +- lib/6to5/transformers/destructuring.js | 21 ++++++++++++++++ lib/6to5/transformers/for-of.js | 25 ++++++------------- .../fixtures/destructuring/for-in/expected.js | 5 ++-- .../fixtures/destructuring/for-of/expected.js | 7 +++--- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/lib/6to5/transform.js b/lib/6to5/transform.js index e23dd7f718..79bbe6b70e 100644 --- a/lib/6to5/transform.js +++ b/lib/6to5/transform.js @@ -117,7 +117,7 @@ transform.transformers = { generators: require("./transformers/generators"), blockBinding: require("./transformers/block-binding"), restParameters: require("./transformers/rest-parameters"), - forOf: require("./transformers/for-of"), destructuring: require("./transformers/destructuring"), + forOf: require("./transformers/for-of"), unicodeRegex: require("./transformers/unicode-regex") }; diff --git a/lib/6to5/transformers/destructuring.js b/lib/6to5/transformers/destructuring.js index bbc7de07f0..0f865fcbe6 100644 --- a/lib/6to5/transformers/destructuring.js +++ b/lib/6to5/transformers/destructuring.js @@ -62,6 +62,27 @@ var pushPattern = function (kind, nodes, pattern, parentId, generateUid) { push(kind, nodes, pattern, parentId); }; +exports.ForInStatement = +exports.ForOfStatement = function (node, parent, opts, generateUid) { + var declar = node.left; + if (declar.type !== "VariableDeclaration") return; + + var pattern = declar.declarations[0].id; + if (!util.isPattern(pattern)) return; + + var key = b.identifier(generateUid("ref")); + node.left = b.variableDeclaration(declar.kind, [ + b.variableDeclarator(key, null) + ]); + + var nodes = []; + + push(declar.kind, nodes, pattern, key); + + var block = node.body; + block.body = nodes.concat(block.body); +}; + exports.FunctionDeclaration = exports.FunctionExpression = function (node, parent, opts, generateUid) { var block = node.body; diff --git a/lib/6to5/transformers/for-of.js b/lib/6to5/transformers/for-of.js index 26a3937ce1..f17a619727 100644 --- a/lib/6to5/transformers/for-of.js +++ b/lib/6to5/transformers/for-of.js @@ -2,17 +2,15 @@ var util = require("../util"); var b = require("ast-types").builders; exports.ForOfStatement = function (node, parent, opts, generateUid) { - var declar; - var isPattern = false; - + var left = node.left; var key; - if (node.left.type === "VariableDeclaration") { - declar = node.left; - key = declar.declarations[0].id; + + if (left.type === "Identifier") { + key = left; + } else if (left.type === "VariableDeclaration") { + key = left.declarations[0].id; } else { - isPattern = true; - declar = node.left[0]; - key = b.identifier(generateUid("ref")); + return; } var node2 = util.template("for-of", { @@ -22,14 +20,7 @@ exports.ForOfStatement = function (node, parent, opts, generateUid) { KEY: key }); - var block = node2.body; - - if (isPattern) { - block.body.push(b.variableDeclaration(declar.kind, [ - b.variableDeclarator(declar.declarations[0].id, key) - ])); - } - + var block = node2.body; block.body = block.body.concat(node.body.body || []); var declar = block.body[0]; diff --git a/test/fixtures/destructuring/for-in/expected.js b/test/fixtures/destructuring/for-in/expected.js index b4d357111f..f07723cb59 100644 --- a/test/fixtures/destructuring/for-in/expected.js +++ b/test/fixtures/destructuring/for-in/expected.js @@ -1,4 +1,5 @@ -for (var name in obj) { - var value = obj[name]; +for (var _ref in obj) { + var name = _ref[0]; + var value = _ref[1]; print("Name: " + name + ", Value: " + value); } diff --git a/test/fixtures/destructuring/for-of/expected.js b/test/fixtures/destructuring/for-of/expected.js index 5f0004fccd..3ba44b3480 100644 --- a/test/fixtures/destructuring/for-of/expected.js +++ b/test/fixtures/destructuring/for-of/expected.js @@ -1,5 +1,6 @@ for (var _iterator = this.test.expectation.registers[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var name = _step.value[0]; - var before = _step.value[1]; - var after = _step.value[2]; + var _ref = _step.value; + var name = _ref[0]; + var before = _ref[1]; + var after = _ref[2]; }