Skip to content

Commit

Permalink
iRedundantBraces: add canRewriteXyzWithParens
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Meltzer authored and kitbellew committed Feb 18, 2024
1 parent e6a58f5 commit 84f0b4b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ class FormatWriter(formatOps: FormatOps) {
case rb: T.RightBrace => // look for "foo { bar }"
val ok = tok.meta.leftOwner match {
case b: Term.Block =>
checkApply(b) && RedundantBraces.canRewriteWithParens(b) &&
checkApply(b) && RedundantBraces.canRewriteBlockWithParens(b) &&
b.parent.exists(_.tokens.last.start == rb.start)
case f: Term.FunctionTerm =>
checkApply(f) && RedundantBraces.canRewriteWithParens(f)
checkApply(f) && RedundantBraces.canRewriteFuncWithParens(f)
case _ => false
}
if (ok) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory {
case _ => false
}

def canRewriteWithParens(b: Term.Block): Boolean =
getBlockSingleStat(b).exists {
case f: Term.FunctionTerm => canRewriteWithParens(f)
def canRewriteBlockWithParens(b: Term.Block): Boolean =
getBlockSingleStat(b).exists(canRewriteStatWithParens)

def canRewriteStatWithParens(t: Stat): Boolean =
t match {
case f: Term.FunctionTerm => canRewriteFuncWithParens(f)
case _: Term.Assign => false // disallowed in 2.13
case _: Defn => false
case _ => true
Expand All @@ -38,12 +41,12 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory {
/* guard for statements requiring a wrapper block
* "foo { x => y; z }" can't become "foo(x => y; z)" */
@tailrec
def canRewriteWithParens(
def canRewriteFuncWithParens(
f: Term.FunctionTerm,
nested: Boolean = false
): Boolean =
!needParensAroundParams(f) && (getTreeSingleStat(f.body) match {
case Some(t: Term.FunctionTerm) => canRewriteWithParens(t, true)
case Some(t: Term.FunctionTerm) => canRewriteFuncWithParens(t, true)
case Some(_: Defn) => false
case x => nested || x.isDefined
})
Expand Down

0 comments on commit 84f0b4b

Please # to comment.