From daee3e1c695795d60dcef6380cf388c5d41de5e5 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 6 Oct 2024 20:00:17 -0700 Subject: [PATCH] ConvertToNewScala3Syntax: remove `:_`/`@_` as unit --- .../rewrite/ConvertToNewScala3Syntax.scala | 23 +++++++++++++++---- .../src/test/resources/scala3/Vararg.stat | 14 +---------- .../test/scala/org/scalafmt/FormatTests.scala | 2 +- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/ConvertToNewScala3Syntax.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/ConvertToNewScala3Syntax.scala index 2395d18e7c..cc45cb6bf1 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/ConvertToNewScala3Syntax.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/ConvertToNewScala3Syntax.scala @@ -34,7 +34,16 @@ private class ConvertToNewScala3Syntax(implicit val ftoks: FormatTokens) style: ScalafmtConfig, ): Option[Replacement] = Option { val flag = style.rewrite.scala3.newSyntax - def left = ftoks.prevNonComment(ft).left + def pft = ftoks.prevNonComment(ft) + def left = pft.left + + def removeAndClaimUnderscore(): Replacement = { + val nft = ftoks.nextNonCommentAfter(ft) + if (nft.right.is[Token.Underscore]) removeToken(nft.idx :: Nil) else null + } + def removeUnderscoreIfPreviousRemoved(): Replacement = + if (session.isRemovedOnLeft(pft, ok = true)) removeToken else null + ft.right match { case _: Token.LeftParen if flag.control && dialect.allowQuietSyntax => @@ -47,15 +56,19 @@ private class ConvertToNewScala3Syntax(implicit val ftoks: FormatTokens) case _: Token.Colon if flag.deprecated && dialect.allowPostfixStarVarargSplices => + // trick: to get "*", just remove ":" and "_" ft.meta.rightOwner match { - case t: Term.Repeated if isSimpleRepeated(t) => removeToken // trick: to get "*", just remove ":" and "_" + case t: Term.Repeated if isSimpleRepeated(t) => + removeAndClaimUnderscore() + case Pat.Bind(_, _: Pat.SeqWildcard) => removeAndClaimUnderscore() case _ => null } case _: Token.At if flag.deprecated && dialect.allowPostfixStarVarargSplices => + // trick: to get "*", just remove "@" and "_" ft.meta.rightOwner match { - case Pat.Bind(_, _: Pat.SeqWildcard) => removeToken // trick: to get "*", just remove "@" and "_" + case Pat.Bind(_, _: Pat.SeqWildcard) => removeAndClaimUnderscore() case _ => null } @@ -68,10 +81,10 @@ private class ConvertToNewScala3Syntax(implicit val ftoks: FormatTokens) t.parent.is[Type.ArgClause] => replaceTokenIdent("?", ft.right) case t: Term.Repeated if dialect.allowPostfixStarVarargSplices && isSimpleRepeated(t) => - removeToken // see above, under Colon + removeUnderscoreIfPreviousRemoved() case t: Pat.SeqWildcard if dialect.allowPostfixStarVarargSplices && - t.parent.is[Pat.Bind] => removeToken // see above, under At + t.parent.is[Pat.Bind] => removeUnderscoreIfPreviousRemoved() case _ => null } diff --git a/scalafmt-tests/shared/src/test/resources/scala3/Vararg.stat b/scalafmt-tests/shared/src/test/resources/scala3/Vararg.stat index 58d40e039e..c4b2818d53 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/Vararg.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/Vararg.stat @@ -32,23 +32,11 @@ node match { false } >>> -test does not parse: [dialect scala3] illegal start of simple pattern - Node3(TERMREFpkg, Seq("scala.annotation.internal"), zxc), - children:* - ) - ^ - ) => - children.filterNot(_.tag == IMPORT).exists { - case Node3(TYPEDEF, Seq("SourceFile"), _) => true -====== full result: ====== node match { case Node3( PACKAGE, _, - Seq( - Node3(TERMREFpkg, Seq("scala.annotation.internal"), zxc), - children:* - ) + Seq(Node3(TERMREFpkg, Seq("scala.annotation.internal"), zxc), children*) ) => children.filterNot(_.tag == IMPORT).exists { case Node3(TYPEDEF, Seq("SourceFile"), _) => true diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala index bb4cd62974..cc9b150f1e 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala +++ b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala @@ -137,7 +137,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions { override def afterAll(): Unit = { logger.debug(s"Total explored: ${Debug.explored}") if (!onlyUnit && !onlyManual) - assertEquals(Debug.explored, 1785581, "total explored") + assertEquals(Debug.explored, 1785622, "total explored") val results = debugResults.result() // TODO(olafur) don't block printing out test results. // I don't want to deal with scalaz's Tasks :'(