From 75eec7cf46e23f0d10fa0a7f24d2f9b44c6c9738 Mon Sep 17 00:00:00 2001 From: Matthias Kurz Date: Tue, 12 Mar 2024 10:14:50 +0100 Subject: [PATCH] Do not add Template traits when generating Scala 3 code --- build.sbt | 5 ---- .../play/twirl/compiler/TwirlCompiler.scala | 26 +++++++++++++++---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/build.sbt b/build.sbt index 30bb5d3b..8ea902d4 100644 --- a/build.sbt +++ b/build.sbt @@ -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 @@ -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" - ), ) ) diff --git a/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala b/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala index 6ee86b93..20f46d3d 100644 --- a/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala +++ b/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala @@ -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 { @@ -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 @@ -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 :+ """ = { @@ -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 { @@ -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(" => "), @@ -749,7 +765,7 @@ package """ :+ packageName :+ """ .mkString ) - (renderCall, f) + (renderCall, f, templateType) } }