Skip to content

Commit e53307b

Browse files
zdenkoGeoffreyBooth
authored andcommitted
Fix #3441: parentheses wrapping expression throw invalid error (#4849)
* fix #3441 * improvements * refactor
1 parent c8c8c16 commit e53307b

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

lib/coffeescript/nodes.js

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

src/nodes.coffee

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,11 @@ exports.Value = class Value extends Base
863863
constructor: (base, props, tag, isDefaultValue = no) ->
864864
super()
865865
return base if not props and base instanceof Value
866+
# When `Parens` block includes a `StatementLiteral` (e.g. `(b; break) for a in arr`),
867+
# it won't compile since `Parens` (`(b; break)`) is compiled as `Value` and
868+
# pure statement (`break`) can't be used in an expression.
869+
# For this reasons, we return `Block` instead of `Parens`.
870+
return base.unwrap() if base instanceof Parens and base.contains (n) -> n instanceof StatementLiteral
866871
@base = base
867872
@properties = props or []
868873
@[tag] = yes if tag

test/control_flow.coffee

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,34 @@ test "#4267: lots of for-loops in the same scope", ->
484484
true
485485
"""
486486
ok CoffeeScript.eval(code)
487+
488+
# Issue #3441: Parentheses wrapping expression throw invalid error in `then` clause
489+
test "#3441: `StatementLiteral` in parentheses", ->
490+
i = 0
491+
r1 = ((i++; break) while i < 10)
492+
arrayEq r1, []
493+
494+
i = 0
495+
r2 = ((i++; continue) while i < 10)
496+
arrayEq r2, []
497+
498+
i = 0
499+
r4 = while i < 10 then (i++; break)
500+
arrayEq r4, []
501+
502+
i = 0
503+
r5 = while i < 10 then (i++; continue)
504+
arrayEq r5, []
505+
506+
arr = [0..9]
507+
r6 = ((a; break) for a in arr)
508+
arrayEq r6, []
509+
510+
r7 = ((a; continue) for a in arr)
511+
arrayEq r7, []
512+
513+
r8 = for a in arr then (a; break)
514+
arrayEq r8, []
515+
516+
r9 = for a in arr then (a; continue)
517+
arrayEq r9, []

0 commit comments

Comments
 (0)