add support for this in computed property names
This commit is contained in:
3
lib/6to5/templates/function-return-obj-this.js
Normal file
3
lib/6to5/templates/function-return-obj-this.js
Normal file
@@ -0,0 +1,3 @@
|
||||
(function (obj) {
|
||||
return obj;
|
||||
}).call(this, OBJECT)
|
||||
@@ -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";
|
||||
|
||||
//
|
||||
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user