Repository: scalameta/scalameta
Pull request #3177 (2023-06-12 20:30:55)

Trees: add trait Mod.WithWithin


Patch:
diff --git a/scalameta/trees/shared/src/main/scala/scala/meta/Trees.scala b/scalameta/trees/shared/src/main/scala/scala/meta/Trees.scala
index 116e4bfae..fb3af9508 100644
--- a/scalameta/trees/shared/src/main/scala/scala/meta/Trees.scala
+++ b/scalameta/trees/shared/src/main/scala/scala/meta/Trees.scala
@@ -862,10 +862,11 @@ object Mod {
     @deprecated("Use init instead", "1.9.0")
     def body = init
   }
-  @ast class Private(within: Ref) extends Mod {
+  @branch trait WithWithin extends Mod { def within: Ref }
+  @ast class Private(within: Ref) extends Mod with WithWithin {
     checkFields(within.isWithin)
   }
-  @ast class Protected(within: Ref) extends Mod {
+  @ast class Protected(within: Ref) extends Mod with WithWithin {
     checkFields(within.isWithin)
   }
   @ast class Implicit() extends ParamsType
diff --git a/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/package.scala b/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/package.scala
index 5196ac6cd..548e5e9c2 100644
--- a/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/package.scala
+++ b/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/package.scala
@@ -15,7 +15,7 @@ package object trees {
     def isWithin: Boolean = ref match {
       case _: Ref.Quasi => true
       case _: Name => true
-      case Term.This(Name.Anonymous()) => true
+      case Term.This(_: Name.Anonymous) => true
       case _ => false
     }
   }
@@ -134,7 +134,7 @@ package object trees {
       case _: Type.ApplyInfix => true
       case _: Type.Refine => true
       case _: Type.AnonymousLambda => true
-      case Type.Singleton(Term.This(Name.Anonymous())) => true
+      case Type.Singleton(Term.This(_: Name.Anonymous)) => true
       case _ => false
     }
   }
@@ -146,10 +146,11 @@ package object trees {
       case _ => false
     }
     def accessBoundary: Option[Ref] = mod match {
-      case Mod.Private(Name.Anonymous()) => None
-      case Mod.Protected(Name.Anonymous()) => None
-      case Mod.Private(ref) => Some(ref)
-      case Mod.Protected(ref) => Some(ref)
+      case m: Mod.WithWithin =>
+        m.within match {
+          case r: Name if r.value.isEmpty => None
+          case r => Some(r)
+        }
       case _ => None
     }
     def isNakedAccessMod: Boolean = isAccessMod && accessBoundary.isEmpty


Test patch:
diff --git a/tests/jvm/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala b/tests/jvm/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala
index 824dbd445..634531444 100644
--- a/tests/jvm/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala
+++ b/tests/jvm/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala
@@ -338,6 +338,7 @@ class SurfaceSuite extends FunSuite {
       |scala.meta.Mod.ValParam
       |scala.meta.Mod.VarParam
       |scala.meta.Mod.Variant
+      |scala.meta.Mod.WithWithin
       |scala.meta.MultiSource
       |scala.meta.Name
       |scala.meta.Name.Anonymous
diff --git a/tests/jvm/src/test/scala-2.13/scala/meta/tests/trees/ReflectionSuite.scala b/tests/jvm/src/test/scala-2.13/scala/meta/tests/trees/ReflectionSuite.scala
index 044fd041e..6d4bec3a7 100644
--- a/tests/jvm/src/test/scala-2.13/scala/meta/tests/trees/ReflectionSuite.scala
+++ b/tests/jvm/src/test/scala-2.13/scala/meta/tests/trees/ReflectionSuite.scala
@@ -24,7 +24,7 @@ class ReflectionSuite extends FunSuite {
     val sym = symbolOf[scala.meta.Tree]
     assert(sym.isRoot)
     val root = sym.asRoot
-    assertEquals((root.allBranches.length, root.allLeafs.length), (47, 399))
+    assertEquals((root.allBranches.length, root.allLeafs.length), (48, 400))
   }
 
   test("If") {