Skip to content

Commit

Permalink
Merge pull request #713 from playframework/mergify/bp/main/pr-712
Browse files Browse the repository at this point in the history
[main] Fix whitespaces after `else{...}` and `elseif{...}` is not shown (backport #712) by @yousuketto
  • Loading branch information
mergify[bot] authored Nov 16, 2023
2 parents 6ee9211 + 75db93e commit 8ce4924
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
6 changes: 2 additions & 4 deletions parser/src/main/scala/play/twirl/parser/TwirlParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ import scala.util.parsing.input.OffsetPosition
* complexExpr : parentheses
* safeExpression : '@' parentheses
* ifExpression : '@' "if" parentheses expressionPart (elseIfCall)* elseCall?
* elseCall : whitespaceNoBreak? "else" expressionPart whitespaceNoBreak?
* elseIfCall : whitespaceNoBreak? "else if" parentheses expressionPart whitespaceNoBreak?
* elseCall : whitespaceNoBreak? "else" whitespaceNoBreak? expressionPart
* elseIfCall : whitespaceNoBreak? "else if" parentheses whitespaceNoBreak? expressionPart
* chainedMethods : ('.' methodCall)+
* expressionPart : chainedMethods | block | (whitespaceNoBreak scalaBlockChained) | parentheses
* expression : '@' methodCall expressionPart*
Expand Down Expand Up @@ -853,7 +853,6 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) {
if (args != null) {
val blk = expressionPart(blockArgsAllowed = true)
if (blk != null) {
whitespaceNoBreak()
Seq(Simple("else if" + args), blk)
} else {
null
Expand All @@ -874,7 +873,6 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) {
whitespaceNoBreak()
val blk = expressionPart(blockArgsAllowed = true)
if (blk != null) {
whitespaceNoBreak()
Seq(Simple("else"), blk)
} else {
null
Expand Down
36 changes: 36 additions & 0 deletions parser/src/test/scala/play/twirl/parser/test/ParserSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,42 @@ class ParserSpec extends AnyWordSpec with Matchers with Inside {
val secondCaseBlockBody = secondCaseBlock(1).asInstanceOf[Block]
secondCaseBlockBody.content(1).asInstanceOf[Plain].text mustBe "Not a nice string "
}

"whitespaces after 'else {...}' as plain" in {
val template = parseTemplateString(
"""@if(condition) {ifblock body} else {elseblock body} Some plain text with whitespaces"""
)
val ifExpressions = template.content(0).asInstanceOf[Display].exp.parts
ifExpressions.head must be(Simple("if(condition)"))
val ifBlockBody = ifExpressions(1).asInstanceOf[Block].content(0)
ifBlockBody mustBe Plain("ifblock body")
val elsePart = ifExpressions(2)
elsePart mustBe Simple("else")
val elseBlockBody = ifExpressions(3).asInstanceOf[Block].content(0)
elseBlockBody mustBe Plain("elseblock body")
val afterIfExpressionOfWhitespaces = template.content(1)
afterIfExpressionOfWhitespaces mustBe Plain(" ")
val afterWhitespaces = template.content(2)
afterWhitespaces mustBe Plain("Some plain text with whitespaces")
}

"whitespaces after 'else if(condition) {...}' as plain" in {
val template = parseTemplateString(
"""@if(condition) {ifblock body} else if(condition2) {elseifblock body} Some plain text with whitespaces"""
)
val ifExpressions = template.content(0).asInstanceOf[Display].exp.parts
ifExpressions.head must be(Simple("if(condition)"))
val ifBlockBody = ifExpressions(1).asInstanceOf[Block].content(0)
ifBlockBody mustBe Plain("ifblock body")
val elseIfPart = ifExpressions(2)
elseIfPart mustBe Simple("else if(condition2)")
val elseBlockBody = ifExpressions(3).asInstanceOf[Block].content(0)
elseBlockBody mustBe Plain("elseifblock body")
val afterIfExpressionOfWhitespaces = template.content(1)
afterIfExpressionOfWhitespaces mustBe Plain(" ")
val afterWhitespaces = template.content(2)
afterWhitespaces mustBe Plain("Some plain text with whitespaces")
}
}

"handle local definitions" when {
Expand Down

0 comments on commit 8ce4924

Please sign in to comment.