add id to a function expression scope

This commit is contained in:
Sebastian McKenzie
2015-01-28 18:14:52 +11:00
parent 5477a990bc
commit 4ff66a5cfc
3 changed files with 16 additions and 4 deletions

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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)) {