remove isDynamic checks and always execute those expressions **once**

This commit is contained in:
Sebastian McKenzie
2015-01-03 21:48:06 +11:00
parent 5a622ac9c7
commit d945379b5b
24 changed files with 238 additions and 164 deletions

View File

@@ -89,7 +89,7 @@ Class.prototype.run = function () {
this.closure = true;
// so we're only evaluating it once
var superRef = t.getUid(superName, this.file);
var superRef = this.scope.generateUidBasedOnNode(superName, this.file);
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(superRef, superName)
]));

View File

@@ -1,6 +1,6 @@
var t = require("../../types");
exports.ObjectExpression = function (node, parent, file) {
exports.ObjectExpression = function (node, parent, file, scope) {
var hasComputed = false;
var prop;
var key;
@@ -13,7 +13,7 @@ exports.ObjectExpression = function (node, parent, file) {
if (!hasComputed) return;
var objId = t.getUid(parent, file);
var objId = scope.generateUidBasedOnNode(parent, file);
var body = [];
var container = t.functionExpression(null, [], t.blockStatement(body));

View File

@@ -75,7 +75,7 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) {
var toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length);
var _parentId = t.getUid(parentId, opts.file, opts.scope);
var _parentId = opts.scope.generateUidBasedOnNode(parentId, opts.file);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(_parentId, toArray)
]));
@@ -113,7 +113,7 @@ var pushPattern = function (opts) {
var scope = opts.scope;
if (!t.isMemberExpression(parentId) && !t.isIdentifier(parentId)) {
var key = t.getUid(parentId, file, scope);
var key = scope.generateUidBasedOnNode(parentId, file);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(key, parentId)

View File

@@ -7,7 +7,7 @@ exports.Property = function (node, parent, file, scope) {
node.method = false;
var key = t.toComputedPropertyKey(node);
var key = t.toComputedKey(node, node.key);
if (!t.isLiteral(key)) return; // we can't set a function id with this
var id = t.toIdentifier(key.value);
@@ -46,7 +46,7 @@ exports.Property = function (node, parent, file, scope) {
}
};
exports.ObjectExpression = function (node, parent, file) {
exports.ObjectExpression = function (node, parent, file, scope) {
var mutatorMap = {};
var hasAny = false;
@@ -63,7 +63,7 @@ exports.ObjectExpression = function (node, parent, file) {
if (!hasAny) return;
if (node.properties.length) {
var objId = t.getUid(parent, file);
var objId = scope.generateUidBasedOnNode(parent, file);
return util.template("object-define-properties-closure", {
KEY: objId,

View File

@@ -77,16 +77,13 @@ exports.CallExpression = function (node, parent, file, scope) {
}
var callee = node.callee;
var temp;
if (t.isMemberExpression(callee)) {
contextLiteral = callee.object;
if (t.isDynamic(contextLiteral)) {
temp = contextLiteral = scope.generateTemp(file);
var temp = scope.generateTempBasedOnNode(callee.object, file);
if (temp) {
callee.object = t.assignmentExpression("=", temp, callee.object);
contextLiteral = temp;
}
t.appendToMemberExpression(callee, t.identifier("apply"));
} else {
node.callee = t.memberExpression(node.callee, t.identifier("apply"));

View File

@@ -30,10 +30,8 @@ exports.AssignmentExpression = function (node, parent, file, scope) {
// we need to return `node.right`
if (!t.isExpressionStatement(parent)) {
// `node.right` isn't a simple identifier so we need to reference it
if (t.isDynamic(value)) {
temp = value = scope.generateTemp(file);
}
temp = scope.generateTempBasedOnNode(node.right, file);
if (temp) value = temp;
}
if (node.operator !== "=") {
@@ -80,11 +78,7 @@ exports.CallExpression = function (node, parent, file, scope) {
var callee = node.callee;
if (!t.isVirtualPropertyExpression(callee)) return;
var temp;
if (t.isDynamic(callee.object)) {
// we need to save `callee.object` so we can call it again
temp = scope.generateTemp(file);
}
var temp = scope.generateTempBasedOnNode(callee.object, file);
var call = util.template("abstract-expression-call", {
PROPERTY: callee.property,

View File

@@ -5,7 +5,7 @@ var t = require("../../types");
exports.experimental = true;
var build = function (node, parent, file, scope) {
var uid = t.getUid(parent, file, scope);
var uid = scope.generateUidBasedOnNode(parent, file);
var container = util.template("array-comprehension-container", {
KEY: uid

View File

@@ -10,7 +10,7 @@ var getPropRef = function (nodes, prop, file, scope) {
if (t.isIdentifier(prop)) {
return t.literal(prop.name);
} else {
var temp = t.getUid(prop, file, scope);
var temp = scope.generateUidBasedOnNode(prop, file);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, prop)
]));
@@ -19,15 +19,11 @@ var getPropRef = function (nodes, prop, file, scope) {
};
var getObjRef = function (nodes, obj, file, scope) {
if (t.isDynamic(obj)) {
var temp = t.getUid(obj, file, scope);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, obj)
]));
return temp;
} else {
return obj;
}
var temp = scope.generateUidBasedOnNode(obj, file);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, obj)
]));
return temp;
};
var buildHasOwn = function (obj, prop, file) {

View File

@@ -5,10 +5,8 @@ exports.BindMemberExpression = function (node, parent, file, scope) {
var object = node.object;
var prop = node.property;
var temp;
if (t.isDynamic(object)) {
temp = object = scope.generateTemp(file);
}
var temp = scope.generateTempBasedOnNode(node.object, file);
if (temp) object = temp;
var call = t.callExpression(
t.memberExpression(t.memberExpression(object, prop), t.identifier("bind")),
@@ -33,16 +31,12 @@ exports.BindFunctionExpression = function (node, parent, file, scope) {
]));
};
if (_.find(node.arguments, t.isDynamic)) {
var temp = scope.generateTemp(file, "args");
var temp = scope.generateTemp(file, "args");
return t.sequenceExpression([
t.assignmentExpression("=", temp, t.arrayExpression(node.arguments)),
buildCall(node.arguments.map(function (node, i) {
return t.memberExpression(temp, t.literal(i), true);
}))
]);
} else {
return buildCall(node.arguments);
}
return t.sequenceExpression([
t.assignmentExpression("=", temp, t.arrayExpression(node.arguments)),
buildCall(node.arguments.map(function (node, i) {
return t.memberExpression(temp, t.literal(i), true);
}))
]);
};

View File

@@ -31,6 +31,13 @@ Scope.add = function (node, references) {
_.defaults(references, t.getIds(node, true));
};
/**
* Description
*
* @param {File} file
* @param {String} [name="temp"]
*/
Scope.prototype.generateTemp = function (file, name) {
var id = file.generateUidIdentifier(name || "temp", this);
this.push({
@@ -40,6 +47,83 @@ Scope.prototype.generateTemp = function (file, name) {
return id;
};
/*
* Description
*
* @param {Object} parent
* @param {File} file
* @param {Scope} scope
* @returns {Object}
*/
Scope.prototype.generateUidBasedOnNode = function (parent, file) {
var node = parent;
if (t.isAssignmentExpression(parent)) {
node = parent.left;
} else if (t.isVariableDeclarator(parent)) {
node = parent.id;
}
var id = "ref";
if (t.isProperty(node)) {
node = node.key;
}
if (t.isIdentifier(node)) {
id = node.name;
} else if (t.isLiteral(node)) {
id = node.value;
} else if (t.isMemberExpression(node)) {
var parts = [];
var add = function (node) {
if (t.isMemberExpression(node)) {
add(node.object);
add(node.property);
} else if (t.isIdentifier(node)) {
parts.push(node.name);
} else if (t.isLiteral(node)) {
parts.push(node.value);
}
};
add(node);
id = parts.join("$");
}
id = id.replace(/^_/, "");
return file.generateUidIdentifier(id, this);
};
/**
* Description
*
* @param {Object} node
* @param {File} file
* @returns {Object}
*/
Scope.prototype.generateTempBasedOnNode = function (node, file) {
if (!t.isIdentifier(node) && !t.isMemberExpression(node)) {
throw new TypeError("Invalid node type " + JSON.stringify(node.type) + " passed to Scope.prototype.generateTempBasedOnNode");
}
if (t.isIdentifier(node) && this.has(node.name, true)) {
return null;
}
var id = this.generateUidBasedOnNode(node, file);
this.push({
key: id.name,
id: id
});
return id;
};
Scope.prototype.getInfo = function () {
var block = this.block;
if (block._scopeInfo) return block._scopeInfo;
@@ -123,6 +207,12 @@ Scope.prototype.getInfo = function () {
return info;
};
/**
* Description
*
* @param {Object} opts
*/
Scope.prototype.push = function (opts) {
var block = this.block;
@@ -143,33 +233,84 @@ Scope.prototype.push = function (opts) {
}
};
/**
* Description
*
* @param {Object} node
*/
Scope.prototype.add = function (node) {
Scope.add(node, this.references);
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
*/
Scope.prototype.get = function (id, decl) {
return id && (this.getOwn(id, decl) || this.parentGet(id, decl));
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
*/
Scope.prototype.getOwn = function (id, decl) {
var refs = this.references;
if (decl) refs = this.declarations;
return _.has(refs, id) && refs[id];
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
*/
Scope.prototype.parentGet = function (id, decl) {
return this.parent && this.parent.get(id, decl);
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
* @returns {Boolean}
*/
Scope.prototype.has = function (id, decl) {
return (id && (this.hasOwn(id, decl) || this.parentHas(id, decl))) ||
_.contains(Scope.defaultDeclarations, id);
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
* @returns {Boolean}
*/
Scope.prototype.hasOwn = function (id, decl) {
return !!this.getOwn(id, decl);
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
* @returns {Boolean}
*/
Scope.prototype.parentHas = function (id, decl) {
return this.parent && this.parent.has(id, decl);
};

View File

@@ -71,9 +71,8 @@ _.each(t.FLIPPED_ALIAS_KEYS, function (types, type) {
* @returns {Object}
*/
t.toComputedPropertyKey = function (prop) {
var key = prop.key;
if (!prop.computed) {
t.toComputedKey = function (node, key) {
if (!node.computed) {
if (t.isIdentifier(key)) key = t.literal(key.name);
}
return key;
@@ -126,6 +125,10 @@ t.toSequenceExpression = function (nodes, scope) {
}
});
if (exprs.length === 1) {
return exprs[0]
}
return t.sequenceExpression(exprs);
};
@@ -174,25 +177,6 @@ t.prependToMemberExpression = function (member, append) {
return member;
};
/*
* Description
*
* @param {Object} node
* @returns {Boolean}
*/
t.isDynamic = function (node) {
if (t.isExpressionStatement(node)) {
return t.isDynamic(node.expression);
} else if (t.isIdentifier(node) || t.isLiteral(node) || t.isThisExpression(node)) {
return false;
} else if (t.isMemberExpression(node)) {
return t.isDynamic(node.object) || t.isDynamic(node.property);
} else {
return true;
}
};
/**
* Description
*
@@ -341,58 +325,6 @@ t.toBlock = function (node, parent) {
return t.blockStatement(node);
};
/*
* Description
*
* @param {Object} parent
* @param {File} file
* @param {Scope} scope
* @returns {Object}
*/
t.getUid = function (parent, file, scope) {
var node = parent;
if (t.isAssignmentExpression(parent)) {
node = parent.left;
} else if (t.isVariableDeclarator(parent)) {
node = parent.id;
}
var id = "ref";
if (t.isProperty(node)) {
node = node.key;
}
if (t.isIdentifier(node)) {
id = node.name;
} else if (t.isLiteral(node)) {
id = node.value;
} else if (t.isMemberExpression(node)) {
var parts = [];
var add = function (node) {
if (t.isMemberExpression(node)) {
add(node.object);
add(node.property);
} else if (t.isIdentifier(node)) {
parts.push(node.name);
} else if (t.isLiteral(node)) {
parts.push(node.value);
}
};
add(node);
id = parts.join("$");
}
id = id.replace(/^_/, "");
return file.generateUidIdentifier(id, scope);
};
/**
* Description
*