pretty printer: print Throw, TraitMethodRef, TraitUseAlias, TraitUsePrecedence, TraitUse

This commit is contained in:
z7zmey 2018-04-02 01:29:24 +03:00
parent a9b6e93480
commit bd8b8f5b1a
2 changed files with 191 additions and 0 deletions

View File

@ -356,6 +356,16 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) {
return printStmtStmtList return printStmtStmtList
case *stmt.Switch: case *stmt.Switch:
return printStmtSwitch return printStmtSwitch
case *stmt.Throw:
return printStmtThrow
case *stmt.TraitMethodRef:
return printStmtTraitMethodRef
case *stmt.TraitUseAlias:
return printStmtTraitUseAlias
case *stmt.TraitUsePrecedence:
return printStmtTraitUsePrecedence
case *stmt.TraitUse:
return printStmtTraitUse
case *stmt.Use: case *stmt.Use:
return printStmtUse return printStmtUse
@ -1858,6 +1868,66 @@ func printStmtSwitch(o io.Writer, n node.Node) {
io.WriteString(o, "}\n") io.WriteString(o, "}\n")
} }
func printStmtThrow(o io.Writer, n node.Node) {
nn := n.(*stmt.Throw)
io.WriteString(o, "throw ")
Print(o, nn.Expr)
io.WriteString(o, ";\n")
}
func printStmtTraitMethodRef(o io.Writer, n node.Node) {
nn := n.(*stmt.TraitMethodRef)
Print(o, nn.Trait)
io.WriteString(o, "::")
Print(o, nn.Method)
}
func printStmtTraitUseAlias(o io.Writer, n node.Node) {
nn := n.(*stmt.TraitUseAlias)
Print(o, nn.Ref)
io.WriteString(o, " as")
if nn.Modifier != nil {
io.WriteString(o, " ")
Print(o, nn.Modifier)
}
if nn.Alias != nil {
io.WriteString(o, " ")
Print(o, nn.Alias)
}
io.WriteString(o, ";\n")
}
func printStmtTraitUsePrecedence(o io.Writer, n node.Node) {
nn := n.(*stmt.TraitUsePrecedence)
Print(o, nn.Ref)
io.WriteString(o, " insteadof ")
joinPrint(", ", o, nn.Insteadof)
io.WriteString(o, ";\n")
}
func printStmtTraitUse(o io.Writer, n node.Node) {
nn := n.(*stmt.TraitUse)
io.WriteString(o, "use ")
joinPrint(", ", o, nn.Traits)
if nn.Adaptations != nil {
io.WriteString(o, " {\n")
printNodes(o, nn.Adaptations)
io.WriteString(o, "}\n")
} else {
io.WriteString(o, ";\n")
}
}
func printStmtUse(o io.Writer, n node.Node) { func printStmtUse(o io.Writer, n node.Node) {
nn := n.(*stmt.Use) nn := n.(*stmt.Use)

View File

@ -3120,6 +3120,127 @@ $b;
} }
} }
func TestPrintStmtThrow(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Throw{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
})
expected := "throw $var;\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtTraitMethodRef(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.TraitMethodRef{
Trait: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
Method: &node.Identifier{Value: "a"},
})
expected := "Foo::a"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtTraitUseAlias(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{
Trait: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
Method: &node.Identifier{Value: "a"},
},
Modifier: &node.Identifier{Value: "public"},
Alias: &node.Identifier{Value: "b"},
})
expected := "Foo::a as public b;\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtTraitUsePrecedence(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.TraitUsePrecedence{
Ref: &stmt.TraitMethodRef{
Trait: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
Method: &node.Identifier{Value: "a"},
},
Insteadof: []node.Node{
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}},
},
})
expected := "Foo::a insteadof Bar, Baz;\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtTraitUse(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.TraitUse{
Traits: []node.Node{
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
},
})
expected := "use Foo, Bar;\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtTraitAdaptations(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.TraitUse{
Traits: []node.Node{
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
},
Adaptations: []node.Node{
&stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{
Trait: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
Method: &node.Identifier{Value: "a"},
},
Alias: &node.Identifier{Value: "b"},
},
},
})
expected := `use Foo, Bar {
Foo::a as b;
}
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintUse(t *testing.T) { func TestPrintUse(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")