From 27ba4b2bba6d9c2caa7266a3706ef575e8539a23 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 16 Nov 2014 14:33:16 +1100 Subject: [PATCH] scope: switch over declaration building to info so we can build multiple things --- lib/6to5/traverse/scope.js | 41 ++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/6to5/traverse/scope.js b/lib/6to5/traverse/scope.js index 335fe9d942..420c656b8b 100644 --- a/lib/6to5/traverse/scope.js +++ b/lib/6to5/traverse/scope.js @@ -9,24 +9,31 @@ var FOR_KEYS = ["left", "init"]; function Scope(parent, block) { this.parent = parent; this.block = block; - this.ids = this.getIds(); - this.getIds(); + this.info = this.getInfo(); + this.declarations = this.info.declarations; + this.references = this.info.references; } -Scope.prototype.getIds = function () { +Scope.prototype.getInfo = function () { var block = this.block; - if (block._scopeIds) return block._scopeIds; + if (block._scope) return block._scope; var self = this; - var ids = block._scopeIds = {}; + var info = block._scope = { + declarations: {} + }; + + var add = function (node) { + self.addDeclaration(node, info.declarations); + }; // ForStatement - left, init if (t.isFor(block)) { _.each(FOR_KEYS, function (key) { var node = block[key]; - if (t.isLet(node)) self.add(node, ids); + if (t.isLet(node)) add(node); }); block = block.body; @@ -37,14 +44,14 @@ Scope.prototype.getIds = function () { if (t.isBlockStatement(block) || t.isProgram(block)) { _.each(block.body, function (node) { // check for non-var `VariableDeclaration`s - if (t.isLet(node)) self.add(node, ids); + if (t.isLet(node)) add(node); }); } // CatchClause - param if (t.isCatchClause(block)) { - self.add(block.param, ids); + add(block.param); } // Program, Function - var variables @@ -54,7 +61,7 @@ Scope.prototype.getIds = function () { if (t.isFor(node)) { _.each(FOR_KEYS, function (key) { var declar = node[key]; - if (t.isVar(declar)) self.add(declar, ids); + if (t.isVar(declar)) add(declar); }); } @@ -65,26 +72,26 @@ Scope.prototype.getIds = function () { // we've ran into a declaration! // we'll let the BlockStatement scope deal with `let` declarations if (t.isDeclaration(node) && !t.isLet(node)) { - self.add(node, ids); + add(node); } }); } - // Function - params + // Function - params, rest if (t.isFunction(block)) { - this.add(block.rest, ids); + add(block.rest); _.each(block.params, function (param) { - self.add(param, ids); + add(param); }); } - return ids; + return info; }; -Scope.prototype.add = function (node, ids) { +Scope.prototype.addDeclaration = function (node, declarations) { if (!node) return; - _.merge(ids || this.ids, t.getIds(node, true)); + _.merge(declarations || this.declarations, t.getIds(node, true)); }; Scope.prototype.get = function (id) { @@ -92,7 +99,7 @@ Scope.prototype.get = function (id) { }; Scope.prototype.getOwn = function (id) { - return _.has(this.ids, id) && this.ids[id]; + return _.has(this.declarations, id) && this.declarations[id]; }; Scope.prototype.parentGet = function (id) {