diff --git a/lib/6to5/transformers/destructuring.js b/lib/6to5/transformers/destructuring.js index bb6e714e17..e1dfa703e5 100644 --- a/lib/6to5/transformers/destructuring.js +++ b/lib/6to5/transformers/destructuring.js @@ -6,6 +6,81 @@ var isPattern = function (id) { return id.type === "ArrayPattern" || id.type === "ObjectPattern"; }; + +var buildVariableAssign = function (kind, id, init) { + return b.variableDeclaration(kind, [ + b.variableDeclarator(id, init) + ]); +}; + +var push = function (kind, nodes, pattern, parentId) { + if (pattern.type === "ObjectPattern") { + pushObjectPattern(kind, nodes, pattern, parentId); + } else if (pattern.type === "ArrayPattern") { + pushArrayPattern(kind, nodes, pattern, parentId); + } +}; + +var pushObjectPattern = function (kind, nodes, pattern, parentId) { + _.each(pattern.properties, function (prop) { + var pattern2 = prop.value; + var patternId2 = b.memberExpression(parentId, prop.key, false); + + if (isPattern(pattern2)) { + push(kind, nodes, pattern2, patternId2); + } else { + nodes.push(buildVariableAssign(kind, pattern2, patternId2)); + } + }); +}; + +var pushArrayPattern = function (kind, nodes, pattern, parentId) { + _.each(pattern.elements, function (elem, i) { + if (!elem) return; + + var newPatternId = b.memberExpression(parentId, b.literal(i), true); + + if (elem.type === "Identifier") { + nodes.push(buildVariableAssign(kind, elem, newPatternId)); + } else { + push(kind, nodes, elem, newPatternId); + } + }); +}; + +var pushPattern = function (kind, nodes, pattern, parentId, generateUid) { + if (parentId.type !== "MemberExpression" && parentId.type !== "Identifier") { + var key = generateUid("ref"); + + nodes.push(util.template("variable-assign", { + KEY: key, + VALUE: parentId + }, true)); + + parentId = b.identifier(key); + } + + push(kind, nodes, pattern, parentId); +}; + +exports.FunctionDeclaration = +exports.FunctionExpression = function (node, parent, opts, generateUid) { + var block = node.body; + var nodes = []; + + node.params = node.params.map(function (pattern) { + if (!isPattern(pattern)) return pattern; + + var parentId = b.identifier(generateUid("ref")); + + pushPattern("var", nodes, pattern, parentId, generateUid); + + return parentId; + }); + + block.body = nodes.concat(block.body); +}; + exports.VariableDeclaration = function (node, parent, opts, generateUid) { var nodes = []; @@ -18,73 +93,13 @@ exports.VariableDeclaration = function (node, parent, opts, generateUid) { }); if (!hasPattern) return; - // - - var buildVariableAssign = function (id, init) { - return b.variableDeclaration(node.kind, [ - b.variableDeclarator(id, init) - ]); - }; - - var push = function (pattern, parentId) { - if (pattern.type === "ObjectPattern") { - pushObjectPattern(pattern, parentId); - } else if (pattern.type === "ArrayPattern") { - pushArrayPattern(pattern, parentId); - } - }; - - var pushObjectPattern = function (pattern, parentId) { - _.each(pattern.properties, function (prop) { - var id = prop.value; - - var init = b.memberExpression(parentId, prop.key, false); - - if (isPattern(id)) { - push(id, init); - } else { - nodes.push(buildVariableAssign(id, init)); - } - }); - }; - - var pushArrayPattern = function (pattern, parentId) { - _.each(pattern.elements, function (id, i) { - var init = b.memberExpression(parentId, b.literal(i), true); - - if (id.type === "Identifier") { - nodes.push(buildVariableAssign(id, init)); - } else { - push(id, init); - } - }); - }; - - var pushPattern = function (id, init) { - if (init.type !== "MemberExpression" && init.type !== "Identifier") { - var key = generateUid("ref"); - - nodes.push(util.template("variable-assign", { - KEY: key, - VALUE: init - }, true)); - - init = b.identifier(key); - } - - push(id, init); - }; - - // - _.each(node.declarations, function (declar) { - var init = declar.init; - var id = declar.id; - - if (isPattern(id)) { - pushPattern(id, init); + var patternId = declar.init; + var pattern = declar.id; + if (isPattern(pattern)) { + pushPattern(node.kind, nodes, pattern, patternId, generateUid); } else { - nodes.push(buildVariableAssign(id, init)); + nodes.push(buildVariableAssign(node.kind, declar.id, declar.init)); } }); diff --git a/test/fixtures/destructuring/parameters/actual.js b/test/fixtures/destructuring/parameters/actual.js new file mode 100644 index 0000000000..896a7ce416 --- /dev/null +++ b/test/fixtures/destructuring/parameters/actual.js @@ -0,0 +1,15 @@ +function somethingAdvanced({topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}}){ + +} + +function unpackObject({title: title, author: author}) { + return title + ' ' + author; +} + +console.log(unpackObject({title: 'title', author: 'author'})); + +var unpackArray = function ([a, b, c], [x, y, z]) { + return a+b+c; +}; + +console.log(unpackArray(['hello', ', ', 'world'], [1, 2, 3])); diff --git a/test/fixtures/destructuring/parameters/expected.js b/test/fixtures/destructuring/parameters/expected.js new file mode 100644 index 0000000000..3af19ef55b --- /dev/null +++ b/test/fixtures/destructuring/parameters/expected.js @@ -0,0 +1,28 @@ +function somethingAdvanced(_ref){ + var x1 = _ref.topLeft.x; + var y1 = _ref.topLeft.y; + var x2 = _ref.bottomRight.x; + var y2 = _ref.bottomRight.y; +} + +function unpackObject(_ref2) { + var title = _ref2.title; + var author = _ref2.author; + return title + ' ' + author; +} + +console.log(unpackObject({title: 'title', author: 'author'})); + +var unpackArray = function (_ref3, _ref4) { + var a = _ref3[0]; + var b = _ref3[1]; + var c = _ref3[2]; + + var x = _ref4[0]; + var y = _ref4[1]; + var z = _ref4[2]; + + return a + b + c; +}; + +console.log(unpackArray(['hello', ', ', 'world'], [1, 2, 3]));