[#82] array item unpack

This commit is contained in:
z7zmey
2019-12-29 22:42:52 +02:00
parent dc7aa7302d
commit d552681a7d
10 changed files with 982 additions and 863 deletions

View File

@@ -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

View File

@@ -218,3 +218,67 @@ func TestArrayItems(t *testing.T) {
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}
func TestArrayItemUnpack(t *testing.T) {
src := `<? array(...$b);`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 16,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 16,
},
Expr: &expr.Array{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 15,
},
Items: []node.Node{
&expr.ArrayItem{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 14,
},
Unpack: true,
Val: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
Value: "b",
},
},
},
},
},
},
},
}
php7parser := php7.NewParser([]byte(src), "7.4")
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}

View File

@@ -31,11 +31,12 @@ var nodesToTest = []struct {
},
{
&expr.ArrayItem{
Key: &scalar.String{Value: "key"},
Val: &scalar.Lnumber{Value: "1"},
Key: &scalar.String{Value: "key"},
Val: &scalar.Lnumber{Value: "1"},
Unpack: true,
},
[]string{"Key", "Val"},
nil,
map[string]interface{}{"Unpack": true},
},
{
&expr.Array{