Skip to content

Commit

Permalink
Use Term.FunctionTerm where Term.Function was used
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 f2b64cb commit 1d6ca94
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ class FormatOps(
}
def isOldTopLevel(child: Tree) = child.parent.exists {
case _: Term.Block | _: Term.If | _: Term.While | _: Source => true
case fun: Term.FunctionTerm if isBlockFunction(fun) => true
case fun: Term.FunctionTerm => isBlockFunction(fun)
case t: Case => t.pat.eq(child) || t.body.eq(child)
case _ => false
}
Expand Down Expand Up @@ -921,7 +921,7 @@ class FormatOps(

def isEmptyFunctionBody(tree: Tree): Boolean =
tree match {
case function: Term.Function =>
case function: Term.FunctionTerm =>
function.body match {
case b: Term.Block => b.stats.isEmpty
case _ => false
Expand Down Expand Up @@ -2676,7 +2676,7 @@ class FormatOps(
private object RightArrowImpl extends Factory {
def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result =
ft.meta.leftOwner match {
case t: Term.Function =>
case t: Term.FunctionTerm =>
val skip = t.parent.exists(_.is[Term.Block])
if (skip) None else Some((t.body, seq(all, t.paramClause.values)))
case _ => None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class FormatWriter(formatOps: FormatOps) {
case b: Term.Block =>
checkApply(b) && RedundantBraces.canRewriteWithParens(b) &&
b.parent.exists(_.tokens.last.start == rb.start)
case f: Term.Function =>
case f: Term.FunctionTerm =>
checkApply(f) && RedundantBraces.canRewriteWithParens(f)
case _ => false
}
Expand Down Expand Up @@ -1258,7 +1258,7 @@ class FormatWriter(formatOps: FormatOps) {
def unapply(tree: Tree): Option[Tree] =
tree match {
case _: Source | _: Template | _: Term.Block | _: Term.Match |
_: Type.Match | _: Term.Function | _: Term.PartialFunction =>
_: Type.Match | _: Term.FunctionTerm | _: Term.PartialFunction =>
Some(tree)
case _ => None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ class Router(formatOps: FormatOps) {
else Split(Space, 0).onlyIf(useSpace).withSingleLine(close)
val otherSplits = rightOwner match {
case _: Term.PartialFunction | Term.Block(
List(_: Term.Function | _: Term.PartialFunction)
List(_: Term.FunctionTerm | _: Term.PartialFunction)
) =>
Seq(Split(Newline, 0))
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory {
override def create(ftoks: FormatTokens): Rule =
new RedundantBraces(ftoks)

def needParensAroundParams(f: Term.Function): Boolean =
def needParensAroundParams(f: Term.FunctionTerm): Boolean =
/* either we have parens or no type; multiple params or
* no params guarantee parens, so we look for type and
* parens only for a single param */
Expand All @@ -29,7 +29,7 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory {

def canRewriteWithParens(b: Term.Block): Boolean =
getBlockSingleStat(b).exists {
case f: Term.Function => canRewriteWithParens(f)
case f: Term.FunctionTerm => canRewriteWithParens(f)
case _: Term.Assign => false // disallowed in 2.13
case _: Defn => false
case _ => true
Expand All @@ -38,9 +38,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(f: Term.Function, nested: Boolean = false): Boolean =
def canRewriteWithParens(
f: Term.FunctionTerm,
nested: Boolean = false
): Boolean =
!needParensAroundParams(f) && (getTreeSingleStat(f.body) match {
case Some(t: Term.Function) => canRewriteWithParens(t, true)
case Some(t: Term.FunctionTerm) => canRewriteWithParens(t, true)
case Some(_: Defn) => false
case x => nested || x.isDefined
})
Expand Down Expand Up @@ -153,7 +156,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
case arg :: Nil if t.pos.start == arg.pos.start => onLeftBrace(arg)
case _ => null
}
case t: Term.Function if t.tokens.last.is[Token.RightBrace] =>
case t: Term.FunctionTerm if t.tokens.last.is[Token.RightBrace] =>
if (!okToRemoveFunctionInApplyOrInit(t)) null
else if (okToReplaceFunctionInSingleArgApply(t)) replaceWithLeftParen
else removeToken
Expand Down Expand Up @@ -237,12 +240,12 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
b: Term.Block
)(implicit style: ScalafmtConfig): Boolean =
b.parent.exists {
case f: Term.Function =>
case f: Term.FunctionTerm =>
okToReplaceFunctionInSingleArgApply(f)
case _ => false
}

private def okToReplaceFunctionInSingleArgApply(f: Term.Function)(implicit
private def okToReplaceFunctionInSingleArgApply(f: Term.FunctionTerm)(implicit
style: ScalafmtConfig
): Boolean =
f.parent.flatMap(okToReplaceFunctionInSingleArgApply).exists(_._2 eq f)
Expand All @@ -255,11 +258,11 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {

// single-arg apply of a lambda
// a(b => { c; d }) change to a { b => c; d }
private def okToReplaceFunctionInSingleArgApply(
tree: Tree
)(implicit style: ScalafmtConfig): Option[(Token.LeftParen, Term.Function)] =
private def okToReplaceFunctionInSingleArgApply(tree: Tree)(implicit
style: ScalafmtConfig
): Option[(Token.LeftParen, Term.FunctionTerm)] =
tree match {
case ta @ Term.ArgClause((func: Term.Function) :: Nil, _) if {
case ta @ Term.ArgClause((func: Term.FunctionTerm) :: Nil, _) if {
val body = func.body
(body.is[Term.Block] || func.tokens.last.ne(body.tokens.last)) &&
isParentAnApply(ta) && okToRemoveAroundFunctionBody(body, true)
Expand All @@ -273,7 +276,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
// a single-stat lambda with braces can be converted to one without braces,
// but the reverse conversion isn't always possible
private def okToRemoveFunctionInApplyOrInit(
t: Term.Function
t: Term.FunctionTerm
)(implicit style: ScalafmtConfig): Boolean =
t.parent match {
case Some(p: Term.ArgClause) =>
Expand Down Expand Up @@ -365,7 +368,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
case d: Defn.GivenAlias =>
checkBlockAsBody(b, d.body, noParams(d.paramClauseGroup))

case p: Term.Function if isFunctionWithBraces(p) =>
case p: Term.FunctionTerm if isFunctionWithBraces(p) =>
okToRemoveAroundFunctionBody(b, true)

case _: Term.If =>
Expand Down Expand Up @@ -409,20 +412,22 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
!b.parent.exists(_.is[Term.Select]) ||
t.templ.inits.lengthCompare(1) <= 0 ||
t.templ.stats.nonEmpty || t.tokens.last.is[Token.RightBrace]
case tree => tree.is[Term] && tree.isNot[Term.Function]
case tree => tree.is[Term] && tree.isNot[Term.FunctionTerm]
}

private def okToRemoveBlockWithinApply(
b: Term.Block
)(implicit style: ScalafmtConfig): Boolean =
getSingleStatIfLineSpanOk(b).exists {
case f: Term.Function if needParensAroundParams(f) => false
case f: Term.Function if f.body.is[Term.Block] =>
val fb = f.body
// don't rewrite block if the inner block will be rewritten, too
// sometimes a function body block doesn't have braces
fb.tokens.headOption.exists(_.is[Token.LeftBrace]) &&
!okToRemoveAroundFunctionBody(fb, true)
case f: Term.FunctionTerm =>
!needParensAroundParams(f) && {
val fb = f.body
!fb.is[Term.Block] ||
// don't rewrite block if the inner block will be rewritten, too
// sometimes a function body block doesn't have braces
fb.tokens.headOption.exists(_.is[Token.LeftBrace]) &&
!okToRemoveAroundFunctionBody(fb, true)
}
case _: Term.Assign => false // f({ a = b }) is not the same as f(a = b)
case _ => true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private class RemoveScala3OptionalBraces(ftoks: FormatTokens)
case _: Term.Throw => removeToken
case _: Term.Return => removeToken
case _: Defn.ExtensionGroup => removeToken
case _: Term.Function => removeToken
case _: Term.FunctionTerm => removeToken
case t: Defn.Def =>
if (tree ne t.body) null
else if (ftoks.prevNonComment(ft).left.is[Token.Equals]) removeToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object TreeOps {
case _ => false
}

def isFunctionWithBraces(fun: Term.Function): Boolean =
def isFunctionWithBraces(fun: Term.FunctionTerm): Boolean =
fun.parent match {
case Some(Term.Block(`fun` :: Nil)) => true
case _ => false
Expand Down Expand Up @@ -154,7 +154,7 @@ object TreeOps {
case t @ Term.Block(arg :: Nil) // single-stat block
if t.tokens.headOption // see if opening brace was removed
.exists(x => x.is[Token.LeftBrace] && ftoks(x).left.ne(x)) =>
if (arg.is[Term.Function]) {
if (arg.is[Term.FunctionTerm]) {
// handle rewritten apply { { x => b } } to a { x => b }
val parentApply = findTreeWithParent(t) {
case Term.Block(_) => None
Expand All @@ -167,9 +167,10 @@ object TreeOps {
case t: Term.Apply =>
val ac = t.argClause
ac.values match {
case (f: Term.Function) :: Nil if ac.tokens.lastOption.exists { x =>
x.is[Token.RightParen] && // see if closing paren is now brace
ftoks.prevNonComment(ftoks(x)).left.is[Token.RightBrace]
case (f: Term.FunctionTerm) :: Nil if ac.tokens.lastOption.exists {
x => // see if closing paren is now brace
x.is[Token.RightParen] &&
ftoks.prevNonComment(ftoks(x)).left.is[Token.RightBrace]
} =>
addOne(f)
case _ =>
Expand Down

0 comments on commit 1d6ca94

Please sign in to comment.