Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedundantBraces: remove parens and trailing comma #3868

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,29 +144,60 @@ class RedundantBraces(implicit val ftoks: FormatTokens)

private def onRightParen(left: Replacement)(implicit
ft: FormatToken,
session: Session,
style: ScalafmtConfig
): (Replacement, Replacement) = left.how match {
case ReplacementType.Remove => (left, removeToken)
case ReplacementType.Remove =>
val resOpt = getRightBraceBeforeRightParen(false).map { rb =>
// we'll use right brace later, when applying fewer-braces rewrite
(left, removeToken)
}
resOpt.orNull

case ReplacementType.Replace if {
val lft = left.ft
val ro = ft.meta.rightOwner
(lft.meta.rightOwner eq ro) &&
lft.right.is[Token.LeftBrace] &&
okToReplaceFunctionInSingleArgApply(ro).isDefined
lft.right.is[Token.LeftBrace]
} =>
val pft = ftoks.prevNonComment(ft)
val rb = pft.left
if (rb.is[Token.RightBrace]) {
val pftOpt = getRightBraceBeforeRightParen(true)
def replaceIfAfterRightBrace = pftOpt.map { pft =>
val rb = pft.left
// move right to the end of the function
val rType = new ReplacementType.RemoveAndResurrect(ftoks.prev(pft))
left -> replaceToken("}", rtype = rType) {
// create a shifted token so that any child tree wouldn't own it
new Token.RightBrace(rb.input, rb.dialect, rb.start + 1)
}
} else null // don't know how to Replace
}
replaceIfAfterRightBrace.orNull // don't know how to Replace
case _ => null
}

private def getRightBraceBeforeRightParen(shouldBeRemoved: Boolean)(implicit
ft: FormatToken,
session: Session,
style: ScalafmtConfig
): Option[FormatToken] = {
val pft = ftoks.prevNonComment(ft)
val ok = pft.left match {
case _: Token.Comma => // looks like trailing comma
val pft2 = ftoks.prevNonCommentBefore(pft)
pft2.left.is[Token.RightBrace] &&
session.isRemovedOnLeft(pft2, true) == shouldBeRemoved &&
session.isRemovedOnLeftOpt(pft).getOrElse {
val crt = ftoks.prev(pft)
val crepl = Replacement(this, crt, ReplacementType.Remove, style)
session.claim(crepl)(crt)
true
}
case _: Token.RightBrace =>
!session.isRemovedOnLeft(pft, !shouldBeRemoved)
case _ => false
}
if (ok) Some(pft) else None
}

private def onLeftBrace(implicit
ft: FormatToken,
session: Session,
Expand Down
33 changes: 16 additions & 17 deletions scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1477,15 +1477,14 @@ object a {
)
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
object a {
- mtd1(x => x + 1) + mtd2(x => x + 1 x +2)
+ mtd1(x => x + 1) + mtd2(x =>
+ x + 1
+ x + 2,
+ )
}
object a {
mtd1 { x =>
x + 1
} + mtd2 { x =>
x + 1
x + 2
}
}
<<< rewrite with trailing commas: func in parens, infix after, !allowFolding
rewrite.trailingCommas.style = keep
rewrite.trailingCommas.allowFolding = false
Expand All @@ -1503,14 +1502,14 @@ object a {
)
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
) + mtd2(
- x => x + 1 x +2,
+ x =>
+ x + 1
+ x + 2,
)
object a {
mtd1 { x =>
x + 1
} + mtd2 { x =>
x + 1
x + 2
}
}
<<< rewrite with trailing commas: func in parens and braces, allowFolding
rewrite.rules = [RedundantBraces, RedundantParens]
rewrite.trailingCommas.style = keep
Expand Down
62 changes: 31 additions & 31 deletions scalafmt-tests/src/test/resources/scala3/FewerBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1934,18 +1934,18 @@ foo
)
>>>
foo
.mtd1(x => x + 1)
.mtd2(x =>
.mtd1 { x =>
x + 1
}
.mtd2 { x =>
x + 1
x + 2
,
)
.mtd3(x =>
}
.mtd3 { x =>
x + 1
x + 2
x + 3
,
)
}
<<< rewrite to fewer braces: func in parens, trailing comma, !allowFolding
rewrite.rules = [RedundantBraces]
rewrite.trailingCommas.style = keep
Expand Down Expand Up @@ -1975,22 +1975,18 @@ foo
)
>>>
foo
.mtd1(
x => x + 1,
)
.mtd2(
x =>
x + 1
x + 2
,
)
.mtd3(
x =>
x + 1
x + 2
x + 3
,
)
.mtd1 { x =>
x + 1
}
.mtd2 { x =>
x + 1
x + 2
}
.mtd3 { x =>
x + 1
x + 2
x + 3
}
<<< rewrite to fewer braces: func in parens, infix after
rewrite.rules = [RedundantBraces]
rewrite.scala3.removeOptionalBraces = {
Expand All @@ -2017,14 +2013,18 @@ object a {
}
>>>
object a:
mtd1(x => x + 1) + mtd2(x =>
x + 1
x + 2
) + mtd3(x =>
x + 1
x + 2
x + 3
)
mtd1 { x =>
x + 1
}
+ mtd2 { x =>
x + 1
x + 2
}
+ mtd3 { x =>
x + 1
x + 2
x + 3
}
<<< rewrite to fewer braces: func in parens and braces
rewrite.rules = [RedundantBraces, RedundantParens]
rewrite.scala3.removeOptionalBraces = {
Expand Down
36 changes: 16 additions & 20 deletions scalafmt-tests/src/test/resources/scala3/FewerBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1693,14 +1693,14 @@ foo
},
)
>>>
foo.mtd1(x => x + 1).mtd2(x =>
foo.mtd1(x => x + 1).mtd2 { x =>
x + 1
x + 2,
).mtd3(x =>
x + 2
}.mtd3 { x =>
x + 1
x + 2
x + 3,
)
x + 3
}
<<< rewrite to fewer braces: func in parens, trailing comma, !allowFolding
rewrite.rules = [RedundantBraces]
rewrite.trailingCommas.style = always
Expand Down Expand Up @@ -1729,18 +1729,14 @@ foo
},
)
>>>
foo.mtd1(
x => x + 1,
).mtd2(
x =>
x + 1
x + 2,
).mtd3(
x =>
x + 1
x + 2
x + 3,
)
foo.mtd1 { x => x + 1 }.mtd2 { x =>
x + 1
x + 2
}.mtd3 { x =>
x + 1
x + 2
x + 3
}
<<< rewrite to fewer braces: func in parens, infix after
rewrite.rules = [RedundantBraces]
rewrite.scala3.removeOptionalBraces = {
Expand All @@ -1767,14 +1763,14 @@ object a {
}
>>>
object a:
mtd1(x => x + 1) + mtd2(x =>
mtd1(x => x + 1) + mtd2 { x =>
x + 1
x + 2
) + mtd3(x =>
} + mtd3 { x =>
x + 1
x + 2
x + 3
)
}
<<< rewrite to fewer braces: func in parens and braces
rewrite.rules = [RedundantBraces, RedundantParens]
rewrite.scala3.removeOptionalBraces = {
Expand Down
70 changes: 37 additions & 33 deletions scalafmt-tests/src/test/resources/scala3/FewerBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1905,20 +1905,21 @@ foo
)
>>>
foo
.mtd1(x =>
x + 1,
)
.mtd2(x =>
x + 1
x + 2
,
)
.mtd3(x =>
x + 1
x + 2
x + 3
,
)
.mtd1 {
x =>
x + 1
}
.mtd2 {
x =>
x + 1
x + 2
}
.mtd3 {
x =>
x + 1
x + 2
x + 3
}
<<< rewrite to fewer braces: func in parens, trailing comma, !allowFolding
rewrite.rules = [RedundantBraces]
rewrite.trailingCommas.style = keep
Expand Down Expand Up @@ -1948,23 +1949,21 @@ foo
)
>>>
foo
.mtd1(
.mtd1 {
x =>
x + 1,
)
.mtd2(
x + 1
}
.mtd2 {
x =>
x + 1
x + 2
,
)
.mtd3(
}
.mtd3 {
x =>
x + 1
x + 2
x + 3
,
)
}
<<< rewrite to fewer braces: func in parens, infix after
rewrite.rules = [RedundantBraces]
rewrite.scala3.removeOptionalBraces = {
Expand All @@ -1991,16 +1990,21 @@ object a {
}
>>>
object a:
mtd1(x =>
x + 1
) + mtd2(x =>
x + 1
x + 2
) + mtd3(x =>
x + 1
x + 2
x + 3
)
mtd1 {
x =>
x + 1
}
+ mtd2 {
x =>
x + 1
x + 2
}
+ mtd3 {
x =>
x + 1
x + 2
x + 3
}
<<< rewrite to fewer braces: func in parens and braces
rewrite.rules = [RedundantBraces, RedundantParens]
rewrite.scala3.removeOptionalBraces = {
Expand Down
Loading
Loading