From d552681a7d2520873427a6899f221eb2efe160bc Mon Sep 17 00:00:00 2001 From: z7zmey Date: Sun, 29 Dec 2019 22:42:52 +0200 Subject: [PATCH] [#82] array item unpack --- node/expr/n_array_item.go | 8 +- node/expr/t_array_test.go | 64 ++ node/expr/t_visitor_test.go | 7 +- php5/php5.go | 34 +- php5/php5.y | 34 +- php7/php7.go | 1646 ++++++++++++++------------- php7/php7.y | 28 +- printer/printer.go | 4 + printer/printer_parsed_php7_test.go | 1 + printer/printer_test.go | 19 + 10 files changed, 982 insertions(+), 863 deletions(-) diff --git a/node/expr/n_array_item.go b/node/expr/n_array_item.go index 1ac202f..5f5a07a 100644 --- a/node/expr/n_array_item.go +++ b/node/expr/n_array_item.go @@ -13,14 +13,16 @@ type ArrayItem struct { Position *position.Position Key node.Node Val node.Node + Unpack bool } // NewArrayItem node constructor -func NewArrayItem(Key node.Node, Val node.Node) *ArrayItem { +func NewArrayItem(Key node.Node, Val node.Node, Unpack bool) *ArrayItem { return &ArrayItem{ FreeFloating: nil, Key: Key, Val: Val, + Unpack: Unpack, } } @@ -40,7 +42,9 @@ func (n *ArrayItem) GetFreeFloating() *freefloating.Collection { // Attributes returns node attributes as map func (n *ArrayItem) Attributes() map[string]interface{} { - return nil + return map[string]interface{}{ + "Unpack": n.Unpack, + } } // Walk traverses nodes diff --git a/node/expr/t_array_test.go b/node/expr/t_array_test.go index 0776e84..559fca8 100644 --- a/node/expr/t_array_test.go +++ b/node/expr/t_array_test.go @@ -218,3 +218,67 @@ func TestArrayItems(t *testing.T) { actual = php5parser.GetRootNode() assert.DeepEqual(t, expected, actual) } + +func TestArrayItemUnpack(t *testing.T) { + src := ` $world , + ... $unpack ] ; ` diff --git a/printer/printer_test.go b/printer/printer_test.go index 8bf928f..331de66 100644 --- a/printer/printer_test.go +++ b/printer/printer_test.go @@ -1551,6 +1551,25 @@ func TestPrinterPrintExprArrayItem(t *testing.T) { } } +func TestPrinterPrintExprArrayItemUnpack(t *testing.T) { + o := bytes.NewBufferString("") + + p := printer.NewPrinter(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 TestPrinterPrintExprArray(t *testing.T) { o := bytes.NewBufferString("")