diff --git a/lib/6to5/transformation/transformers/es6/block-scoping.js b/lib/6to5/transformation/transformers/es6/block-scoping.js index 275bf751ae..4565456140 100644 --- a/lib/6to5/transformation/transformers/es6/block-scoping.js +++ b/lib/6to5/transformation/transformers/es6/block-scoping.js @@ -124,7 +124,7 @@ BlockScoping.prototype.run = function () { if (!this.hasLetReferences) return; if (needsClosure) { - this.needsClosure(); + this.wrapClosure(); } else { this.remap(); } @@ -205,9 +205,28 @@ BlockScoping.prototype.remap = function () { * Description */ -BlockScoping.prototype.needsClosure = function () { +BlockScoping.prototype.wrapClosure = function () { var block = this.block; + var outsideRefs = this.outsideLetReferences; + + // remap loop heads with colliding variables + if (this.loopParent) { + for (var name in outsideRefs) { + var id = outsideRefs[name]; + + if (this.scope.hasGlobal(id.name)) { + delete outsideRefs[id.name]; + delete this.letReferences[id.name]; + + this.scope.rename(id.name); + + this.letReferences[id.name] = id; + outsideRefs[id.name] = id; + } + } + } + // if we're inside of a for loop then we search to see if there are any // `break`s, `continue`s, `return`s etc this.has = this.checkLoop(); @@ -216,7 +235,7 @@ BlockScoping.prototype.needsClosure = function () { this.hoistVarDeclarations(); // turn outsideLetReferences into an array - var params = values(this.outsideLetReferences); + var params = values(outsideRefs); // build the closure that we're going to wrap the block with var fn = t.functionExpression(null, params, t.blockStatement(block.body)); diff --git a/test/fixtures/transformation/es6-block-scoping-exec/closure-wrap-collision.js b/test/fixtures/transformation/es6-block-scoping-exec/closure-wrap-collision.js new file mode 100644 index 0000000000..41f893eccc --- /dev/null +++ b/test/fixtures/transformation/es6-block-scoping-exec/closure-wrap-collision.js @@ -0,0 +1,9 @@ +for (let i = 1; i < 3; i += 1) { + (function () { + i; + })(); +} + +assert.throws(function () { + i; +}, ReferenceError);