This commit is contained in:
@@ -6,24 +6,23 @@ var _ = require("lodash");
|
||||
|
||||
function Scope(parent, block) {
|
||||
this.parent = parent;
|
||||
this.block = block;
|
||||
this.ids = block._scopeIds = block._scopeIds || Scope.getIds(block);
|
||||
}
|
||||
|
||||
Scope.getIds = function (block) {
|
||||
var ids = [];
|
||||
var ids = {};
|
||||
|
||||
if (t.isBlockStatement(block)) {
|
||||
_.each(block.body, function (node) {
|
||||
if (t.isVariableDeclaration(node) && node.kind !== "var") {
|
||||
ids = ids.concat(t.getIds(node));
|
||||
ids = t.getIds(node, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (t.isProgram(block) || t.isFunction(block)) {
|
||||
} else if (t.isProgram(block) || t.isFunction(block)) {
|
||||
traverse(block, function (node) {
|
||||
if (t.isVariableDeclaration(node) && node.kind === "var") {
|
||||
ids = ids.concat(t.getIds(node));
|
||||
ids = t.getIds(node, true);
|
||||
} else if (t.isFunction(node)) {
|
||||
return false;
|
||||
}
|
||||
@@ -32,16 +31,33 @@ Scope.getIds = function (block) {
|
||||
|
||||
if (t.isFunction(block)) {
|
||||
_.each(block.params, function (param) {
|
||||
ids = ids.concat(t.getIds(param));
|
||||
_.merge(ids, t.getIds(param, true));
|
||||
});
|
||||
}
|
||||
|
||||
return ids;
|
||||
};
|
||||
|
||||
Scope.prototype.has = function (id, noParent) {
|
||||
if (!id) return false;
|
||||
if (_.contains(this.ids, id)) return true;
|
||||
if (noParent !== false && this.parent) return this.parent.has(id);
|
||||
return false;
|
||||
Scope.prototype.get = function (id) {
|
||||
return id && (this.getOwn(id) || this.parentGet(id));
|
||||
};
|
||||
|
||||
Scope.prototype.getOwn = function (id) {
|
||||
return _.has(this.ids, id) && this.ids[id];
|
||||
};
|
||||
|
||||
Scope.prototype.parentGet = function (id) {
|
||||
return this.parent && this.parent.get(id);
|
||||
};
|
||||
|
||||
Scope.prototype.has = function (id) {
|
||||
return id && (this.hasOwn(id) || this.parentHas(id));
|
||||
};
|
||||
|
||||
Scope.prototype.hasOwn = function (id) {
|
||||
return !!this.getOwn(id);
|
||||
};
|
||||
|
||||
Scope.prototype.parentHas = function (id) {
|
||||
return this.parent && this.parent.has(id);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user