diff --git a/lib/nodes.js b/lib/nodes.js index e50cbe98..7d604ee5 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1644,7 +1644,7 @@ return n instanceof CodeNode; }); scope = o.scope; - name = (this.name && this.name.compile(o)) || scope.freeVariable('i'); + name = this.name && this.name.compile(o); index = this.index && this.index.compile(o); if (name && !this.pattern && (range || !codeInBody)) { scope.find(name, { @@ -1659,7 +1659,10 @@ if (!(topLevel)) { rvar = scope.freeVariable('result'); } - ivar = codeInBody ? scope.freeVariable('i') : (range ? name : index || scope.freeVariable('i')); + ivar = range ? name : index; + if (!ivar || codeInBody) { + ivar = scope.freeVariable('i'); + } varPart = ''; guardPart = ''; body = Expressions.wrap([this.body]); diff --git a/src/nodes.coffee b/src/nodes.coffee index a5155180..941a446c 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1380,12 +1380,13 @@ exports.ForNode = class ForNode extends BaseNode source = if range then @source.base else @source codeInBody = @body.contains (n) -> n instanceof CodeNode scope = o.scope - name = (@name and @name.compile(o)) or scope.freeVariable 'i' + name = @name and @name.compile o index = @index and @index.compile o scope.find(name, immediate: yes) if name and not @pattern and (range or not codeInBody) scope.find(index, immediate: yes) if index rvar = scope.freeVariable 'result' unless topLevel - ivar = if codeInBody then scope.freeVariable 'i' else if range then name else index or scope.freeVariable 'i' + ivar = if range then name else index + ivar = scope.freeVariable 'i' if not ivar or codeInBody varPart = '' guardPart = '' body = Expressions.wrap([@body])