From 0a25618c34eb15b3ad254d86b6ed8c8e78544ebf Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 20 Nov 2014 16:53:22 +1100 Subject: [PATCH] collect references that haven't been declared in scope - fixes #173 and fixes #175 --- lib/6to5/traverse/scope.js | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/6to5/traverse/scope.js b/lib/6to5/traverse/scope.js index 6dd2433739..5851011487 100644 --- a/lib/6to5/traverse/scope.js +++ b/lib/6to5/traverse/scope.js @@ -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) {