diff --git a/lib/6to5/templates/function-return-obj-this.js b/lib/6to5/templates/function-return-obj-this.js new file mode 100644 index 0000000000..0a69bc5022 --- /dev/null +++ b/lib/6to5/templates/function-return-obj-this.js @@ -0,0 +1,3 @@ +(function (obj) { + return obj; +}).call(this, OBJECT) diff --git a/lib/6to5/transformers/block-binding.js b/lib/6to5/transformers/block-binding.js index 6edca44a2f..7a6c44b724 100644 --- a/lib/6to5/transformers/block-binding.js +++ b/lib/6to5/transformers/block-binding.js @@ -117,9 +117,7 @@ var buildNode = function (node) { var func = b.functionExpression(null, [], block, false); var templateName = "function-call"; - if (traverse.hasType(node, "ThisExpression")) { - templateName = "function-call-this"; - } + if (traverse.hasType(node, "ThisExpression")) templateName += "-this"; // diff --git a/lib/6to5/transformers/computed-property-names.js b/lib/6to5/transformers/computed-property-names.js index df85c79869..7d931f35b6 100644 --- a/lib/6to5/transformers/computed-property-names.js +++ b/lib/6to5/transformers/computed-property-names.js @@ -1,14 +1,18 @@ -var util = require("../util"); -var _ = require("lodash"); +var traverse = require("../traverse"); +var util = require("../util"); +var _ = require("lodash"); exports.ObjectExpression = function (node) { var hasComputed = false; + var hasThis = false; + var computed = []; node.properties = node.properties.filter(function (prop) { if (prop.computed) { hasComputed = true; computed.unshift(prop); + if (!hasThis) hasThis = traverse.hasType(prop, "ThisExpression"); return false; } else { return true; @@ -17,12 +21,22 @@ exports.ObjectExpression = function (node) { if (!hasComputed) return; - var container = util.template("function-return-obj", { + var templateName = "function-return-obj"; + if (hasThis) templateName += "-this"; + + var container = util.template(templateName, { OBJECT: node }); + var containerBody; + if (templateName === "function-return-obj") { + containerBody = container.callee.body.body; + } else { + containerBody = container.callee.object.body.body; + } + _.each(computed, function (prop) { - container.callee.body.body.unshift(util.template("obj-key-set", { + containerBody.unshift(util.template("obj-key-set", { KEY: prop.key, VALUE: prop.value }, true)); diff --git a/test/fixtures/computed-property-names/this/actual.js b/test/fixtures/computed-property-names/this/actual.js new file mode 100644 index 0000000000..3e3a34ef12 --- /dev/null +++ b/test/fixtures/computed-property-names/this/actual.js @@ -0,0 +1,3 @@ +var obj = { + ["x" + this.foo]: "heh" +}; diff --git a/test/fixtures/computed-property-names/this/expected.js b/test/fixtures/computed-property-names/this/expected.js new file mode 100644 index 0000000000..e1aee4c417 --- /dev/null +++ b/test/fixtures/computed-property-names/this/expected.js @@ -0,0 +1,4 @@ +var obj = function (obj) { + obj["x" + this.foo] = "heh"; + return obj; +}.call(this, {});