Skip to content

Commit

Permalink
Merge pull request #1052 from playframework/mergify/bp/2.10.x/pr-1047
Browse files Browse the repository at this point in the history
[2.10.x] Make format generation idempotent (backport #1047) by @reimai
  • Loading branch information
mergify[bot] authored Jun 17, 2024
2 parents a618a27 + d7f923b commit 34d9b20
Showing 1 changed file with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package play.api.libs.json

import scala.annotation.tailrec
import scala.collection.mutable
import scala.reflect.macros.blackbox

/**
Expand Down Expand Up @@ -555,25 +556,26 @@ class JsMacroImpl(val c: blackbox.Context) {
val tpeSym = atag.tpe.typeSymbol.asClass

@tailrec
def allSubclasses(path: List[Symbol], subclasses: Set[Type]): Set[Type] = path match {
case (cls: ClassSymbol) :: tail if tpeSym != cls && cls.selfType.baseClasses.contains(tpeSym) => {
if (cls.typeParams.nonEmpty)
c.warning(c.enclosingPosition, s"cannot handle class ${cls.fullName}: type parameter not supported")
val newSub = if (cls.typeParams.isEmpty) Set(cls.selfType) else Set.empty
allSubclasses(tail, subclasses ++ newSub)
}
def allSubclasses(path: List[Symbol], subclasses: mutable.LinkedHashSet[Type]): mutable.LinkedHashSet[Type] =
path match {
case (cls: ClassSymbol) :: tail if tpeSym != cls && cls.selfType.baseClasses.contains(tpeSym) => {
if (cls.typeParams.nonEmpty)
c.warning(c.enclosingPosition, s"cannot handle class ${cls.fullName}: type parameter not supported")
val newSub = if (cls.typeParams.isEmpty) Set(cls.selfType) else Set.empty
allSubclasses(tail, subclasses ++ newSub)
}

case (o: ModuleSymbol) :: tail
if o.companion == NoSymbol // not a companion object
&& o.typeSignature.baseClasses.contains(tpeSym) =>
allSubclasses(tail, subclasses ++ Set(o.typeSignature))
case (o: ModuleSymbol) :: tail
if o.companion == NoSymbol // not a companion object
&& o.typeSignature.baseClasses.contains(tpeSym) =>
allSubclasses(tail, subclasses ++ Set(o.typeSignature))

case _ :: tail => allSubclasses(tail, subclasses)
case _ => subclasses
}
case _ :: tail => allSubclasses(tail, subclasses)
case _ => subclasses
}

if (tpeSym.isSealed && tpeSym.isAbstract) {
Some(allSubclasses(tpeSym.owner.typeSignature.decls.sorted, Set.empty).toList)
Some(allSubclasses(tpeSym.owner.typeSignature.decls.sorted, mutable.LinkedHashSet[Type]()).toList)
} else None
}

Expand Down

0 comments on commit 34d9b20

Please sign in to comment.