Skip to content

Commit

Permalink
Fix #3441: parentheses wrapping expression throw invalid error (#4849)
Browse files Browse the repository at this point in the history
* fix #3441

* improvements

* refactor
  • Loading branch information
zdenko authored and GeoffreyBooth committed Jan 16, 2018
1 parent c8c8c16 commit e53307b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/coffeescript/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,11 @@ exports.Value = class Value extends Base
constructor: (base, props, tag, isDefaultValue = no) ->
super()
return base if not props and base instanceof Value
# When `Parens` block includes a `StatementLiteral` (e.g. `(b; break) for a in arr`),
# it won't compile since `Parens` (`(b; break)`) is compiled as `Value` and
# pure statement (`break`) can't be used in an expression.
# For this reasons, we return `Block` instead of `Parens`.
return base.unwrap() if base instanceof Parens and base.contains (n) -> n instanceof StatementLiteral
@base = base
@properties = props or []
@[tag] = yes if tag
Expand Down
31 changes: 31 additions & 0 deletions test/control_flow.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,34 @@ test "#4267: lots of for-loops in the same scope", ->
true
"""
ok CoffeeScript.eval(code)

# Issue #3441: Parentheses wrapping expression throw invalid error in `then` clause
test "#3441: `StatementLiteral` in parentheses", ->
i = 0
r1 = ((i++; break) while i < 10)
arrayEq r1, []

i = 0
r2 = ((i++; continue) while i < 10)
arrayEq r2, []

i = 0
r4 = while i < 10 then (i++; break)
arrayEq r4, []

i = 0
r5 = while i < 10 then (i++; continue)
arrayEq r5, []

arr = [0..9]
r6 = ((a; break) for a in arr)
arrayEq r6, []

r7 = ((a; continue) for a in arr)
arrayEq r7, []

r8 = for a in arr then (a; break)
arrayEq r8, []

r9 = for a in arr then (a; continue)
arrayEq r9, []

0 comments on commit e53307b

Please sign in to comment.