@@ -1405,7 +1405,9 @@ exports.For = class For extends Base
1405
1405
@object = !! source .object
1406
1406
[@name , @index ] = [@index , @name ] if @object
1407
1407
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
1408
1409
@pattern = @name instanceof Value
1410
+ throw SyntaxError ' cannot pattern match a range loop' if @range and @pattern
1409
1411
@returns = false
1410
1412
1411
1413
makeReturn : ->
@@ -1423,24 +1425,23 @@ exports.For = class For extends Base
1423
1425
# some cannot.
1424
1426
compileNode : (o ) ->
1425
1427
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
1428
1429
codeInBody = not @body .containsPureStatement () and @body .contains (node) -> node instanceof Code
1429
1430
scope = o .scope
1430
1431
name = @name and @name .compile o
1431
1432
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)
1433
1434
scope .find (index, immediate : yes ) if index
1434
1435
rvar = scope .freeVariable ' result' unless topLevel
1435
- ivar = if range then name else index
1436
+ ivar = if @ range then name else index
1436
1437
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
1438
1439
varPart = ' '
1439
1440
guardPart = ' '
1440
1441
unstepPart = ' '
1441
1442
body = Expressions .wrap [@body ]
1442
1443
idt1 = @ idt 1
1443
- if range
1444
+ if @ range
1444
1445
forPart = source .compile merge o, {index : ivar, @step }
1445
1446
else
1446
1447
svar = sourcePart = @source .compile o
@@ -1463,7 +1464,7 @@ exports.For = class For extends Base
1463
1464
if @guard
1464
1465
body = Expressions .wrap [new If @guard , body]
1465
1466
if codeInBody
1466
- body .unshift new Literal " var #{ name} = #{ ivar} " if range
1467
+ body .unshift new Literal " var #{ name} = #{ ivar} " if @ range
1467
1468
body .unshift new Literal " var #{ namePart} " if namePart
1468
1469
body .unshift new Literal " var #{ index} = #{ ivar} " if index
1469
1470
lastLine = body .expressions .pop ()
@@ -1472,8 +1473,8 @@ exports.For = class For extends Base
1472
1473
body .push lastLine
1473
1474
o .indent = @ idt 1
1474
1475
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
1477
1478
else
1478
1479
varPart = " #{ idt1}#{ namePart} ;\n " if namePart
1479
1480
if forPart and name is ivar
@@ -1483,7 +1484,7 @@ exports.For = class For extends Base
1483
1484
forPart = " #{ ivar} in #{ sourcePart} "
1484
1485
guardPart = " \n #{ idt1} if (!#{ utility (' hasProp' )} .call(#{ svar} , #{ ivar} )) continue;" unless @raw
1485
1486
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} "
1487
1488
"""
1488
1489
#{ resultPart}#{ @tab } for (#{ forPart} ) {#{ guardPart}
1489
1490
#{ varPart}#{ body}
0 commit comments