refactoring: update ast structure of "ArrayItem" node

This commit is contained in:
Vadym Slizov
2020-12-26 20:13:50 +02:00
parent 8bf1fa822d
commit 0f5f5e7dc7
10 changed files with 228 additions and 271 deletions

View File

@@ -1177,6 +1177,7 @@ type ExprArrayItem struct {
EllipsisTkn *token.Token
Key Vertex
DoubleArrowTkn *token.Token
AmpersandTkn *token.Token
Val Vertex
}

View File

@@ -1056,6 +1056,7 @@ func (v *Dumper) ExprArrayItem(n *ast.ExprArrayItem) {
v.dumpToken("EllipsisTkn", n.EllipsisTkn)
v.dumpVertex("Key", n.Key)
v.dumpToken("DoubleArrowTkn", n.DoubleArrowTkn)
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
v.dumpVertex("Val", n.Val)
v.indent--

View File

@@ -641,6 +641,7 @@ func (p *printer) ExprArrayItem(n *ast.ExprArrayItem) {
p.printToken(n.EllipsisTkn, nil)
p.printNode(n.Key)
p.printToken(n.DoubleArrowTkn, p.ifNode(n.Key, []byte("=>")))
p.printToken(n.AmpersandTkn, nil)
p.printNode(n.Val)
}

View File

@@ -1559,9 +1559,31 @@ func TestPrinterPrintExprArrayItem(t *testing.T) {
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.ExprArrayItem{
Val: &ast.ExprReference{Var: &ast.ExprVariable{
Val: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$world")},
}},
},
}
n.Accept(p)
expected := `$world`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprArrayItem_Reference(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.ExprArrayItem{
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Val: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$world")},
},
}
n.Accept(p)