pretty printer: print ConstFetch and ClassMethod
This commit is contained in:
parent
c826d26edf
commit
7c5d20a1b9
@ -201,6 +201,8 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) {
|
||||
return printExprClosureUse
|
||||
case *expr.Closure:
|
||||
return printExprClosure
|
||||
case *expr.ConstFetch:
|
||||
return printExprConstFetch
|
||||
case *expr.Die:
|
||||
return printExprDie
|
||||
case *expr.Empty:
|
||||
@ -290,6 +292,9 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) {
|
||||
return printStmtCatch
|
||||
case *stmt.ClassConstList:
|
||||
return printStmtClassConstList
|
||||
case *stmt.ClassMethod:
|
||||
return printStmtClassMethod
|
||||
|
||||
case *stmt.Constant:
|
||||
return printStmtConstant
|
||||
case *stmt.StmtList:
|
||||
@ -905,6 +910,12 @@ func printExprClosure(o io.Writer, n node.Node) {
|
||||
io.WriteString(o, "}")
|
||||
}
|
||||
|
||||
func printExprConstFetch(o io.Writer, n node.Node) {
|
||||
nn := n.(*expr.ConstFetch)
|
||||
|
||||
Print(o, nn.Constant)
|
||||
}
|
||||
|
||||
func printExprDie(o io.Writer, n node.Node) {
|
||||
nn := n.(*expr.Die)
|
||||
|
||||
@ -1327,6 +1338,34 @@ func printStmtClassConstList(o io.Writer, n node.Node) {
|
||||
io.WriteString(o, ";\n")
|
||||
}
|
||||
|
||||
func printStmtClassMethod(o io.Writer, n node.Node) {
|
||||
nn := n.(*stmt.ClassMethod)
|
||||
|
||||
if nn.Modifiers != nil {
|
||||
joinPrint(" ", o, nn.Modifiers)
|
||||
io.WriteString(o, " ")
|
||||
}
|
||||
io.WriteString(o, "function ")
|
||||
|
||||
if nn.ReturnsRef {
|
||||
io.WriteString(o, "&")
|
||||
}
|
||||
|
||||
Print(o, nn.MethodName)
|
||||
io.WriteString(o, "(")
|
||||
joinPrint(", ", o, nn.Params)
|
||||
io.WriteString(o, ")")
|
||||
|
||||
if nn.ReturnType != nil {
|
||||
io.WriteString(o, ": ")
|
||||
Print(o, nn.ReturnType)
|
||||
}
|
||||
|
||||
io.WriteString(o, "\n{\n") // TODO: handle indentation
|
||||
printNodes(o, nn.Stmts)
|
||||
io.WriteString(o, "}\n") // TODO: handle indentation
|
||||
}
|
||||
|
||||
func printStmtConstant(o io.Writer, n node.Node) {
|
||||
nn := n.(*stmt.Constant)
|
||||
|
||||
|
@ -1232,6 +1232,21 @@ func TestPrintExprClosure(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintExprConstFetch(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &expr.ConstFetch{
|
||||
Constant: &name.Name{Parts: []node.Node{&name.NamePart{Value: "null"}}},
|
||||
})
|
||||
|
||||
expected := "null"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintDie(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
@ -2097,6 +2112,43 @@ func TestPrintStmtClassConstList(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtClassMethod(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.ClassMethod{
|
||||
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
||||
ReturnsRef: true,
|
||||
MethodName: &node.Identifier{Value: "foo"},
|
||||
Params: []node.Node{
|
||||
&node.Parameter{
|
||||
ByRef: true,
|
||||
VariableType: &node.Nullable{Expr: &name.Name{Parts: []node.Node{&name.NamePart{Value: "int"}}}},
|
||||
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
||||
DefaultValue: &expr.ConstFetch{Constant: &name.Name{Parts: []node.Node{&name.NamePart{Value: "null"}}}},
|
||||
},
|
||||
&node.Parameter{
|
||||
Variadic: true,
|
||||
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
||||
},
|
||||
},
|
||||
ReturnType: &name.Name{Parts: []node.Node{&name.NamePart{Value: "void"}}},
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
||||
},
|
||||
})
|
||||
|
||||
expected := `public function &foo(?int &$a = null, ...$b): void
|
||||
{
|
||||
$a;
|
||||
}
|
||||
`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtConstant(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user