diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 9cdb87b108..f6b2286ee9 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -325,7 +325,7 @@ File.prototype.transform = function (ast) { this.ast = ast; this.lastStatements = t.getLastStatements(ast.program); - this.scope = new Scope(ast.program, null, this); + this.scope = new Scope(ast.program, ast, null, this); this.moduleFormatter = this.getModuleFormatter(this.opts.modules); var astRun = function (key) { diff --git a/lib/6to5/traverse/index.js b/lib/6to5/traverse/index.js index 57bfb767eb..06efb546e6 100644 --- a/lib/6to5/traverse/index.js +++ b/lib/6to5/traverse/index.js @@ -111,7 +111,7 @@ TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent, var ourScope = scope; // we're entering a new scope so let's construct it! if (t.isScope(node)) { - ourScope = new Scope(node, scope); + ourScope = new Scope(node, parent, scope); } node = this.enterNode(obj, key, node, opts.enter, parent, ourScope, state); diff --git a/lib/6to5/traverse/scope.js b/lib/6to5/traverse/scope.js index 6fc0ed9059..723cfb3f80 100644 --- a/lib/6to5/traverse/scope.js +++ b/lib/6to5/traverse/scope.js @@ -14,15 +14,18 @@ var FOR_KEYS = ["left", "init"]; * within. * * @param {Node} block + * @param {Node} parentBlock * @param {Scope} [parent] * @param {File} [file] */ -function Scope(block, parent, file) { +function Scope(block, parentBlock, parent, file) { this.parent = parent; - this.block = block; this.file = parent ? parent.file : file; + this.parentBlock = parentBlock; + this.block = block; + var info = this.getInfo(); this.references = info.references; this.declarations = info.declarations; @@ -197,6 +200,7 @@ Scope.prototype.getInfo = function () { }; if (parent && t.isBlockStatement(block) && t.isFor(parent.block, { body: block })) { + // delegate block let declarations to the parent loop return info; } @@ -240,6 +244,14 @@ Scope.prototype.getInfo = function () { }); } + if (t.isFunctionExpression(block) && block.id) { + if (!t.isProperty(this.parentBlock, { method: true })) { + // SpiderMonkey AST doesn't use MethodDefinition here when it probably + // should since they should be semantically the same? + add(block.id); + } + } + // Program if (t.isProgram(block)) {