cache scope ids and limit id variable declarations to kind

This commit is contained in:
Sebastian McKenzie 2014-11-04 12:56:37 +11:00
parent 56271efada
commit 71d87f5b97

View File

@ -6,12 +6,15 @@ var _ = require("lodash");
function Scope(parent, block) {
this.parent = parent;
this.ids = block._scopeIds = block._scopeIds || Scope.getIds(block);
}
Scope.getIds = function (block) {
var ids = [];
if (t.isBlockStatement(block)) {
_.each(block.body, function (node) {
if (t.isVariableDeclaration(node)) {
if (t.isVariableDeclaration(node) && node.kind !== "var") {
ids = ids.concat(t.getIds(node));
}
});
@ -19,7 +22,7 @@ function Scope(parent, block) {
if (t.isProgram(block) || t.isFunction(block)) {
traverse(block, function (node) {
if (t.isVariableDeclaration(node)) {
if (t.isVariableDeclaration(node) && node.kind === "var") {
ids = ids.concat(t.getIds(node));
} else if (t.isFunction(node)) {
return false;
@ -33,12 +36,12 @@ function Scope(parent, block) {
});
}
this.ids = ids;
}
return ids;
};
Scope.prototype.has = function (id) {
Scope.prototype.has = function (id, noParent) {
if (!id) return false;
if (_.contains(this.ids, id)) return true;
if (this.parent) return this.parent.has(id);
if (noParent !== false && this.parent) return this.parent.has(id);
return false;
};