Skip to content

Commit

Permalink
go/printer: do not add a space after comment in template part
Browse files Browse the repository at this point in the history
Currently in the Go printer, the code below is already formatted:

a(sth /*l*/)

This change aligns this behaviour for template parts, before this
change, following code:

"\{sth /*l*/}"

got formatted into:

"\{sth /*l*/ }"

now it is unchanged.
  • Loading branch information
mateusz834 committed Oct 5, 2024
1 parent 535e938 commit 6375708
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions printer/testdata/tgo/3.formatted
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ func test(sth string) {
"test\{sth}" /*test*/

"test \{ /*comment*/ test}"
"test \{test /*c*/ }"
"test \{ /*c*/ test /*c*/ }"
"test \{test /*c*/}"
"test \{ /*c*/ test /*c*/}"

"test" /*test*/

"test \{ /*comment*/ test}" /*test*/

"test \{test /*c*/ }" /*test*/
"test \{test /*c*/}" /*test*/

"test \{ /*c*/ test /*c*/ }" /*test*/
"test \{ /*c*/ test /*c*/}" /*test*/

"hello \{ /*c*/ sth}"
/*test*/ "hello \{ /*c*/ sth}"
Expand Down
11 changes: 7 additions & 4 deletions printer/tgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,14 @@ func (p *printer) templateLiteralExpr(x *ast.TemplateLiteralExpr) {
p.setPos(x.OpenPos)
p.print(x.Strings[0])
for i := range x.Parts {
p.print("\\{")
p.setPos(x.Parts[i].Pos())
p.print("\\", token.LBRACE)
p.setPos(x.Parts[i].LBrace)
p.expr(stripParensAlways(x.Parts[i].X))
p.setPos(x.Parts[i].End())
p.print("}")
p.setPos(x.Parts[i].RBrace)
if p.mode&noExtraLinebreak != 0 || p.mode&noExtraBlank != 0 {
panic("unreachable")
}
p.print(noExtraLinebreak|noExtraBlank, token.RBRACE, noExtraLinebreak|noExtraBlank)
p.print(x.Strings[i+1])
}
}
Expand Down

0 comments on commit 6375708

Please sign in to comment.