Skip to content

Commit

Permalink
RedundantBraces: fix how we find {} in func body
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 5, 2019
1 parent a2d2449 commit 575cc76
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,15 @@ case object RedundantBraces extends Rewrite {
if f.tokens.last.is[Token.RightBrace] && getTermLineSpan(f) > 0 =>
val rbrace = f.tokens.last
val lbrace = ctx.matchingParens(TokenOps.hash(rbrace))
val lparen = ctx.matchingParens(TokenOps.hash(rparen))
builder += TokenPatch.Replace(lparen, lbrace.text)
builder += TokenPatch.Remove(lbrace)
builder += TokenPatch.Remove(rparen)
removeTrailingLF(rbrace.pos, rparen)
// we really wanted the first token of body but Block usually
// points to the next non-whitespace token after opening brace
if (lbrace.start <= f.body.tokens.head.start) {
val lparen = ctx.matchingParens(TokenOps.hash(rparen))
builder += TokenPatch.Replace(lparen, lbrace.text)
builder += TokenPatch.Remove(lbrace)
builder += TokenPatch.Remove(rparen)
removeTrailingLF(rbrace.pos, rparen)
}
case _ =>
}

Expand All @@ -124,12 +128,14 @@ case object RedundantBraces extends Rewrite {
&& exactlyOneStatement(body) && isLineSpanOk(body) =>
val rbrace = fun.tokens.last
val lbrace = ctx.matchingParens(TokenOps.hash(rbrace))
builder += TokenPatch.Remove(lbrace)
builder += TokenPatch.Remove(rbrace)
removeTrailingLF(lbrace.pos, rbrace)
ctx.tokenTraverser
.reverseFind(rbrace)(!Whitespace.unapply(_))
.foreach(t => removeTrailingLF(t.pos, rbrace))
if (lbrace.start <= body.tokens.head.start) {
builder += TokenPatch.Remove(lbrace)
builder += TokenPatch.Remove(rbrace)
removeTrailingLF(lbrace.pos, rbrace)
ctx.tokenTraverser
.reverseFind(rbrace)(!Whitespace.unapply(_))
.foreach(t => removeTrailingLF(t.pos, rbrace))
}
case _ =>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,17 @@ object a {
}

}
<<< #1583 1 shouldn't remove "redundant braces"
val a = b.c(d =>
e match { case f =>
})
>>>
val a = b.c(d =>
e match {
case f =>
}
)
<<< #1583 2 shouldn't remove "redundant braces"
val a = b.c(d => {e}, f =>g match { case h =>})
>>>
val a = b.c(d => e, f => g match { case h => })

0 comments on commit 575cc76

Please sign in to comment.