DRY up isDynamic checks - add isDynamic check to spread - fixes #232

This commit is contained in:
Sebastian McKenzie
2014-12-05 10:53:46 +11:00
parent b5b175c45a
commit dae46bfbfa
4 changed files with 22 additions and 27 deletions

View File

@@ -56,7 +56,7 @@ exports.ArrayExpression = function (node, parent, file) {
return t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes);
};
exports.CallExpression = function (node, parent, file) {
exports.CallExpression = function (node, parent, file, scope) {
var args = node.arguments;
if (!hasSpread(args)) return;
@@ -79,10 +79,16 @@ exports.CallExpression = function (node, parent, file) {
}
var callee = node.callee;
var temp;
if (t.isMemberExpression(callee)) {
contextLiteral = callee.object;
if (t.isDynamic(contextLiteral)) {
temp = contextLiteral = scope.generateTemp(file);
callee.object = t.assignmentExpression("=", temp, callee.object);
}
if (callee.computed) {
callee.object = t.memberExpression(callee.object, callee.property, true);
callee.property = t.identifier("apply");

View File

@@ -30,12 +30,7 @@ exports.AssignmentExpression = function (node, parent, file, scope) {
if (!t.isExpressionStatement(parent)) {
// `node.right` isn't a simple identifier so we need to reference it
if (t.isDynamic(value)) {
var tempName = file.generateUid("temp");
temp = value = t.identifier(tempName);
scope.push({
key: tempName,
id: temp
});
temp = value = scope.generateTemp(file);
}
}
@@ -75,12 +70,7 @@ exports.CallExpression = function (node, parent, file, scope) {
var temp;
if (t.isDynamic(callee.object)) {
// we need to save `callee.object` so we can call it again
var tempName = file.generateUid("temp");
temp = t.identifier(tempName);
scope.push({
key: tempName,
id: temp
});
temp = scope.generateTemp(file);
}
var call = util.template("abstract-expression-call", {

View File

@@ -7,12 +7,7 @@ exports.BindMemberExpression = function (node, parent, file, scope) {
var temp;
if (t.isDynamic(object)) {
var tempName = file.generateUid("temp", scope);
temp = object = t.identifier(tempName);
scope.push({
key: tempName,
id: temp
});
temp = object = scope.generateTemp(file);
}
var call = t.callExpression(
@@ -39,17 +34,12 @@ exports.BindFunctionExpression = function (node, parent, file, scope) {
};
if (_.find(node.arguments, t.isDynamic)) {
var argsIdName = file.generateUid("args", scope);
var argsId = t.identifier(argsIdName);
scope.push({
key: argsIdName,
id: argsId
});
var temp = scope.generateTemp(file, "args");
return t.sequenceExpression([
t.assignmentExpression("=", argsId, t.arrayExpression(node.arguments)),
t.assignmentExpression("=", temp, t.arrayExpression(node.arguments)),
buildCall(node.arguments.map(function (node, i) {
return t.memberExpression(argsId, t.literal(i), true);
return t.memberExpression(temp, t.literal(i), true);
}))
]);
} else {

View File

@@ -26,6 +26,15 @@ Scope.add = function (node, references) {
_.defaults(references, t.getIds(node, true));
};
Scope.prototype.generateTemp = function (file, name) {
var id = file.generateUidIdentifier(name || "temp", this);
this.push({
key: id.name,
id: id
});
return id;
};
Scope.prototype.getReferences = function () {
var block = this.block;
if (block._scopeReferences) return block._scopeReferences;