Skip to content

Commit

Permalink
somewhat better default display names
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiaggio committed Jul 11, 2024
1 parent f180e39 commit 1d7a91e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package japgolly.scalajs.react.component

import sourcecode.FullName

trait DerivedDisplayName {
protected def derivedDisplayName(implicit name: FullName): String = {
// Heuristic to split the name into package and class name
val parts = name.value.split('.')
val (packageName, className) = parts.span(_.headOption.forall(_.isLower))
className.mkString(".") + Some(packageName).filter(_.nonEmpty).fold("")(_.mkString(" (", ".", ")"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import japgolly.scalajs.react.{Children, CtorType, PropsChildren, Reusability, f
import scala.scalajs.js
import sourcecode.FullName

object ScalaFn {
object ScalaFn extends DerivedDisplayName {

type Component[P, CT[-p, +u] <: CtorType[p, u]] = JsFn.ComponentWithRoot[P, CT, Unmounted[P], Box[P], CT, JsFn.Unmounted[Box[P]]]
type Unmounted[P] = JsFn.UnmountedWithRoot[P, Mounted, Box[P]]
Expand All @@ -26,9 +26,6 @@ object ScalaFn {
.mapUnmounted(_.mapUnmountedProps(_.unbox))
}

private def derivedDisplayName(implicit name: FullName): String =
name.value

@inline def withDisplayName(name: String): DisplayNameApplied =
new DisplayNameApplied(name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object ScalaForwardRef {
type Mounted = JsForwardRef.Mounted
}

object ReactForwardRefInternals {
object ReactForwardRefInternals extends DerivedDisplayName {

sealed trait Dsl extends Any {
protected type R
Expand All @@ -26,9 +26,6 @@ object ReactForwardRefInternals {
(render: (Box[P] with facade.PropsWithChildren, Option[R]) => VdomNode)
(implicit s: CtorType.Summoner.Aux[Box[P], C, CT]): Component[P, RefValue, CT]

private def derivedDisplayName(implicit name: FullName): String =
name.value

final def apply(render: Option[R] => VdomNode)(implicit name: FullName): Component[Unit, RefValue, CtorType.Nullary] =
create(derivedDisplayName)((_, r) => render(r))

Expand Down
2 changes: 1 addition & 1 deletion library/project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ libraryDependencies ++= Seq(
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.10.1")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.10")
addSbtPlugin("org.scala-js" % "sbt-jsdependencies" % "1.0.2")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.15.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0")
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ object HooksTest extends TestSuite {
<.button(^.onClick --> s.modState(_ + 1)))
}

assertEq(comp.displayName, "HooksTest.comp (japgolly.scalajs.react.core)")
test(comp(PI(10))) { t =>
t.assertText("P=PI(10), v1=101, v2=112, v3=113")
t.clickButton(); t.assertText("P=PI(10), v1=104, v2=115, v3=116")
Expand Down

0 comments on commit 1d7a91e

Please sign in to comment.