From 3e2d61170773b5735f42c2ea9dcdfb38d3e5e403 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 22 Jan 2015 10:12:03 +1100 Subject: [PATCH] store all undeclared references on Program to better handle let scoping --- lib/6to5/traverse/scope.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/6to5/traverse/scope.js b/lib/6to5/traverse/scope.js index f22b5a0ecf..f749a5a88a 100644 --- a/lib/6to5/traverse/scope.js +++ b/lib/6to5/traverse/scope.js @@ -156,10 +156,6 @@ var functionVariableVisitor = { // function identifier doesn't belong to this scope if (state.blockId && node === state.blockId) return; - if (t.isIdentifier(node) && t.isReferenced(node, parent) && !scope.has(node.name)) { - state.add(node, true); - } - // we've ran into a declaration! // we'll let the BlockStatement scope deal with `let` declarations unless if (t.isDeclaration(node) && !t.isBlockScoped(node)) { @@ -168,6 +164,14 @@ var functionVariableVisitor = { } }; +var programReferenceVisitor = { + enter: function (node, parent, scope, context, add) { + if (t.isReferencedIdentifier(node, parent) && !scope.has(node.name)) { + add(node, true); + } + } +}; + var blockVariableVisitor = { enter: function (node, parent, scope, context, add) { if (t.isBlockScoped(node)) { @@ -243,6 +247,12 @@ Scope.prototype.getInfo = function () { }); } + // Program + + if (t.isProgram(block)) { + traverse(block, programReferenceVisitor, this, add); + } + // Function - params, rest if (t.isFunction(block)) {