Fix #4882: Range not declaring var for the "i" (#4883)

* fix #4882

* test
This commit is contained in:
zdenko 2018-02-04 22:33:08 +01:00 committed by Geoffrey Booth
parent 794f65fbd7
commit ba094126e2
3 changed files with 13 additions and 2 deletions

View File

@ -2043,7 +2043,7 @@
idx = del(o, 'index');
idxName = del(o, 'name');
namedIndex = idxName && idxName !== idx;
varPart = `${idx} = ${this.fromC}`;
varPart = known && !namedIndex ? `var ${idx} = ${this.fromC}` : `${idx} = ${this.fromC}`;
if (this.toC !== this.toVar) {
varPart += `, ${this.toC}`;
}

View File

@ -1353,7 +1353,11 @@ exports.Range = class Range extends Base
idx = del o, 'index'
idxName = del o, 'name'
namedIndex = idxName and idxName isnt idx
varPart = "#{idx} = #{@fromC}"
varPart =
if known and not namedIndex
"var #{idx} = #{@fromC}"
else
"#{idx} = #{@fromC}"
varPart += ", #{@toC}" if @toC isnt @toVar
varPart += ", #{@step}" if @step isnt @stepVar
[lt, gt] = ["#{idx} <#{@equals}", "#{idx} >#{@equals}"]

View File

@ -195,3 +195,10 @@ test "#2047: from, to and step as variables", ->
step = 0
r = (x for x in [b..a] by step)
arrayEq r, []
test "#4884: Range not declaring var for the 'i'", ->
'use strict'
[0..21].forEach (idx) ->
idx + 1
eq global.i, undefined