Merge pull request #116 from quasilyte/quasilyte/fix/pretty_printer_array_item_unpack

printer: handle array item unpack in pretty printer
This commit is contained in:
Vadym Slizov 2020-09-03 22:21:20 +03:00 committed by GitHub
commit 0cd26d7f4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) { func (p *PrettyPrinter) printExprArrayItem(n node.Node) {
nn := n.(*expr.ArrayItem) nn := n.(*expr.ArrayItem)
if nn.Unpack {
io.WriteString(p.w, "...")
}
if nn.Key != nil { if nn.Key != nil {
p.Print(nn.Key) p.Print(nn.Key)
io.WriteString(p.w, " => ") 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) { func TestPrintExprArray(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")