diff --git a/lib/6to5/traverse/scope.js b/lib/6to5/traverse/scope.js index 118af4b42a..1781ffca9d 100644 --- a/lib/6to5/traverse/scope.js +++ b/lib/6to5/traverse/scope.js @@ -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)); };