Skip to content

Commit 001f97a

Browse files
zdenkoGeoffreyBooth
authored andcommitted
Fix jashkenas#5013: return statement as an expression (jashkenas#5014)
* fix jashkenas#5013 * disallow statement in the expression
1 parent ce66a49 commit 001f97a

File tree

4 files changed

+36
-45
lines changed

4 files changed

+36
-45
lines changed

lib/coffeescript/nodes.js

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

src/nodes.coffee

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -863,11 +863,6 @@ 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
871866
@base = base
872867
@properties = props or []
873868
@[tag] = yes if tag

test/control_flow.coffee

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,37 +1112,6 @@ test "#3921: `switch`", ->
11121112
else "none"
11131113
eq "five", c
11141114

1115-
# Issue #3441: Parentheses wrapping expression throw invalid error in `then` clause
1116-
test "#3441: `StatementLiteral` in parentheses", ->
1117-
i = 0
1118-
r1 = ((i++; break) while i < 10)
1119-
arrayEq r1, []
1120-
1121-
i = 0
1122-
r2 = ((i++; continue) while i < 10)
1123-
arrayEq r2, []
1124-
1125-
i = 0
1126-
r4 = while i < 10 then (i++; break)
1127-
arrayEq r4, []
1128-
1129-
i = 0
1130-
r5 = while i < 10 then (i++; continue)
1131-
arrayEq r5, []
1132-
1133-
arr = [0..9]
1134-
r6 = ((a; break) for a in arr)
1135-
arrayEq r6, []
1136-
1137-
r7 = ((a; continue) for a in arr)
1138-
arrayEq r7, []
1139-
1140-
r8 = for a in arr then (a; break)
1141-
arrayEq r8, []
1142-
1143-
r9 = for a in arr then (a; continue)
1144-
arrayEq r9, []
1145-
11461115
# Issue #3909: backslash to break line in `for` loops throw syntax error
11471116
test "#3909: backslash `for own ... of`", ->
11481117

test/error_messages.coffee

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,42 @@ test "#4097: `yield return` as an expression", ->
962962
^^^^^^^^^^^^
963963
'''
964964

965+
test "#5013: `await return` as an expression", ->
966+
assertErrorFormat '''
967+
-> (await return)
968+
''', '''
969+
[stdin]:1:5: error: cannot use a pure statement in an expression
970+
-> (await return)
971+
^^^^^^^^^^^^
972+
'''
973+
974+
test "#5013: `return` as an expression", ->
975+
assertErrorFormat '''
976+
-> (return)
977+
''', '''
978+
[stdin]:1:5: error: cannot use a pure statement in an expression
979+
-> (return)
980+
^^^^^^
981+
'''
982+
983+
test "#5013: `break` as an expression", ->
984+
assertErrorFormat '''
985+
(b = 1; break) for b in a
986+
''', '''
987+
[stdin]:1:9: error: cannot use a pure statement in an expression
988+
(b = 1; break) for b in a
989+
^^^^^
990+
'''
991+
992+
test "#5013: `continue` as an expression", ->
993+
assertErrorFormat '''
994+
(b = 1; continue) for b in a
995+
''', '''
996+
[stdin]:1:9: error: cannot use a pure statement in an expression
997+
(b = 1; continue) for b in a
998+
^^^^^^^^
999+
'''
1000+
9651001
test "`&&=` and `||=` with a space in-between", ->
9661002
assertErrorFormat '''
9671003
a = 0

0 commit comments

Comments
 (0)