Repository: scalameta/scalameta
Pull request #3515 (2024-01-20 17:42:19)

ScalametaParser: keep block around func with mods
Helps with scalameta/scalafmt#3763.


Patch:
diff --git a/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala b/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
index dcf6d93ac..1f213181f 100644
--- a/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
+++ b/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
@@ -2500,7 +2500,8 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>
   private def blockInDelims(f: (=> Term.Block) => Term, allowRepeated: Boolean = false): Term =
     autoPos(f(blockWithinDelims(allowRepeated = allowRepeated)))
 
-  private def blockOnIndent(): Term = blockInDelims(x => dropOuterBlock(indentedOnOpen(x)))
+  private def blockOnIndent(): Term =
+    blockInDelims(x => dropOuterBlock(indentedOnOpen(x), exceptFuncWithMods = true))
   private def blockExprOnIndent(): Term = blockExprPartial(indentedOnOpen)(blockOnIndent())
 
   private def blockOnBrace(allowRepeated: Boolean = false): Term =
@@ -4699,9 +4700,13 @@ object ScalametaParser {
       case _ => term
     }
 
-  private def dropOuterBlock(term: Term.Block): Term =
+  private def dropOuterBlock(term: Term.Block, exceptFuncWithMods: Boolean = false): Term =
     term.stats match {
-      case (stat: Term) :: Nil => stat
+      case (stat: Term) :: Nil =>
+        stat match {
+          case f: Term.Function if exceptFuncWithMods && f.paramClause.mod.isDefined => term
+          case _ => stat
+        }
       case _ => term
     }
 


Test patch:
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/FewerBracesSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/FewerBracesSuite.scala
index f04c3e599..09887a41c 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/FewerBracesSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/FewerBracesSuite.scala
@@ -1668,7 +1668,11 @@ class FewerBracesSuite extends BaseDottySuite {
         |   implicit bar =>
         |      baz
         |""".stripMargin
-    val layout = "def a = foo { implicit bar => baz }"
+    val layout =
+      """|def a = foo {
+         |  implicit bar => baz
+         |}
+         |""".stripMargin
     val tree = Defn.Def(
       Nil,
       tname("a"),
@@ -1676,13 +1680,18 @@ class FewerBracesSuite extends BaseDottySuite {
       None,
       Term.Apply(
         tname("foo"),
-        Term.Function(
-          Term.ParamClause(List(tparam(List(Mod.Implicit()), "bar")), Some(Mod.Implicit())),
-          tname("baz")
+        Term.Block(
+          Term.Function(
+            Term.ParamClause(
+              List(tparam(List(Mod.Implicit()), "bar")),
+              Some(Mod.Implicit())
+            ),
+            tname("baz")
+          ) :: Nil
         ) :: Nil
       )
     )
-    parseAndCheckTree[Stat](code, layout)(tree)
+    runTestAssert[Stat](code, Some(layout))(tree)
   }
 
   test("scalafmt #3763 implicit in braces, no fewer") {