Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zdenko committed Jan 12, 2018
1 parent 7c6f378 commit 95eb472
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/coffeescript/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1331,14 +1331,16 @@ exports.Range = class Range extends Base
[lt, gt] = ["#{idx} <#{@equals}", "#{idx} >#{@equals}"]

# Generate the condition.
condPart = if @stepNum?
if @stepNum > 0 then "#{lt} #{@toVar}" else "#{gt} #{@toVar}"
else if known
[from, to] = [@fromNum, @toNum]
if from <= to then "#{lt} #{to}" else "#{gt} #{to}"
else
cond = if @stepVar then "#{@stepVar} > 0" else "#{@fromVar} <= #{@toVar}"
"#{cond} ? #{lt} #{@toVar} : #{gt} #{@toVar}"
[from, to] = [@fromNum, @toNum]
condPart =
if known
unless @step?
if from <= to then "#{lt} #{to}" else "#{gt} #{to}"
else
if from <= to then "#{from} <= #{idx} && #{lt} #{to}" else "#{from} >= #{idx} && #{gt} #{to}"
else
"#{@fromVar} <= #{@toVar} ? #{@fromVar} <= #{idx} && #{lt} #{@toVar} : #{@fromVar} >= #{idx} && #{gt} #{@toVar}"
cond = if @stepVar then "#{@stepVar} > 0" else "#{@fromVar} <= #{@toVar}"

# Generate the step.
stepPart = if @stepVar
Expand Down
56 changes: 56 additions & 0 deletions test/ranges.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,59 @@ test "#1012 slices with arguments object", ->

test "#1409: creating large ranges outside of a function body", ->
CoffeeScript.eval '[0..100]'

test "#2047: Infinite loop possible when `for` loop with `range` uses variables", ->
up = 1
down = -1
a = 1
b = 5

testRange = (arg) ->
[from, to, step, expectedResult] = arg
r = (x for x in [from..to] by step)
arrayEq r, expectedResult

testData = [
[1, 5, 1, [1..5]]
[1, 5, -1, [1]]
[1, 5, up, [1..5]]
[1, 5, down, [1]]

[a, 5, 1, [1..5]]
[a, 5, -1, [1]]
[a, 5, up, [1..5]]
[a, 5, down, [1]]

[1, b, 1, [1..5]]
[1, b, -1, [1]]
[1, b, up, [1..5]]
[1, b, down, [1]]

[a, b, 1, [1..5]]
[a, b, -1, [1]]
[a, b, up, [1..5]]
[a, b, down, [1]]

[5, 1, 1, [5]]
[5, 1, -1, [5..1]]
[5, 1, up, [5]]
[5, 1, down, [5..1]]

[5, a, 1, [5]]
[5, a, -1, [5..1]]
[5, a, up, [5]]
[5, a, down, [5..1]]

[b, 1, 1, [5]]
[b, 1, -1, [5..1]]
[b, 1, up, [5]]
[b, 1, down, [5..1]]

[b, a, 1, [5]]
[b, a, -1, [5..1]]
[b, a, up, [5]]
[b, a, down, [5..1]]
]

testRange d for d in testData

0 comments on commit 95eb472

Please sign in to comment.