Skip to content

Commit

Permalink
ConvertToNewScala3Syntax: use the new parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 28, 2023
1 parent e9363ce commit 3f34bf3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,41 @@ private class ConvertToNewScala3Syntax(ftoks: FormatTokens)
ft: FormatToken,
style: ScalafmtConfig
): Option[Replacement] = Option {
val flag = style.rewrite.scala3.newSyntax
def left = ftoks.prevNonComment(ft).left
ft.right match {

case _: Token.LeftParen if dialect.allowSignificantIndentation =>
case _: Token.LeftParen
if flag.control && dialect.allowSignificantIndentation =>
ft.meta.rightOwner match {
case _: Term.If if ftoks.prevNonComment(ft).left.is[Token.KwIf] =>
case _: Term.If if left.is[Token.KwIf] =>
removeToken
case _: Term.While
if ftoks.prevNonComment(ft).left.is[Token.KwWhile] =>
case _: Term.While if left.is[Token.KwWhile] =>
removeToken
case _: Term.For if ftoks.prevNonComment(ft).left.is[Token.KwFor] =>
case _: Term.For if left.is[Token.KwFor] =>
removeToken
case _: Term.ForYield
if ftoks.prevNonComment(ft).left.is[Token.KwFor] =>
case _: Term.ForYield if left.is[Token.KwFor] =>
removeToken
case _ => null
}

case _: Token.Colon if dialect.allowPostfixStarVarargSplices =>
case _: Token.Colon
if flag.deprecated && dialect.allowPostfixStarVarargSplices =>
ft.meta.rightOwner match {
case t: Term.Repeated if isSimpleRepeated(t) =>
removeToken // trick: to get "*", just remove ":" and "_"
case _ => null
}

case _: Token.At if dialect.allowPostfixStarVarargSplices =>
case _: Token.At
if flag.deprecated && dialect.allowPostfixStarVarargSplices =>
ft.meta.rightOwner match {
case Pat.Bind(_, _: Pat.SeqWildcard) =>
removeToken // trick: to get "*", just remove "@" and "_"
case _ => null
}

case _: Token.Underscore =>
case _: Token.Underscore if flag.deprecated =>
ft.meta.rightOwner match {
case _: Importee.Wildcard if dialect.allowStarWildcardImport =>
replaceTokenIdent("*", ft.right)
Expand All @@ -81,14 +84,15 @@ private class ConvertToNewScala3Syntax(ftoks: FormatTokens)
case _ => null
}

case _: Token.RightArrow if dialect.allowAsForImportRename =>
case _: Token.RightArrow
if flag.deprecated && dialect.allowAsForImportRename =>
ft.meta.rightOwner match {
case _: Importee.Rename | _: Importee.Unimport =>
replaceTokenIdent("as", ft.right)
case _ => null
}

case Token.Ident("*") =>
case Token.Ident("*") if flag.deprecated =>
ft.meta.rightOwner match {
case _: Type.AnonymousParam
if dialect.allowUnderscoreAsTypePlaceholder =>
Expand All @@ -106,18 +110,19 @@ private class ConvertToNewScala3Syntax(ftoks: FormatTokens)
ft: FormatToken,
style: ScalafmtConfig
): Option[(Replacement, Replacement)] = Option {
def nextRight = ftoks.nextNonComment(ftoks.next(ft)).right
ft.right match {

case x: Token.RightParen if left.how eq ReplacementType.Remove =>
ft.meta.rightOwner match {
case _: Term.If =>
if (!ftoks.nextNonComment(ftoks.next(ft)).right.is[Token.KwThen])
if (!nextRight.is[Token.KwThen])
replaceToken("then")(
new Token.KwThen(x.input, x.dialect, x.start)
)
else removeToken
case _: Term.While | _: Term.For =>
if (!ftoks.nextNonComment(ftoks.next(ft)).right.is[Token.KwDo])
if (!nextRight.is[Token.KwDo])
replaceToken("do")(new Token.KwDo(x.input, x.dialect, x.start))
else removeToken
case _ => null
Expand Down
13 changes: 8 additions & 5 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1235,8 +1235,8 @@ object a {
object a:
val a =
if a then // scalafmt: { rewrite.scala3.newSyntax.control = false }
if aa then // scalafmt: { rewrite.scala3.newSyntax.control = true }
aaa
if (aa) // scalafmt: { rewrite.scala3.newSyntax.control = true }
aaa
// c1
else b
val a =
Expand Down Expand Up @@ -1300,11 +1300,14 @@ object a:
for (a <- b) yield for a <- b yield foo
while a > 0 do while a > 0 do foo
// scalafmt: { rewrite.scala3.newSyntax.control = false }
while a > 0 do while a > 0 do foo
while (a > 0)
while a > 0 do foo
// scalafmt: { rewrite.scala3.newSyntax.control = true }
for a <- b do for a <- b do foo
// scalafmt: { rewrite.scala3.newSyntax.control = false }
for a <- b do for a <- b do foo
for (a <- b) do
for (a <- b)
foo
// scalafmt: { rewrite.scala3.newSyntax.control = true }
for a <- b do for a <- b do foo
end a
Expand Down Expand Up @@ -1412,7 +1415,7 @@ object a:
import java as j
import Predef.{augmentString as _}
// scalafmt: { rewrite.scala3.newSyntax.deprecated = false }
import Predef.{augmentString as _}
import Predef.{augmentString => _}
<<< rewrite to new syntax, imports, scala2-source3
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = yes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1162,8 +1162,8 @@ object a {
object a:
val a =
if a then // scalafmt: { rewrite.scala3.newSyntax.control = false }
if aa then // scalafmt: { rewrite.scala3.newSyntax.control = true }
aaa
if (aa) // scalafmt: { rewrite.scala3.newSyntax.control = true }
aaa
// c1
else b
val a =
Expand Down Expand Up @@ -1227,11 +1227,11 @@ object a:
for (a <- b) yield for a <- b yield foo
while a > 0 do while a > 0 do foo
// scalafmt: { rewrite.scala3.newSyntax.control = false }
while a > 0 do while a > 0 do foo
while (a > 0) while a > 0 do foo
// scalafmt: { rewrite.scala3.newSyntax.control = true }
for a <- b do for a <- b do foo
// scalafmt: { rewrite.scala3.newSyntax.control = false }
for a <- b do for a <- b do foo
for (a <- b) do for (a <- b) foo
// scalafmt: { rewrite.scala3.newSyntax.control = true }
for a <- b do for a <- b do foo
end a
Expand Down Expand Up @@ -1339,7 +1339,7 @@ object a:
import java as j
import Predef.{augmentString as _}
// scalafmt: { rewrite.scala3.newSyntax.deprecated = false }
import Predef.{augmentString as _}
import Predef.{augmentString => _}
<<< rewrite to new syntax, imports, scala2-source3
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = yes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1242,8 +1242,8 @@ object a {
object a:
val a =
if a then // scalafmt: { rewrite.scala3.newSyntax.control = false }
if aa then // scalafmt: { rewrite.scala3.newSyntax.control = true }
aaa
if (aa) // scalafmt: { rewrite.scala3.newSyntax.control = true }
aaa
// c1
else
b
Expand Down Expand Up @@ -1314,17 +1314,17 @@ object a:
while a > 0 do
foo
// scalafmt: { rewrite.scala3.newSyntax.control = false }
while a > 0 do
while a > 0 do
foo
while (a > 0)
while a > 0 do
foo
// scalafmt: { rewrite.scala3.newSyntax.control = true }
for a <- b do
for a <- b do
foo
// scalafmt: { rewrite.scala3.newSyntax.control = false }
for a <- b do
for a <- b do
foo
for (a <- b) do
for (a <- b)
foo
// scalafmt: { rewrite.scala3.newSyntax.control = true }
for a <- b do
for a <- b do
Expand Down Expand Up @@ -1434,7 +1434,7 @@ object a:
import java as j
import Predef.{augmentString as _}
// scalafmt: { rewrite.scala3.newSyntax.deprecated = false }
import Predef.{augmentString as _}
import Predef.{augmentString => _}
<<< rewrite to new syntax, imports, scala2-source3
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = yes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,8 @@ object a {
object a:
val a =
if a then // scalafmt: { rewrite.scala3.newSyntax.control = false }
if aa then // scalafmt: { rewrite.scala3.newSyntax.control = true }
aaa
if (aa) // scalafmt: { rewrite.scala3.newSyntax.control = true }
aaa
// c1
else
b
Expand Down Expand Up @@ -1396,19 +1396,19 @@ object a:
foo
end while
// scalafmt: { rewrite.scala3.newSyntax.control = false }
while a > 0 do
while a > 0 do
foo
while (a > 0)
while a > 0 do
foo
end while
// scalafmt: { rewrite.scala3.newSyntax.control = true }
for a <- b do
for a <- b do
foo
end for
// scalafmt: { rewrite.scala3.newSyntax.control = false }
for a <- b do
for a <- b do
foo
for (a <- b) do
for (a <- b)
foo
end for
// scalafmt: { rewrite.scala3.newSyntax.control = true }
for a <- b do
Expand Down Expand Up @@ -1554,7 +1554,7 @@ object a:
import java as j
import Predef.{augmentString as _}
// scalafmt: { rewrite.scala3.newSyntax.deprecated = false }
import Predef.{augmentString as _}
import Predef.{augmentString => _}
<<< rewrite to new syntax, imports, scala2-source3
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = yes
Expand Down

0 comments on commit 3f34bf3

Please # to comment.