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

RewriteTrailingCommas: handle optional braces case #4059

Merged
merged 2 commits into from
Jun 17, 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 @@ -55,25 +55,26 @@ private class RewriteTrailingCommas(implicit val ftoks: FormatTokens)
private[rewrite] def shouldRemove(
ft: FormatToken,
)(implicit session: Session): Boolean = ft.right.is[Token.Comma] && {
val rightOwner = ft.meta.rightOwner
val nft = ftoks.nextNonCommentAfter(ft)
def delimOwner = nft.meta.rightOwner

// comma and paren/bracket/brace need to have the same owner
(rightOwner eq nft.meta.rightOwner) &&
(nft.right match {
case rp: Token.RightParen => rightOwner
// comma and paren/bracket/brace should generally have the same owner
// however, with optional-braces comma could be before outdent
// and hence owned by the previous expression
nft.right match {
case rp: Token.RightParen => delimOwner
.isAny[Member.SyntaxValuesClause, Member.Tuple] ||
ftoks.matchingOpt(rp).exists { lp =>
val claimant = session.claimedRule(ftoks.justBefore(lp))
claimant.forall(_.rule.isInstanceOf[RedundantParens])
}

case _: Token.RightBracket => rightOwner.is[Member.SyntaxValuesClause]
case _: Token.RightBracket => delimOwner.is[Member.SyntaxValuesClause]

case _: Token.RightBrace => rightOwner.is[Importer]
case _: Token.RightBrace => delimOwner.is[Importer]

case _ => false
})
}
}

override def onRight(lt: Replacement, hasFormatOff: Boolean)(implicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,34 @@ val x = (
val x = (
x => x + 1,
)
<<< #4058 1
maxColumn = 16
runner.dialect = scala3
===
f(
if true then 0
else
1234567890
,
)
>>>
f(
if true then 0
else
1234567890,
)
<<< #4058 2
maxColumn = 16
runner.dialect = scala3
===
f(
if true then 0
else
1234567890,
)
>>>
f(
if true then 0
else
1234567890,
)
Loading