|
|
|
|
@@ -6,25 +6,30 @@ var _ = require("lodash");
|
|
|
|
|
|
|
|
|
|
var FOR_KEYS = ["left", "init"];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This searches the current "scope" and collects all references/declarations
|
|
|
|
|
* within.
|
|
|
|
|
*
|
|
|
|
|
* @param {Scope} [parent]
|
|
|
|
|
* @param {Node} block
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function Scope(parent, block) {
|
|
|
|
|
this.parent = parent;
|
|
|
|
|
this.block = block;
|
|
|
|
|
|
|
|
|
|
this.info = this.getInfo();
|
|
|
|
|
this.declarations = this.info.declarations;
|
|
|
|
|
this.references = this.getReferences();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Scope.prototype.getInfo = function () {
|
|
|
|
|
Scope.prototype.getReferences = function () {
|
|
|
|
|
var block = this.block;
|
|
|
|
|
if (block._scope) return block._scope;
|
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
var info = block._scope = {
|
|
|
|
|
declarations: {}
|
|
|
|
|
};
|
|
|
|
|
var self = this;
|
|
|
|
|
var references = block._scope = {};
|
|
|
|
|
|
|
|
|
|
var add = function (node) {
|
|
|
|
|
self.addDeclaration(node, info.declarations);
|
|
|
|
|
self.add(node, references);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ForStatement - left, init
|
|
|
|
|
@@ -56,7 +61,7 @@ Scope.prototype.getInfo = function () {
|
|
|
|
|
// Program, Function - var variables
|
|
|
|
|
|
|
|
|
|
if (t.isProgram(block) || t.isFunction(block)) {
|
|
|
|
|
traverse(block, function (node) {
|
|
|
|
|
traverse(block, function (node, parent, scope) {
|
|
|
|
|
if (t.isFor(node)) {
|
|
|
|
|
_.each(FOR_KEYS, function (key) {
|
|
|
|
|
var declar = node[key];
|
|
|
|
|
@@ -68,12 +73,16 @@ Scope.prototype.getInfo = function () {
|
|
|
|
|
// declared within are accessible
|
|
|
|
|
if (t.isFunction(node)) return false;
|
|
|
|
|
|
|
|
|
|
if (t.isIdentifier(node) && t.isReferenced(node, parent) && !scope.has(node.name)) {
|
|
|
|
|
add(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we've ran into a declaration!
|
|
|
|
|
// we'll let the BlockStatement scope deal with `let` declarations
|
|
|
|
|
if (t.isDeclaration(node) && !t.isLet(node)) {
|
|
|
|
|
add(node);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, { scope: this });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Function - params, rest
|
|
|
|
|
@@ -85,12 +94,12 @@ Scope.prototype.getInfo = function () {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
return references;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Scope.prototype.addDeclaration = function (node, declarations) {
|
|
|
|
|
Scope.prototype.add = function (node, references) {
|
|
|
|
|
if (!node) return;
|
|
|
|
|
_.merge(declarations || this.declarations, t.getIds(node, true));
|
|
|
|
|
_.merge(references || this.references, t.getIds(node, true));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Scope.prototype.get = function (id) {
|
|
|
|
|
@@ -98,7 +107,7 @@ Scope.prototype.get = function (id) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Scope.prototype.getOwn = function (id) {
|
|
|
|
|
return _.has(this.declarations, id) && this.declarations[id];
|
|
|
|
|
return _.has(this.references, id) && this.references[id];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Scope.prototype.parentGet = function (id) {
|
|
|
|
|
|