Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template parameters can be deprecated #273

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,11 @@ object TwirlCompiler {
additionalImports: collection.Seq[String],
constructorAnnotations: collection.Seq[String]
): collection.Seq[Any] = {
val (renderCall, f, templateType) = TemplateAsFunctionCompiler.getFunctionMapping(root.params.str, resultType)
val (renderCall, f, templateType) = TemplateAsFunctionCompiler.getFunctionMapping(
root.templateFunctionName.map(_.str).getOrElse("f"),
root.params.str,
resultType
)

// Get the imports that we need to include, filtering out empty imports
val imports: Seq[Any] = Seq(additionalImports.map(i => Seq("import ", i, "\n")), formatImports(root.topImports))
Expand Down Expand Up @@ -520,7 +524,7 @@ package """ :+ packageName :+ """
/** The maximum time in milliseconds to wait for a compiler response to finish. */
private val Timeout = 10000

def getFunctionMapping(signature: String, returnType: String): (String, String, String) = synchronized {
def getFunctionMapping(fn: String, signature: String, returnType: String): (String, String, String) = synchronized {

def filterType(t: String) =
t.replace("_root_.scala.<repeated>", "Array")
Expand Down Expand Up @@ -584,7 +588,8 @@ package """ :+ packageName :+ """
(if (params.flatten.isEmpty) "" else ",") + returnType
)

val f = "def f:%s = %s => apply%s".format(
val f = "def %s:%s = %s => apply%s".format(
fn,
functionType,
params.map(group => "(" + group.map(_.name.toString).mkString(",") + ")").mkString(" => "),
params
Expand Down
1 change: 1 addition & 0 deletions parser/src/main/scala/play/twirl/parser/TreeNodes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ object TreeNodes {
name: PosString,
constructor: Option[Constructor],
comment: Option[Comment],
templateFunctionName: Option[PosString],
params: PosString,
topImports: collection.Seq[Simple],
imports: collection.Seq[Simple],
Expand Down
27 changes: 26 additions & 1 deletion parser/src/main/scala/play/twirl/parser/TwirlParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) {
if (check("{")) {
val (imports, localDefs, templates, mixeds) = templateContent()
if (check("}"))
result = Template(templDecl._1, None, None, templDecl._2, Nil, imports, localDefs, templates, mixeds)
result = Template(templDecl._1, None, None, None, templDecl._2, Nil, imports, localDefs, templates, mixeds)
}
}
}
Expand Down Expand Up @@ -931,6 +931,29 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) {
} else None
}

/**
* Parse the template function name, if it exists
*/
private def maybeTemplateFunctionName(): Option[PosString] = {
if (check("@templateFunctionName(")) {
val p = input.offset()
whitespaceNoBreak
val name = stringLiteral("\"", "\\")
if (name != null) {
whitespaceNoBreak
if (!check(")")) {
error("Expected closing parenthesis after template function name")
None
} else {
Some(position(PosString(name), p))
}
} else {
error("Expected template function name")
None
}
} else None
}

/**
* Parse the template arguments, if they exist
*/
Expand Down Expand Up @@ -965,12 +988,14 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) {
}
}
val args = maybeTemplateArgs()
val templateFunctionName = maybeTemplateFunctionName()
val (imports, localDefs, templates, mixeds) = templateContent()

val template = Template(
PosString(""),
constructor,
argsComment,
templateFunctionName,
args.getOrElse(PosString("()")),
topImports,
imports,
Expand Down