diff --git a/lib/6to5/templates/function-return-obj-this.js b/lib/6to5/templates/function-return-obj-this.js deleted file mode 100644 index 99b9dd6df4..0000000000 --- a/lib/6to5/templates/function-return-obj-this.js +++ /dev/null @@ -1,3 +0,0 @@ -(function (KEY) { - return KEY; -}).call(this, OBJECT) diff --git a/lib/6to5/transform.js b/lib/6to5/transform.js index bb2edefffa..2d5bfc4d60 100644 --- a/lib/6to5/transform.js +++ b/lib/6to5/transform.js @@ -11,7 +11,7 @@ function transform(code, opts) { code = (code || "") + ""; var file = new File(opts); - return util.parse(code); + return file.parse(code); } transform.test = function (task, assert) { diff --git a/lib/6to5/transformers/computed-property-names.js b/lib/6to5/transformers/computed-property-names.js index 967bbbd5a5..bba4e6da94 100644 --- a/lib/6to5/transformers/computed-property-names.js +++ b/lib/6to5/transformers/computed-property-names.js @@ -5,7 +5,6 @@ var _ = require("lodash"); exports.ObjectExpression = function (node, parent, file) { var hasComputed = false; - var hasThis = false; var computed = []; @@ -13,7 +12,6 @@ exports.ObjectExpression = function (node, parent, file) { if (prop.computed) { hasComputed = true; computed.unshift(prop); - hasThis = hasThis || traverse.hasType(prop, "ThisExpression"); return false; } else { return true; @@ -22,22 +20,32 @@ exports.ObjectExpression = function (node, parent, file) { if (!hasComputed) return; - var templateName = "function-return-obj"; - if (hasThis) templateName += "-this"; + var objIdNode; - var objId = b.identifier(file.generateUid("ref")); + if (parent.type === "AssignmentExpression") { + objIdNode = parent.left; + } else if (parent.type === "VariableDeclarator") { + objIdNode = parent.id; + } - var container = util.template(templateName, { + var objId = "ref"; + + if (objIdNode && objIdNode.type === "Identifier") { + objId = objIdNode.name; + } + + objId = b.identifier(file.generateUid(objId)); + + var container = util.template("function-return-obj", { KEY: objId, OBJECT: node }); - var containerBody; - if (templateName === "function-return-obj") { - containerBody = container.callee.body.body; - } else { - containerBody = container.callee.object.body.body; - } + var containerCallee = container.callee; + var containerBody = containerCallee.body.body; + + containerCallee._aliasFunction = true; + containerCallee._aliasFunctionStopNonArrowFunctions = true; _.each(computed, function (prop) { containerBody.unshift(util.template("obj-key-set", { diff --git a/test/fixtures/syntax/computed-property-names/argument/actual.js b/test/fixtures/syntax/computed-property-names/argument/actual.js new file mode 100644 index 0000000000..fc50c41c49 --- /dev/null +++ b/test/fixtures/syntax/computed-property-names/argument/actual.js @@ -0,0 +1,3 @@ +foo({ + [bar]: "foobar" +}); diff --git a/test/fixtures/syntax/computed-property-names/argument/expected.js b/test/fixtures/syntax/computed-property-names/argument/expected.js new file mode 100644 index 0000000000..9865ef7e60 --- /dev/null +++ b/test/fixtures/syntax/computed-property-names/argument/expected.js @@ -0,0 +1,4 @@ +foo(function(_ref) { + _ref[bar] = "foobar"; + return _ref; +}({})); diff --git a/test/fixtures/syntax/computed-property-names/assignment/actual.js b/test/fixtures/syntax/computed-property-names/assignment/actual.js new file mode 100644 index 0000000000..dd74ed9f04 --- /dev/null +++ b/test/fixtures/syntax/computed-property-names/assignment/actual.js @@ -0,0 +1,3 @@ +foo = { + [bar]: "foobar" +}; diff --git a/test/fixtures/syntax/computed-property-names/assignment/expected.js b/test/fixtures/syntax/computed-property-names/assignment/expected.js new file mode 100644 index 0000000000..c432b74583 --- /dev/null +++ b/test/fixtures/syntax/computed-property-names/assignment/expected.js @@ -0,0 +1,4 @@ +foo = function(_foo) { + _foo[bar] = "foobar"; + return _foo; +}({}); diff --git a/test/fixtures/syntax/computed-property-names/method/expected.js b/test/fixtures/syntax/computed-property-names/method/expected.js index e229295c5b..11bc369aff 100644 --- a/test/fixtures/syntax/computed-property-names/method/expected.js +++ b/test/fixtures/syntax/computed-property-names/method/expected.js @@ -1,6 +1,6 @@ -var obj = function (_ref) { - _ref[foobar] = function () { +var obj = function (_obj) { + _obj[foobar] = function () { return "foobar"; }; - return _ref; + return _obj; }({}); diff --git a/test/fixtures/syntax/computed-property-names/mixed/expected.js b/test/fixtures/syntax/computed-property-names/mixed/expected.js index 5553d0d9ff..f8fcc2f4d8 100644 --- a/test/fixtures/syntax/computed-property-names/mixed/expected.js +++ b/test/fixtures/syntax/computed-property-names/mixed/expected.js @@ -1,7 +1,7 @@ -var obj = function (_ref) { - _ref["x" + foo] = "heh"; - _ref["y" + bar] = "noo"; - return _ref; +var obj = function (_obj) { + _obj["x" + foo] = "heh"; + _obj["y" + bar] = "noo"; + return _obj; }({ foo: "foo", bar: "bar" diff --git a/test/fixtures/syntax/computed-property-names/multiple/expected.js b/test/fixtures/syntax/computed-property-names/multiple/expected.js index 81c11f23f2..10cf8cb14a 100644 --- a/test/fixtures/syntax/computed-property-names/multiple/expected.js +++ b/test/fixtures/syntax/computed-property-names/multiple/expected.js @@ -1,5 +1,5 @@ -var obj = function (_ref) { - _ref["x" + foo] = "heh"; - _ref["y" + bar] = "noo"; - return _ref; +var obj = function (_obj) { + _obj["x" + foo] = "heh"; + _obj["y" + bar] = "noo"; + return _obj; }({}); diff --git a/test/fixtures/syntax/computed-property-names/single/expected.js b/test/fixtures/syntax/computed-property-names/single/expected.js index 52a86de647..6d4a23a060 100644 --- a/test/fixtures/syntax/computed-property-names/single/expected.js +++ b/test/fixtures/syntax/computed-property-names/single/expected.js @@ -1,4 +1,4 @@ -var obj = function (_ref) { - _ref["x" + foo] = "heh"; - return _ref; +var obj = function (_obj) { + _obj["x" + foo] = "heh"; + return _obj; }({}); diff --git a/test/fixtures/syntax/computed-property-names/this/expected.js b/test/fixtures/syntax/computed-property-names/this/expected.js index 7d26e193a7..64bfd6da04 100644 --- a/test/fixtures/syntax/computed-property-names/this/expected.js +++ b/test/fixtures/syntax/computed-property-names/this/expected.js @@ -1,4 +1,5 @@ -var obj = function (_ref) { - _ref["x" + this.foo] = "heh"; - return _ref; -}.call(this, {}); +var _this = this; +var obj = function (_obj) { + _obj["x" + _this.foo] = "heh"; + return _obj; +}({}); diff --git a/test/fixtures/syntax/computed-property-names/variable/actual.js b/test/fixtures/syntax/computed-property-names/variable/actual.js new file mode 100644 index 0000000000..13d8e7f24a --- /dev/null +++ b/test/fixtures/syntax/computed-property-names/variable/actual.js @@ -0,0 +1,3 @@ +var foo = { + [bar]: "foobar" +}; diff --git a/test/fixtures/syntax/computed-property-names/variable/expected.js b/test/fixtures/syntax/computed-property-names/variable/expected.js new file mode 100644 index 0000000000..400286a528 --- /dev/null +++ b/test/fixtures/syntax/computed-property-names/variable/expected.js @@ -0,0 +1,4 @@ +var foo = function(_foo) { + _foo[bar] = "foobar"; + return _foo; +}({});