nicer automatic reference variables for comptued property names and property method assignment #54

This commit is contained in:
Sebastian McKenzie 2014-10-14 08:50:34 +11:00
parent 1f61e7675b
commit 6af0ffd97e
7 changed files with 45 additions and 36 deletions

View File

@ -19,21 +19,7 @@ exports.ObjectExpression = function (node, parent, file) {
if (!hasComputed) return; if (!hasComputed) return;
var objIdNode; var objId = util.getUid(parent, file);
if (parent.type === "AssignmentExpression") {
objIdNode = parent.left;
} else if (parent.type === "VariableDeclarator") {
objIdNode = parent.id;
}
var objId = "ref";
if (objIdNode && objIdNode.type === "Identifier") {
objId = objIdNode.name;
}
objId = b.identifier(file.generateUid(objId));
var container = util.template("function-return-obj", { var container = util.template("function-return-obj", {
KEY: objId, KEY: objId,

View File

@ -20,7 +20,7 @@ exports.ObjectExpression = function (node, parent, file) {
if (_.isEmpty(mutatorMap)) return; if (_.isEmpty(mutatorMap)) return;
var objId = b.identifier(file.generateUid("ref")); var objId = util.getUid(parent, file);
return util.template("object-define-properties-closure", { return util.template("object-define-properties-closure", {
KEY: objId, KEY: objId,

View File

@ -20,6 +20,24 @@ exports.ensureBlock = function (node) {
node.body = b.blockStatement(block); node.body = b.blockStatement(block);
}; };
exports.getUid = function (parent, file) {
var node;
if (parent.type === "AssignmentExpression") {
node = parent.left;
} else if (parent.type === "VariableDeclarator") {
node = parent.id;
}
var id = "ref";
if (node && node.type === "Identifier") {
id = node.name;
}
return b.identifier(file.generateUid(id));
};
exports.isAbsolute = function (loc) { exports.isAbsolute = function (loc) {
if (!loc) return false; if (!loc) return false;
if (loc[0] === "/") return true; // unix if (loc[0] === "/") return true; // unix
@ -245,6 +263,11 @@ exports.parse = function (opts, code, callback) {
} }
}; };
exports.parseNoProperties = function (loc, code) {
var ast = exports.parse({ filename: loc }, code).program;
return exports.removeProperties(ast);
};
try { try {
exports.templates = require("../../templates.json"); exports.templates = require("../../templates.json");
} catch (err) { } catch (err) {
@ -266,8 +289,6 @@ try {
var loc = templatesLoc + "/" + name; var loc = templatesLoc + "/" + name;
var code = fs.readFileSync(loc, "utf8"); var code = fs.readFileSync(loc, "utf8");
var ast = exports.parse({ filename: loc }, code).program; exports.templates[key] = exports.parseNoProperties(loc, code);
ast = exports.removeProperties(ast);
exports.templates[key] = ast;
}); });
} }

View File

@ -1,16 +1,18 @@
var _slice = Array.prototype.slice;
var Test = function(Foo) { var Test = function(Foo) {
var Test = function Test() { var Test = function Test() {
woops.super.test(); woops.super.test();
Foo.call(this); Foo.call(this);
Foo.prototype.test.call(this); Foo.prototype.test.call(this);
foob(Foo); foob(Foo);
Foo.call.apply(Foo, [this].concat(Array.prototype.slice.call(arguments))); Foo.call.apply(Foo, [this].concat(_slice.call(arguments)));
Foo.call.apply(Foo, [this, "test"].concat(Array.prototype.slice.call(arguments))); Foo.call.apply(Foo, [this, "test"].concat(_slice.call(arguments)));
Foo.prototype.test.call.apply(Foo.prototype, [this].concat(Array.prototype.slice.call(arguments))); Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_slice.call(arguments)));
Foo.prototype.test.call.apply( Foo.prototype.test.call.apply(
Foo.prototype, Foo.prototype,
[this, "test"].concat(Array.prototype.slice.call(arguments)) [this, "test"].concat(_slice.call(arguments))
); );
}; };
@ -31,11 +33,11 @@ var Test = function(Foo) {
value: function() { value: function() {
Foo.prototype.test.call(this); Foo.prototype.test.call(this);
Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(Array.prototype.slice.call(arguments))); Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_slice.call(arguments)));
Foo.prototype.test.call.apply( Foo.prototype.test.call.apply(
Foo.prototype.test, Foo.prototype.test,
[this, "test"].concat(Array.prototype.slice.call(arguments)) [this, "test"].concat(_slice.call(arguments))
); );
} }
} }
@ -47,8 +49,8 @@ var Test = function(Foo) {
value: function() { value: function() {
Foo.foo.call(this); Foo.foo.call(this);
Foo.foo.call.apply(Foo.foo, [this].concat(Array.prototype.slice.call(arguments))); Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments)));
Foo.foo.call.apply(Foo.foo, [this, "test"].concat(Array.prototype.slice.call(arguments))); Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments)));
} }
} }
}); });

View File

@ -1,5 +1,5 @@
var obj = function (_ref) { var obj = function (_obj) {
Object.defineProperties(_ref, { Object.defineProperties(_obj, {
foo: { foo: {
get: function () { get: function () {
return 5 + 5; return 5 + 5;
@ -9,5 +9,5 @@ var obj = function (_ref) {
} }
} }
}); });
return _ref; return _obj;
}({}); }({});

View File

@ -1,10 +1,10 @@
var obj = function (_ref) { var obj = function (_obj) {
Object.defineProperties(_ref, { Object.defineProperties(_obj, {
foo: { foo: {
get: function () { get: function () {
return 5 + 5; return 5 + 5;
} }
} }
}); });
return _ref; return _obj;
}({}); }({});

View File

@ -1,10 +1,10 @@
var obj = function (_ref) { var obj = function (_obj) {
Object.defineProperties(_ref, { Object.defineProperties(_obj, {
foo: { foo: {
set: function (value) { set: function (value) {
this._foo = value; this._foo = value;
} }
} }
}); });
return _ref; return _obj;
}({}); }({});