traverse: clean up scope getIds building

This commit is contained in:
Sebastian McKenzie 2014-11-09 12:07:14 +11:00
parent 0b86a2fef8
commit cd9e289dee

View File

@ -7,16 +7,22 @@ var _ = require("lodash");
function Scope(parent, block) {
this.parent = parent;
this.block = block;
this.ids = block._scopeIds = block._scopeIds || Scope.getIds(block);
this.ids = this.getIds();
this.getIds();
}
Scope.getIds = function (block) {
var ids = {};
Scope.prototype.getIds = function () {
var block = this.block;
if (block._scopeIds) return block._scopeIds;
var self = this;
var ids = block._scopeIds = {};
if (t.isBlockStatement(block)) {
_.each(block.body, function (node) {
if (t.isVariableDeclaration(node) && node.kind !== "var") {
_.merge(ids, t.getIds(node, true));
self.add(node, ids);
}
});
} else if (t.isProgram(block) || t.isFunction(block)) {
@ -26,7 +32,7 @@ Scope.getIds = function (block) {
}
if (t.isDeclaration(node)) {
_.merge(ids, t.getIds(node, true));
self.add(node, ids);
} else if (t.isFunction(node)) {
return false;
}
@ -35,13 +41,17 @@ Scope.getIds = function (block) {
if (t.isFunction(block)) {
_.each(block.params, function (param) {
_.merge(ids, t.getIds(param, true));
self.add(param, ids);
});
}
return ids;
};
Scope.prototype.add = function (node, ids) {
_.merge(ids || this.ids, t.getIds(node, true));
};
Scope.prototype.get = function (id) {
return id && (this.getOwn(id) || this.parentGet(id));
};