Skip to content

Commit 880c5c8

Browse files
author
Timothy Jones
committed
Fixing destructor in magicked for. Also making destructors in range loops syntax errors.
1 parent 7596e3a commit 880c5c8

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

lib/nodes.js

Lines changed: 14 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,9 @@ exports.For = class For extends Base
14051405
@object = !!source.object
14061406
[@name, @index] = [@index, @name] if @object
14071407
throw SyntaxError 'index cannot be a pattern matching expression' if @index instanceof Value
1408+
@range = @source instanceof Value and @source.base instanceof Range and not @source.properties.length
14081409
@pattern = @name instanceof Value
1410+
throw SyntaxError 'cannot pattern match a range loop' if @range and @pattern
14091411
@returns = false
14101412

14111413
makeReturn: ->
@@ -1423,24 +1425,23 @@ exports.For = class For extends Base
14231425
# some cannot.
14241426
compileNode: (o) ->
14251427
topLevel = del(o, 'top') and not @returns
1426-
range = @source instanceof Value and @source.base instanceof Range and not @source.properties.length
1427-
source = if range then @source.base else @source
1428+
source = if @range then @source.base else @source
14281429
codeInBody = not @body.containsPureStatement() and @body.contains (node) -> node instanceof Code
14291430
scope = o.scope
14301431
name = @name and @name.compile o
14311432
index = @index and @index.compile o
1432-
scope.find(name, immediate: yes) if name and not @pattern and (range or not codeInBody)
1433+
scope.find(name, immediate: yes) if name and not @pattern and (@range or not codeInBody)
14331434
scope.find(index, immediate: yes) if index
14341435
rvar = scope.freeVariable 'result' unless topLevel
1435-
ivar = if range then name else index
1436+
ivar = if @range then name else index
14361437
ivar = scope.freeVariable 'i' if not ivar or codeInBody
1437-
nvar = scope.freeVariable 'i' if name and not range and codeInBody
1438+
nvar = scope.freeVariable 'i' if name and not @range and codeInBody
14381439
varPart = ''
14391440
guardPart = ''
14401441
unstepPart = ''
14411442
body = Expressions.wrap [@body]
14421443
idt1 = @idt 1
1443-
if range
1444+
if @range
14441445
forPart = source.compile merge o, {index: ivar, @step}
14451446
else
14461447
svar = sourcePart = @source.compile o
@@ -1463,7 +1464,7 @@ exports.For = class For extends Base
14631464
if @guard
14641465
body = Expressions.wrap [new If @guard, body]
14651466
if codeInBody
1466-
body.unshift new Literal "var #{name} = #{ivar}" if range
1467+
body.unshift new Literal "var #{name} = #{ivar}" if @range
14671468
body.unshift new Literal "var #{namePart}" if namePart
14681469
body.unshift new Literal "var #{index} = #{ivar}" if index
14691470
lastLine = body.expressions.pop()
@@ -1472,8 +1473,8 @@ exports.For = class For extends Base
14721473
body.push lastLine
14731474
o.indent = @idt 1
14741475
body = Expressions.wrap [new Literal body.compile o]
1475-
body.push new Assign new Literal(index), new Literal ivar if index
1476-
body.push new Assign new Literal(name), new Literal nvar or ivar if name
1476+
body.push new Assign @index, new Literal ivar if index
1477+
body.push new Assign @name, new Literal nvar or ivar if name
14771478
else
14781479
varPart = "#{idt1}#{namePart};\n" if namePart
14791480
if forPart and name is ivar
@@ -1483,7 +1484,7 @@ exports.For = class For extends Base
14831484
forPart = "#{ivar} in #{sourcePart}"
14841485
guardPart = "\n#{idt1}if (!#{utility('hasProp')}.call(#{svar}, #{ivar})) continue;" unless @raw
14851486
body = body.compile merge o, indent: idt1, top: true
1486-
vars = if range then name else "#{name}, #{ivar}"
1487+
vars = if @range then name else "#{name}, #{ivar}"
14871488
"""
14881489
#{resultPart}#{@tab}for (#{forPart}) {#{guardPart}
14891490
#{varPart}#{body}

test/test_comprehensions.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ for i in [0]
9595
ok count is 0
9696
ok i is 50
9797

98+
for [a, b] in [[0, 1]] then ->
99+
ok a is 0
100+
ok b is 1
101+
98102

99103
# Even when referenced in the filter.
100104
list = ['one', 'two', 'three']

0 commit comments

Comments
 (0)