Skip to content

Commit

Permalink
ConvertToNewScala3Syntax: remove :_/@_ as unit
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Oct 7, 2024
1 parent 4368e04 commit daee3e1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
14 changes: 1 addition & 13 deletions scalafmt-tests/shared/src/test/resources/scala3/Vararg.stat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 :'(
Expand Down

0 comments on commit daee3e1

Please sign in to comment.