From 04bd0237875f95a33995d40b03307ca910b610d0 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 24 Nov 2014 00:56:06 +1100 Subject: [PATCH] add support for AssignmentExpression destructuring outside of ExpressionStatement --- .../transformers/es6-destructuring.js | 34 +++++++++++++++++-- .../actual.js | 1 - .../options.json | 3 -- .../assignment-expression/expected.js | 2 ++ .../assignment-expression/options.json | 3 -- 5 files changed, 34 insertions(+), 9 deletions(-) delete mode 100644 test/fixtures/transformation/es6-destructuring/assignment-expression-statement-only/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring/assignment-expression-statement-only/options.json create mode 100644 test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring/assignment-expression/options.json diff --git a/lib/6to5/transformation/transformers/es6-destructuring.js b/lib/6to5/transformation/transformers/es6-destructuring.js index 15c90b4171..3747bc95b2 100644 --- a/lib/6to5/transformation/transformers/es6-destructuring.js +++ b/lib/6to5/transformation/transformers/es6-destructuring.js @@ -1,3 +1,5 @@ +// TODO: Clean up + var t = require("../../types"); var _ = require("lodash"); @@ -183,10 +185,38 @@ exports.ExpressionStatement = function (node, parent, file, scope) { return nodes; }; -exports.AssignmentExpression = function (node, parent, file) { +exports.AssignmentExpression = function (node, parent, file, scope) { if (parent.type === "ExpressionStatement") return; if (!t.isPattern(node.left)) return; - throw file.errorWithNode(node, "AssignmentExpression destructuring outside of a ExpressionStatement is forbidden due to current 6to5 limitations"); + + var tempName = file.generateUid("temp", scope); + var temp = t.identifier(tempName); + scope.push(tempName, temp); + + var nodes = []; + nodes.push(t.assignmentExpression("=", temp, node.right)); + + push({ + kind: false, + file: file, + scope: scope + }, nodes, node.left, temp); + + nodes.push(temp); + + nodes = nodes.map(function (node) { + if (t.isExpressionStatement(node)) { + return node.expression; + } else if (t.isVariableDeclaration(node)) { + var declar = node.declarations[0]; + scope.push(declar.id.name, declar.id); + return t.assignmentExpression("=", declar.id, declar.init); + } else { + return node; + } + }); + + return t.sequenceExpression(nodes); }; exports.VariableDeclaration = function (node, parent, file, scope) { diff --git a/test/fixtures/transformation/es6-destructuring/assignment-expression-statement-only/actual.js b/test/fixtures/transformation/es6-destructuring/assignment-expression-statement-only/actual.js deleted file mode 100644 index e5a17f6ca2..0000000000 --- a/test/fixtures/transformation/es6-destructuring/assignment-expression-statement-only/actual.js +++ /dev/null @@ -1 +0,0 @@ -console.log([x] = [1]); diff --git a/test/fixtures/transformation/es6-destructuring/assignment-expression-statement-only/options.json b/test/fixtures/transformation/es6-destructuring/assignment-expression-statement-only/options.json deleted file mode 100644 index 56380df8e7..0000000000 --- a/test/fixtures/transformation/es6-destructuring/assignment-expression-statement-only/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "AssignmentExpression destructuring outside of a ExpressionStatement is forbidden due to current 6to5 limitations" -} diff --git a/test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js b/test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js new file mode 100644 index 0000000000..85c8a9ff0e --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js @@ -0,0 +1,2 @@ +var _temp; +console.log((_temp = [1, 2, 3], x = _temp[0], _temp)); diff --git a/test/fixtures/transformation/es6-destructuring/assignment-expression/options.json b/test/fixtures/transformation/es6-destructuring/assignment-expression/options.json deleted file mode 100644 index 56380df8e7..0000000000 --- a/test/fixtures/transformation/es6-destructuring/assignment-expression/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "AssignmentExpression destructuring outside of a ExpressionStatement is forbidden due to current 6to5 limitations" -}