fix bug in let scoping resulting in unneccesary replacement - closes #193, closes #185

This commit is contained in:
Sebastian McKenzie
2014-11-19 13:46:00 +11:00
parent a884a26402
commit 7fc2fe41af
2 changed files with 24 additions and 8 deletions

View File

@@ -88,6 +88,9 @@ LetScoping.prototype.run = function () {
this.info = this.getInfo();
// remap all let references that exist in upper scopes to their uid
this.remap();
// this is a block within a `Function` so we can safely leave it be
if (t.isFunction(this.parent)) return this.noClosure();
@@ -138,23 +141,29 @@ LetScoping.prototype.run = function () {
};
/**
* There are no let references accessed within a closure so we can just traverse
* through this block and replace all references that exist in a higher scope to
* their uids.
* There are no let references accessed within a closure so we can just turn the
* lets into vars.
*/
LetScoping.prototype.noClosure = function () {
var replacements = this.info.duplicates;
var declarators = this.info.declarators;
var block = this.block;
standardiseLets(this.info.declarators);
};
standardiseLets(declarators);
/**
* Traverse through block and replace all references that exist in a higher
* scope to their uids.
*/
LetScoping.prototype.remap = function () {
var replacements = this.info.duplicates;
var block = this.block;
if (_.isEmpty(replacements)) return;
var replace = function (node, parent) {
var replace = function (node, parent, scope) {
if (!t.isIdentifier(node)) return;
if (!t.isReferenced(node, parent)) return;
if (scope && scope.hasOwn(node.name)) return;
node.name = replacements[node.name] || node.name;
};