printer: handle array item unpack in pretty printer

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
This commit is contained in:
Iskander Sharipov 2020-09-01 19:07:40 +03:00
parent cbf185c8f4
commit f8bd680ca0
2 changed files with 23 additions and 0 deletions

View File

@ -972,6 +972,10 @@ func (p *PrettyPrinter) printExprArrayDimFetch(n node.Node) {
func (p *PrettyPrinter) printExprArrayItem(n node.Node) {
nn := n.(*expr.ArrayItem)
if nn.Unpack {
io.WriteString(p.w, "...")
}
if nn.Key != nil {
p.Print(nn.Key)
io.WriteString(p.w, " => ")

View File

@ -1278,6 +1278,25 @@ func TestPrintExprArrayItem(t *testing.T) {
}
}
func TestPrintExprArrayItemUnpack(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&expr.ArrayItem{
Unpack: true,
Val: &expr.Variable{
VarName: &node.Identifier{Value: "world"},
},
})
expected := `...$world`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintExprArray(t *testing.T) {
o := bytes.NewBufferString("")