Skip to content

Commit

Permalink
FormatWriter: trailing comma with a removed paren
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Mar 27, 2024
1 parent be33ce0 commit bb5b96a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,12 @@ class FormatWriter(formatOps: FormatOps) {
style.getTrailingCommas match {
// remove comma if no newline
case TrailingCommas.keep
if tok.left.is[T.Comma] && isClosedDelimWithNewline(false) =>
if tok.left.is[T.Comma] && (isClosedDelimWithNewline(false) ||
(tok.meta.leftOwner match {
// closing paren could have been removed by rewrite
case x: Term.ArgClause => tokens.getLast(x) eq tok
case _ => false
})) =>
sb.setLength(sb.length - 1)
if (!tok.right.is[T.RightParen]) ws(1)
else if (style.spaces.inParentheses) {
Expand Down
18 changes: 2 additions & 16 deletions scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1642,17 +1642,10 @@ foo.mtd({
},
)
>>>
test does not parse: [dialect scala213] end of file expected but , found
foo.mtd {
case x => x + 1
case y => y + 1
^
},
====== full result: ======
foo.mtd {
case x => x + 1
case y => y + 1
},
}
<<< rewrite with trailing commas: partial func, !allowFolding
rewrite.trailingCommas.style = keep
rewrite.trailingCommas.allowFolding = false
Expand All @@ -1663,17 +1656,10 @@ foo.mtd({
},
)
>>>
test does not parse: [dialect scala213] end of file expected but , found
foo.mtd {
case x => x + 1
case y => y + 1
^
},
====== full result: ======
foo.mtd {
case x => x + 1
case y => y + 1
},
}
<<< rewrite with trailing commas: match, allowFolding
rewrite.trailingCommas.style = keep
rewrite.trailingCommas.allowFolding = true
Expand Down

0 comments on commit bb5b96a

Please sign in to comment.