2010-04-03 10:39:32 -04:00
|
|
|
# Expression conversion under explicit returns.
|
2010-07-25 01:23:37 -04:00
|
|
|
first = ->
|
2010-10-24 14:02:59 -04:00
|
|
|
return ('do' for x in [1,2,3])
|
2010-03-25 18:54:17 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
second = ->
|
2010-03-25 18:54:17 -04:00
|
|
|
return ['re' for x in [1,2,3]]
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
third = ->
|
2010-03-25 18:54:17 -04:00
|
|
|
return ('mi' for x in [1,2,3])
|
|
|
|
|
|
|
|
ok first().join(' ') is 'do do do'
|
|
|
|
ok second()[0].join(' ') is 're re re'
|
2010-06-13 01:13:52 -04:00
|
|
|
ok third().join(' ') is 'mi mi mi'
|
|
|
|
|
|
|
|
|
|
|
|
# Testing returns with multiple branches.
|
2010-07-25 01:23:37 -04:00
|
|
|
func = ->
|
2010-06-13 01:13:52 -04:00
|
|
|
if false
|
|
|
|
for a in b
|
|
|
|
return c if d
|
|
|
|
else
|
|
|
|
"word"
|
|
|
|
|
2010-06-13 01:19:31 -04:00
|
|
|
ok func() is 'word'
|
|
|
|
|
|
|
|
|
|
|
|
# And with switches.
|
2010-07-25 01:23:37 -04:00
|
|
|
func = ->
|
2010-06-13 01:19:31 -04:00
|
|
|
switch 'a'
|
|
|
|
when 'a' then 42
|
|
|
|
else return 23
|
|
|
|
|
2010-10-25 20:08:16 -04:00
|
|
|
eq func(), 42
|
|
|
|
|
|
|
|
# Ensure that we don't wrap Nodes that are "pureStatement" in a closure.
|
|
|
|
items = [1, 2, 3, "bacon", 4, 5]
|
|
|
|
|
|
|
|
findit = (items) ->
|
|
|
|
for item in items
|
|
|
|
return item if item is "bacon"
|
|
|
|
|
|
|
|
ok findit(items) is "bacon"
|
|
|
|
|
|
|
|
|
|
|
|
# When a closure wrapper is generated for expression conversion, make sure
|
|
|
|
# that references to "this" within the wrapper are safely converted as well.
|
|
|
|
obj =
|
|
|
|
num: 5
|
|
|
|
func: ->
|
|
|
|
this.result = if false
|
|
|
|
10
|
|
|
|
else
|
|
|
|
"a"
|
|
|
|
"b"
|
|
|
|
this.num
|
|
|
|
|
|
|
|
eq obj.num, obj.func()
|
|
|
|
eq obj.num, obj.result
|
2010-11-20 20:22:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Multiple semicolon-separated statements in parentheticals.
|
|
|
|
eq 3, (1; 2; 3)
|
|
|
|
eq 3, (-> return (1; 2; 3))()
|