make shadowed function findParent target finder more reliable

This commit is contained in:
Sebastian McKenzie 2015-06-26 02:38:14 +01:00
parent a265c3f25c
commit 6359675a4f
4 changed files with 21 additions and 5 deletions

View File

@ -38,7 +38,7 @@ export var visitor = {
//
var argsIdentifier = t.identifier("arguments");
argsIdentifier._shadowedFunctionLiteral = node;
argsIdentifier._shadowedFunctionLiteral = this;
// push a default parameter definition
function pushDefNode(left, right, i) {

View File

@ -84,7 +84,7 @@ export var visitor = {
var argsId = t.identifier("arguments");
// otherwise `arguments` will be remapped in arrow functions
argsId._shadowedFunctionLiteral = node;
argsId._shadowedFunctionLiteral = this;
// support patterns
if (t.isPattern(rest)) {

View File

@ -211,7 +211,7 @@ class TailCallTransformer {
var decl = t.variableDeclarator(this.argumentsId);
if (this.argumentsId) {
decl.init = t.identifier("arguments");
decl.init._shadowedFunctionLiteral = node;
decl.init._shadowedFunctionLiteral = this.path;
}
topVars.push(decl);
}

View File

@ -9,15 +9,31 @@ function remap(path, key, create) {
if (!path.inShadow()) return;
var shadowed = path.node._shadowedFunctionLiteral;
var currentFunction;
var fnPath = path.findParent(function (path) {
if (shadowed) {
return path.node === shadowed;
// only match our shadowed function parent
if (path === shadowed) {
// found our target reference function
currentFunction = path;
return true;
} else {
return false;
}
} else if (path.isFunction() || path.isProgram()) {
// catch current function in case this is the shadowed one
if (!currentFunction) currentFunction = path;
return !path.is("shadow");
} else {
return !path.is("shadow") && (path.isFunction() || path.isProgram());
return false;
}
});
// no point in realiasing if we're in this function
if (fnPath === currentFunction) return;
var cached = fnPath.getData(key);
if (cached) return cached;