From fe3f90bf29d96e8e92ae87572a74afb46fe17dec Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Wed, 18 Oct 2023 08:36:00 -0700 Subject: [PATCH 1/2] Add test with trailing commas in tuple --- .../trailingCommasAlwaysSingleLine.stat | 9 +++++++++ .../trailing-commas/trailingCommasNeverSingleLine.stat | 9 +++++++++ .../trailing-commas/trailingCommasPreserve.stat | 10 ++++++++++ 3 files changed, 28 insertions(+) diff --git a/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasAlwaysSingleLine.stat b/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasAlwaysSingleLine.stat index 411a08f455..dc72a029ea 100644 --- a/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasAlwaysSingleLine.stat +++ b/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasAlwaysSingleLine.stat @@ -15,3 +15,12 @@ foo( ) >>> foo(a) +<<< #3663 tuple +val x = ( + "a", + "b", +) +>>> +test does not parse +val x = ("a", "b", ) + ^ diff --git a/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasNeverSingleLine.stat b/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasNeverSingleLine.stat index 84b2afdb2f..fc513cc222 100644 --- a/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasNeverSingleLine.stat +++ b/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasNeverSingleLine.stat @@ -15,3 +15,12 @@ foo( ) >>> foo(a) +<<< #3663 tuple +val x = ( + "a", + "b", +) +>>> +test does not parse +val x = ("a", "b", ) + ^ diff --git a/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasPreserve.stat b/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasPreserve.stat index 84ef63c66f..1f2699f20c 100644 --- a/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasPreserve.stat +++ b/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasPreserve.stat @@ -353,3 +353,13 @@ object a { b: Int /* c */ ) = a + b } +<<< #3663 tuple +val x = ( + "a", + "b", +) +>>> +val x = ( + "a", + "b", +) From 275b2fbe6365925a59e4099673a7cdcf4f7cbd8a Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Wed, 18 Oct 2023 08:46:27 -0700 Subject: [PATCH 2/2] RewriteTrailingCommas: allow tuples as well --- .../scala/org/scalafmt/rewrite/RewriteTrailingCommas.scala | 5 ++++- .../trailing-commas/trailingCommasAlwaysSingleLine.stat | 4 +--- .../trailing-commas/trailingCommasNeverSingleLine.stat | 4 +--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RewriteTrailingCommas.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RewriteTrailingCommas.scala index 96cdc98463..9ed755ebb5 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RewriteTrailingCommas.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RewriteTrailingCommas.scala @@ -42,7 +42,10 @@ private class RewriteTrailingCommas(ftoks: FormatTokens) // comma and paren/bracket/brace need to have the same owner (rightOwner eq nft.meta.rightOwner) && (nft.right match { - case _: Token.RightBracket | _: Token.RightParen => + case _: Token.RightParen => + rightOwner.isAny[Member.SyntaxValuesClause, Member.Tuple] + + case _: Token.RightBracket => rightOwner.is[Member.SyntaxValuesClause] case _: Token.RightBrace => diff --git a/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasAlwaysSingleLine.stat b/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasAlwaysSingleLine.stat index dc72a029ea..c2f4e75fd2 100644 --- a/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasAlwaysSingleLine.stat +++ b/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasAlwaysSingleLine.stat @@ -21,6 +21,4 @@ val x = ( "b", ) >>> -test does not parse -val x = ("a", "b", ) - ^ +val x = ("a", "b") diff --git a/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasNeverSingleLine.stat b/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasNeverSingleLine.stat index fc513cc222..09f7662c9c 100644 --- a/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasNeverSingleLine.stat +++ b/scalafmt-tests/src/test/resources/trailing-commas/trailingCommasNeverSingleLine.stat @@ -21,6 +21,4 @@ val x = ( "b", ) >>> -test does not parse -val x = ("a", "b", ) - ^ +val x = ("a", "b")