Skip to content

Commit

Permalink
Merge pull request #1468 from DanielXMoore/return-if
Browse files Browse the repository at this point in the history
Forbid next-line braced blocks after `return if` and `yield if`
  • Loading branch information
edemaine authored Oct 14, 2024
2 parents bae1ff6 + baa8e42 commit 2da1682
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
9 changes: 6 additions & 3 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -2650,7 +2650,7 @@ EmptyBareBlock

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

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

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

ThrowStatement
Expand Down Expand Up @@ -5274,6 +5275,8 @@ NestedExpression
# parentheses if indented, so that the expression starts on the same line.
# (e.g. for `return` or `yield`)
MaybeParenNestedExpression
# Skip if there's a postfix if/etc.
&( _? PostfixStatement NoBlock ) -> ""
# Not nested case
!EOS Expression -> $2
# Avoid wrapping array/object return value in parentheses.
Expand Down
40 changes: 37 additions & 3 deletions test/if.civet
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,40 @@ describe "if", ->
}
"""

testCase """
return postfix if with braced expression after
---
=>
return if x
{
x
}
---
() => {
if (x) { return }
return ({
x
})
}
"""

testCase """
yield postfix if with braced expression after
---
->
yield if x
{
x
}
---
(function*() {
if (x) { yield };
({
x
})
})
"""

testCase """
implied parens parethesized expression
---
Expand Down Expand Up @@ -954,15 +988,15 @@ describe "if", ->

describe "return if expression", ->
testCase """
return if expression
one line
---
return if y then 1 else 0
---
return (y? 1 : 0)
"""

testCase """
return if expression
if/then/else
---
return if y
then 1
Expand All @@ -973,7 +1007,7 @@ describe "if", ->
"""

testCase """
return if expression
indented if/else
---
return if y
1
Expand Down

0 comments on commit 2da1682

Please sign in to comment.