From 8adb6af1c3a0baf2df79797ec3f75e9ddd5b4d1b Mon Sep 17 00:00:00 2001 From: Renato Cavalcanti Date: Thu, 11 Jul 2019 09:48:44 +0200 Subject: [PATCH] Fix parenthesis parsing for args block (#233) --- .../src/test/resources/blockWithNestedTuple.scala.html | 5 +++++ .../scala/play/twirl/compiler/test/CompilerSpec.scala | 10 ++++++++++ .../src/main/scala/play/twirl/parser/TwirlParser.scala | 3 +-- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 compiler/src/test/resources/blockWithNestedTuple.scala.html diff --git a/compiler/src/test/resources/blockWithNestedTuple.scala.html b/compiler/src/test/resources/blockWithNestedTuple.scala.html new file mode 100644 index 00000000..d1adf917 --- /dev/null +++ b/compiler/src/test/resources/blockWithNestedTuple.scala.html @@ -0,0 +1,5 @@ +@(args: Seq[(String, String)]) + +@args.zipWithIndex.map { case ((k, v), index) => + @k => @v +} \ No newline at end of file diff --git a/compiler/src/test/scala/play/twirl/compiler/test/CompilerSpec.scala b/compiler/src/test/scala/play/twirl/compiler/test/CompilerSpec.scala index dc397593..5716652d 100644 --- a/compiler/src/test/scala/play/twirl/compiler/test/CompilerSpec.scala +++ b/compiler/src/test/scala/play/twirl/compiler/test/CompilerSpec.scala @@ -241,6 +241,16 @@ class CompilerSpec extends WordSpec with MustMatchers { hello.static(args).toString.trim must be("the-key => the-value") } + "compile successfully (block with nested tuples)" in { + val helper = newCompilerHelper + val hello = helper.compile[(Seq[(String, String)] => Html)]("blockWithNestedTuple.scala.html", "html.blockWithNestedTuple") + + val args = Seq[(String, String)]( + "the-key" -> "the-value" + ) + hello.static(args).toString.trim must be("the-key => the-value") + } + } object Helper { diff --git a/parser/src/main/scala/play/twirl/parser/TwirlParser.scala b/parser/src/main/scala/play/twirl/parser/TwirlParser.scala index 61e2ba08..9b2c5e28 100644 --- a/parser/src/main/scala/play/twirl/parser/TwirlParser.scala +++ b/parser/src/main/scala/play/twirl/parser/TwirlParser.scala @@ -496,9 +496,8 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) { val startsWithParenthesis = result.trim.startsWith("(") || result.trim.stripPrefix("case").trim.startsWith("(") val endsWithParenthesis = result.stripSuffix("=>").trim.endsWith(")") - val exactOneOpenParenthesis = result.count(_ == '(') == 1 - noContainsOpenParenthesis || (startsWithParenthesis && endsWithParenthesis && exactOneOpenParenthesis) + noContainsOpenParenthesis || (startsWithParenthesis && endsWithParenthesis) } val p = input.offset()