From b1266584acf9dee57494afbc734bfcdee574c0bb Mon Sep 17 00:00:00 2001 From: Ryan Liu Date: Fri, 10 Jul 2020 15:50:09 +0800 Subject: [PATCH] Do not override Stmts in printNodeRoot() --- printer/pretty_printer.go | 8 +++---- printer/pretty_printer_test.go | 41 +++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/printer/pretty_printer.go b/printer/pretty_printer.go index 95de96f..bfe6660 100644 --- a/printer/pretty_printer.go +++ b/printer/pretty_printer.go @@ -4,8 +4,6 @@ import ( "io" "strings" - "github.com/z7zmey/php-parser/node/stmt" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node/expr" "github.com/z7zmey/php-parser/node/expr/assign" @@ -13,6 +11,7 @@ import ( "github.com/z7zmey/php-parser/node/expr/cast" "github.com/z7zmey/php-parser/node/name" "github.com/z7zmey/php-parser/node/scalar" + "github.com/z7zmey/php-parser/node/stmt" ) type PrettyPrinter struct { @@ -415,11 +414,12 @@ func (p *PrettyPrinter) printNode(n node.Node) { // node func (p *PrettyPrinter) printNodeRoot(n node.Node) { + var stmts []node.Node v := n.(*node.Root) if len(v.Stmts) > 0 { firstStmt := v.Stmts[0] - v.Stmts = v.Stmts[1:] + stmts = v.Stmts[1:] switch fs := firstStmt.(type) { case *stmt.InlineHtml: @@ -433,7 +433,7 @@ func (p *PrettyPrinter) printNodeRoot(n node.Node) { } } p.indentDepth-- - p.printNodes(v.Stmts) + p.printNodes(stmts) io.WriteString(p.w, "\n") } diff --git a/printer/pretty_printer_test.go b/printer/pretty_printer_test.go index 7eca7f5..e45f5e6 100644 --- a/printer/pretty_printer_test.go +++ b/printer/pretty_printer_test.go @@ -16,10 +16,17 @@ import ( ) func TestPrintFile(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, "\t") - p.Print(&node.Root{ + expected := `