Skip to content

Commit 2da1682

Browse files
authored
Merge pull request #1468 from DanielXMoore/return-if
Forbid next-line braced blocks after `return if` and `yield if`
2 parents bae1ff6 + baa8e42 commit 2da1682

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

source/parser.hera

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,7 +2650,7 @@ EmptyBareBlock
26502650

26512651
NoBlock
26522652
# Check that there isn't a block here. Used for empty loop bodies.
2653-
&EOS !IndentedFurther
2653+
&EOS !IndentedFurther !( Nested Then )
26542654

26552655
# A nonempty block that must include braces
26562656
# This version allows same-line postfixes like `while cond`.
@@ -5209,10 +5209,11 @@ KeywordStatement
52095209

52105210
# https://262.ecma-international.org/#prod-ReturnStatement
52115211
# NOTE: Modified to leave room for `return.value` and `return =`
5212-
Return !(":" / "." / AfterReturnShorthand) MaybeParenNestedExpression?:expression -> {
5212+
# NOTE: Stop parsing if there's a postfix if/etc.
5213+
Return:ret !(":" / "." / AfterReturnShorthand) MaybeParenNestedExpression?:expression -> {
52135214
type: "ReturnStatement",
52145215
expression,
5215-
children: $0,
5216+
children: [ ret, expression ],
52165217
}
52175218

52185219
ThrowStatement
@@ -5274,6 +5275,8 @@ NestedExpression
52745275
# parentheses if indented, so that the expression starts on the same line.
52755276
# (e.g. for `return` or `yield`)
52765277
MaybeParenNestedExpression
5278+
# Skip if there's a postfix if/etc.
5279+
&( _? PostfixStatement NoBlock ) -> ""
52775280
# Not nested case
52785281
!EOS Expression -> $2
52795282
# Avoid wrapping array/object return value in parentheses.

test/if.civet

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,40 @@ describe "if", ->
560560
}
561561
"""
562562

563+
testCase """
564+
return postfix if with braced expression after
565+
---
566+
=>
567+
return if x
568+
{
569+
x
570+
}
571+
---
572+
() => {
573+
if (x) { return }
574+
return ({
575+
x
576+
})
577+
}
578+
"""
579+
580+
testCase """
581+
yield postfix if with braced expression after
582+
---
583+
->
584+
yield if x
585+
{
586+
x
587+
}
588+
---
589+
(function*() {
590+
if (x) { yield };
591+
({
592+
x
593+
})
594+
})
595+
"""
596+
563597
testCase """
564598
implied parens parethesized expression
565599
---
@@ -954,15 +988,15 @@ describe "if", ->
954988

955989
describe "return if expression", ->
956990
testCase """
957-
return if expression
991+
one line
958992
---
959993
return if y then 1 else 0
960994
---
961995
return (y? 1 : 0)
962996
"""
963997

964998
testCase """
965-
return if expression
999+
if/then/else
9661000
---
9671001
return if y
9681002
then 1
@@ -973,7 +1007,7 @@ describe "if", ->
9731007
"""
9741008

9751009
testCase """
976-
return if expression
1010+
indented if/else
9771011
---
9781012
return if y
9791013
1

0 commit comments

Comments
 (0)