Skip to content

Commit 31892e1

Browse files
committed
Issue jashkenas#959 (and countless others) Removing the loop-block-scoped magic for once and for all.
1 parent aa3099c commit 31892e1

File tree

5 files changed

+10
-88
lines changed

5 files changed

+10
-88
lines changed

Cakefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,13 @@ runTests = (CoffeeScript) ->
151151
failures = []
152152

153153
# Mix in the assert module globally, to make it available for tests.
154-
for name, func of require 'assert'
154+
addGlobal = (name, func) ->
155155
global[name] = ->
156156
passedTests += 1
157157
func arguments...
158158

159+
addGlobal name, func for name, func of require 'assert'
160+
159161
# Convenience aliases.
160162
global.eq = global.strictEqual
161163
global.CoffeeScript = CoffeeScript

lib/command.js

+5-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/nodes.js

+1-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

-3
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,6 @@ exports.For = class For extends Base
14411441
# some cannot.
14421442
compileNode: (o) ->
14431443
body = Expressions.wrap [@body]
1444-
hasCode = body.contains (node) -> node instanceof Code
14451444
hasPure = last(body.expressions)?.containsPureStatement()
14461445
source = if @range then @source.base else @source
14471446
scope = o.scope
@@ -1476,8 +1475,6 @@ exports.For = class For extends Base
14761475
body = Push.wrap rvar, body
14771476
if @guard
14781477
body = Expressions.wrap [new If @guard, body]
1479-
if hasCode
1480-
body = Closure.wrap(body, yes, not @returns)
14811478
varPart = "\n#{idt1}#{namePart};" if namePart
14821479
if @object
14831480
forPart = "#{ivar} in #{svar}"

test/test_comprehensions.coffee

+1-68
Original file line numberDiff line numberDiff line change
@@ -73,63 +73,14 @@ ok 2 of evens
7373
all = 1
7474

7575

76-
# Ensure that the closure wrapper preserves local variables.
77-
obj = {}
78-
79-
for method in ['one', 'two', 'three']
80-
obj[method] = ->
81-
"I'm " + method
82-
83-
ok obj.one() is "I'm one"
84-
ok obj.two() is "I'm two"
85-
ok obj.three() is "I'm three"
86-
76+
# Index values at the end of a loop.
8777
i = 0
8878
for i in [1..3]
8979
-> 'func'
9080
break if false
9181
ok i is 4
9282

9383

94-
# Ensure that local variables are closed over for range comprehensions.
95-
funcs = for i in [1..3]
96-
-> -i
97-
98-
ok (func() for func in funcs).join(' ') is '-1 -2 -3'
99-
ok i is 4
100-
101-
102-
# Even when referenced in the filter.
103-
list = ['one', 'two', 'three']
104-
105-
methods = for num, i in list when num isnt 'two' and i isnt 1
106-
-> num + ' ' + i
107-
108-
ok methods.length is 2
109-
ok methods[0]() is 'one 0'
110-
ok methods[1]() is 'three 2'
111-
112-
113-
# Even a convoluted one.
114-
funcs = []
115-
116-
for i in [1..3]
117-
x = i * 2
118-
((z)->
119-
funcs.push -> z + ' ' + i
120-
)(x)
121-
122-
ok (func() for func in funcs).join(', ') is '2 1, 4 2, 6 3'
123-
124-
funcs = []
125-
126-
results = for i in [1..3]
127-
z = (x * 3 for x in [1..i])
128-
((a, b, c) -> [a, b, c].join(' ')).apply this, z
129-
130-
ok results.join(', ') is '3 , 3 6 , 3 6 9'
131-
132-
13384
# Naked ranges are expanded into arrays.
13485
array = [0..10]
13586
ok(num % 2 is 0 for num in array by 2)
@@ -221,15 +172,6 @@ odds = while i--
221172
ok odds.join(', ') is '5, 3, 1'
222173

223174

224-
# Nested shared scopes.
225-
foo = ->
226-
for i in [0..7]
227-
for j in [0..7]
228-
-> i + j
229-
230-
eq foo()[3][4](), 7
231-
232-
233175
# Issue #897: Ensure that plucked function variables aren't leaked.
234176
facets = {}
235177
list = ['one', 'two']
@@ -249,12 +191,3 @@ for d in a.b?.c
249191
e = d
250192

251193
eq e, 3
252-
253-
254-
# Issue #948. Capturing loop variables.
255-
funcs = []
256-
for y in [1, 2, 3]
257-
z = y
258-
funcs.push -> "y is #{y} and z is #{z}"
259-
260-
eq funcs[1](), "y is 2 and z is 2"

0 commit comments

Comments
 (0)