From 12104f822fceb688f9acad3cee679d1c8c6d407e Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 18 May 2015 22:44:40 +0100 Subject: [PATCH] add assignment pattern shorthand support to explode transformer - fixes #1566 --- .../transformation/transformers/internal/explode.js | 9 ++++++--- .../spec.function-name/shorthand-property/actual.js | 9 +++++++++ .../shorthand-property/expected.js | 13 +++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/babel/transformation/transformers/internal/explode.js b/src/babel/transformation/transformers/internal/explode.js index 374d88f022..9f11c013d8 100644 --- a/src/babel/transformation/transformers/internal/explode.js +++ b/src/babel/transformation/transformers/internal/explode.js @@ -5,9 +5,9 @@ export var metadata = { group: "builtin-setup" }; -function buildClone(bindingKey, refKey) { +function buildClone(bindingKey, refKey, check?) { return function (node) { - if (node[bindingKey] === node[refKey]) { + if (node[bindingKey] === node[refKey] || (check && check(node))) { node[refKey] = t.removeComments(clone(node[refKey])); } }; @@ -25,6 +25,9 @@ function buildListClone(listKey, bindingKey, refKey) { }; } -export var Property = buildClone("value", "key"); +export var Property = buildClone("value", "key", function (node) { + return t.isAssignmentPattern(node.value) && node.value.left === node.key; +}); + export var ExportDeclaration = buildListClone("specifiers", "local", "exported"); export var ImportDeclaration = buildListClone("specifiers", "local", "imported"); diff --git a/test/core/fixtures/transformation/spec.function-name/shorthand-property/actual.js b/test/core/fixtures/transformation/spec.function-name/shorthand-property/actual.js index 2c06014fa3..67ddae3d56 100644 --- a/test/core/fixtures/transformation/spec.function-name/shorthand-property/actual.js +++ b/test/core/fixtures/transformation/spec.function-name/shorthand-property/actual.js @@ -9,3 +9,12 @@ var bar = { get(arg, "baz"); } }; + +var f = function ({ foo = "bar" }) { + var obj = { + // same name as parameter + foo: function () { + foo; + } + }; +}; diff --git a/test/core/fixtures/transformation/spec.function-name/shorthand-property/expected.js b/test/core/fixtures/transformation/spec.function-name/shorthand-property/expected.js index 374fc7c053..fd89e7bebf 100644 --- a/test/core/fixtures/transformation/spec.function-name/shorthand-property/expected.js +++ b/test/core/fixtures/transformation/spec.function-name/shorthand-property/expected.js @@ -11,3 +11,16 @@ var bar = { _get(arg, "baz"); } }; + +var f = function f(_ref) { + var _ref$foo = _ref.foo; + + var _foo = _ref$foo === undefined ? "bar" : _ref$foo; + + var obj = { + // same name as parameter + foo: function foo() { + _foo; + } + }; +};