Skip to content

Commit 55ea48c

Browse files
committed
1 parent 5a43b2d commit 55ea48c

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

lib/coffeescript/nodes.js

Lines changed: 6 additions & 5 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,11 +863,14 @@ 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`),
866+
# When `Parens` block includes a `StatementLiteral`, `Return`, `YieldReturn` or `AwaitReturn`
867+
# (e.g. `(b; break) for a in arr`, '-> (return)`, '-> (yield return)`),
867868
# it won't compile since `Parens` (`(b; break)`) is compiled as `Value` and
868869
# pure statement (`break`) can't be used in an expression.
869870
# For this reasons, we return `Block` instead of `Parens`.
870-
return base.unwrap() if base instanceof Parens and base.contains (n) -> n instanceof StatementLiteral
871+
if base instanceof Parens and
872+
base.contains (n) -> n instanceof StatementLiteral or n instanceof Return
873+
return base.unwrap()
871874
@base = base
872875
@properties = props or []
873876
@[tag] = yes if tag

test/error_messages.coffee

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -953,15 +953,6 @@ test "`yield` outside of a function", ->
953953
^^^^^^^^^^^^
954954
'''
955955

956-
test "#4097: `yield return` as an expression", ->
957-
assertErrorFormat '''
958-
-> (yield return)
959-
''', '''
960-
[stdin]:1:5: error: cannot use a pure statement in an expression
961-
-> (yield return)
962-
^^^^^^^^^^^^
963-
'''
964-
965956
test "`&&=` and `||=` with a space in-between", ->
966957
assertErrorFormat '''
967958
a = 0

test/functions.coffee

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,17 @@ test "#4657: destructured array parameters", ->
572572
result = f [1, 2, 3, 4]
573573
arrayEq result.a, [1, 2, 3]
574574
eq result.b, 4
575+
576+
test "#5013: return statements in parentheses", ->
577+
compile = (code) -> CoffeeScript.compile(code, bare: yes).trim().replace(/\s+/g, " ")
578+
579+
eq "(function() {});", compile("-> (return)")
580+
eq "(function*() {});", compile("-> (yield return)")
581+
eq "(async function() {});", compile("-> (await return)")
582+
583+
foo = (cond) ->
584+
(a = 1; return) if cond
585+
a = 2
586+
587+
eq undefined, foo yes
588+
eq 2, foo no

0 commit comments

Comments
 (0)