inherit temporary computed property closure name from AssignmentExpressions and VariableDeclarators - #56
This commit is contained in:
parent
c28e78ecff
commit
f7a84c36cd
@ -1,3 +0,0 @@
|
||||
(function (KEY) {
|
||||
return KEY;
|
||||
}).call(this, OBJECT)
|
||||
@ -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) {
|
||||
|
||||
@ -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", {
|
||||
|
||||
3
test/fixtures/syntax/computed-property-names/argument/actual.js
vendored
Normal file
3
test/fixtures/syntax/computed-property-names/argument/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
foo({
|
||||
[bar]: "foobar"
|
||||
});
|
||||
4
test/fixtures/syntax/computed-property-names/argument/expected.js
vendored
Normal file
4
test/fixtures/syntax/computed-property-names/argument/expected.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
foo(function(_ref) {
|
||||
_ref[bar] = "foobar";
|
||||
return _ref;
|
||||
}({}));
|
||||
3
test/fixtures/syntax/computed-property-names/assignment/actual.js
vendored
Normal file
3
test/fixtures/syntax/computed-property-names/assignment/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
foo = {
|
||||
[bar]: "foobar"
|
||||
};
|
||||
4
test/fixtures/syntax/computed-property-names/assignment/expected.js
vendored
Normal file
4
test/fixtures/syntax/computed-property-names/assignment/expected.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
foo = function(_foo) {
|
||||
_foo[bar] = "foobar";
|
||||
return _foo;
|
||||
}({});
|
||||
@ -1,6 +1,6 @@
|
||||
var obj = function (_ref) {
|
||||
_ref[foobar] = function () {
|
||||
var obj = function (_obj) {
|
||||
_obj[foobar] = function () {
|
||||
return "foobar";
|
||||
};
|
||||
return _ref;
|
||||
return _obj;
|
||||
}({});
|
||||
|
||||
@ -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"
|
||||
|
||||
@ -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;
|
||||
}({});
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
var obj = function (_ref) {
|
||||
_ref["x" + foo] = "heh";
|
||||
return _ref;
|
||||
var obj = function (_obj) {
|
||||
_obj["x" + foo] = "heh";
|
||||
return _obj;
|
||||
}({});
|
||||
|
||||
@ -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;
|
||||
}({});
|
||||
|
||||
3
test/fixtures/syntax/computed-property-names/variable/actual.js
vendored
Normal file
3
test/fixtures/syntax/computed-property-names/variable/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = {
|
||||
[bar]: "foobar"
|
||||
};
|
||||
4
test/fixtures/syntax/computed-property-names/variable/expected.js
vendored
Normal file
4
test/fixtures/syntax/computed-property-names/variable/expected.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
var foo = function(_foo) {
|
||||
_foo[bar] = "foobar";
|
||||
return _foo;
|
||||
}({});
|
||||
Loading…
x
Reference in New Issue
Block a user