add support for this in computed property names

This commit is contained in:
Sebastian McKenzie
2014-10-08 15:36:45 +11:00
parent 388f09fb5b
commit 577877b813
5 changed files with 29 additions and 7 deletions

View File

@@ -0,0 +1,3 @@
(function (obj) {
return obj;
}).call(this, OBJECT)

View File

@@ -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";
//

View File

@@ -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));