#49: pretty printer wraps variables by curly braced in double-quoted strings

This commit is contained in:
z7zmey 2018-07-09 21:37:07 +03:00
parent d2655bd75b
commit 6d554c0468
2 changed files with 29 additions and 8 deletions

View File

@ -553,8 +553,15 @@ func (p *Printer) printScalarEncapsedStringPart(n node.Node) {
func (p *Printer) printScalarEncapsed(n node.Node) {
io.WriteString(p.w, "\"")
for _, nn := range n.(*scalar.Encapsed).Parts {
p.Print(nn)
for _, part := range n.(*scalar.Encapsed).Parts {
switch part.(type) {
case *scalar.EncapsedStringPart:
p.Print(part)
default:
io.WriteString(p.w, "{")
p.Print(part)
io.WriteString(p.w, "}")
}
}
io.WriteString(p.w, "\"")
@ -567,8 +574,15 @@ func (p *Printer) printScalarHeredoc(n node.Node) {
io.WriteString(p.w, nn.Label)
io.WriteString(p.w, "\n")
for _, nn := range nn.Parts {
p.Print(nn)
for _, part := range nn.Parts {
switch part.(type) {
case *scalar.EncapsedStringPart:
p.Print(part)
default:
io.WriteString(p.w, "{")
p.Print(part)
io.WriteString(p.w, "}")
}
}
io.WriteString(p.w, strings.Trim(nn.Label, "\"'"))
@ -1232,7 +1246,14 @@ func (p *Printer) printExprShellExec(n node.Node) {
io.WriteString(p.w, "`")
for _, part := range nn.Parts {
switch part.(type) {
case *scalar.EncapsedStringPart:
p.Print(part)
default:
io.WriteString(p.w, "{")
p.Print(part)
io.WriteString(p.w, "}")
}
}
io.WriteString(p.w, "`")
}

View File

@ -346,7 +346,7 @@ func TestPrintScalarEncapsed(t *testing.T) {
},
})
if o.String() != `"hello $var world"` {
if o.String() != `"hello {$var} world"` {
t.Errorf("TestPrintScalarEncapsed is failed\n")
}
}
@ -365,7 +365,7 @@ func TestPrintScalarHeredoc(t *testing.T) {
})
expected := `<<<LBL
hello $var world
hello {$var} world
LBL`
actual := o.String()
@ -1844,7 +1844,7 @@ func TestPrintShellExec(t *testing.T) {
},
})
expected := "`hello $world!`"
expected := "`hello {$world}!`"
actual := o.String()
if expected != actual {