Skip to content

Commit

Permalink
Do not add Template traits when generating Scala 3 code
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurz committed Mar 12, 2024
1 parent e9e3dd4 commit 75eec7c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
5 changes: 0 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import Dependencies._

import com.typesafe.tools.mima.core.IncompatibleResultTypeProblem
import com.typesafe.tools.mima.core.ProblemFilters
import sbtcrossproject.CrossPlugin.autoImport.crossProject
import org.scalajs.jsenv.nodejs.NodeJSEnv
import java.util.Properties
Expand All @@ -23,9 +21,6 @@ val previousVersion: Option[String] = Some("2.0.1")
val mimaSettings = Seq(
mimaPreviousArtifacts := previousVersion.map(organization.value %% moduleName.value % _).toSet,
mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[IncompatibleResultTypeProblem](
"play.twirl.compiler.TwirlCompiler#TemplateAsFunctionCompiler.getFunctionMapping"
),
)
)

Expand Down
26 changes: 21 additions & 5 deletions compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ object TwirlCompiler {
private[compiler] class ScalaCompat(emitScala3Sources: Boolean) {
val varargSplicesSyntax: String =
if (emitScala3Sources) "*" else ": _*"
def valueOrEmptyIfScala3(value: => String): String =
if (emitScala3Sources) "" else value
}

private[compiler] object ScalaCompat {
Expand Down Expand Up @@ -556,7 +558,7 @@ object TwirlCompiler {
additionalImports: collection.Seq[String],
constructorAnnotations: collection.Seq[String]
): collection.Seq[Any] = {
val (renderCall, f) =
val (renderCall, f, templateType) =
TemplateAsFunctionCompiler.getFunctionMapping(root.params.str, resultType, scalaCompat)

// Get the imports that we need to include, filtering out empty imports
Expand All @@ -574,7 +576,8 @@ class """ :+ name :+ " " :+ constructorAnnotations :+ " " :+ Source(constructor.
package """ :+ packageName :+ """
""" :+ imports :+ """
""" :+ classDeclaration :+ """ extends _root_.play.twirl.api.BaseScalaTemplate[""" :+ resultType :+ """,_root_.play.twirl.api.Format[""" :+ resultType :+ """]](""" :+ formatterType :+ """) {
""" :+ classDeclaration :+ """ extends _root_.play.twirl.api.BaseScalaTemplate[""" :+ resultType :+ """,_root_.play.twirl.api.Format[""" :+ resultType :+ """]](""" :+ formatterType :+ """)""" :+ scalaCompat
.valueOrEmptyIfScala3(s" with $templateType") :+ """ {
/*""" :+ root.comment.map(_.msg).getOrElse("") :+ """*/
def apply""" :+ Source(root.params.str, root.params.pos) :+ """:""" :+ resultType :+ """ = {
Expand Down Expand Up @@ -675,14 +678,14 @@ package """ :+ packageName :+ """
def getFunctionMapping(
signature: String,
returnType: String,
): (String, String) =
): (String, String, String) =
getFunctionMapping(signature, returnType, ScalaCompat(None))

private[compiler] def getFunctionMapping(
signature: String,
returnType: String,
sc: ScalaCompat
): (String, String) = {
): (String, String, String) = {

val params: List[List[Term.Param]] =
try {
Expand Down Expand Up @@ -732,6 +735,19 @@ package """ :+ packageName :+ """
.mkString
)

val templateType = sc.valueOrEmptyIfScala3(
"_root_.play.twirl.api.Template%s[%s%s]".format(
params.flatten.size,
params.flatten
.map {
case ByNameParam(_, paramType) => paramType
case p => filterType(p)
}
.mkString(","),
(if (params.flatten.isEmpty) "" else ",") + returnType
)
)

val f = "def f:%s = %s => apply%s".format(
functionType,
params.map(group => "(" + group.map(_.name.toString).mkString(",") + ")").mkString(" => "),
Expand All @@ -749,7 +765,7 @@ package """ :+ packageName :+ """
.mkString
)

(renderCall, f)
(renderCall, f, templateType)
}

}
Expand Down

0 comments on commit 75eec7c

Please sign in to comment.