Merge pull request #109 from azhai/hotfix/override-root-stmts

Do not override Stmts in printNodeRoot()
This commit is contained in:
Vadym Slizov 2020-07-26 14:57:08 +02:00 committed by GitHub
commit db09dd443f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 22 deletions

View File

@ -4,8 +4,6 @@ import (
"io" "io"
"strings" "strings"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/expr" "github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/expr/assign" "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/expr/cast"
"github.com/z7zmey/php-parser/node/name" "github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node/scalar" "github.com/z7zmey/php-parser/node/scalar"
"github.com/z7zmey/php-parser/node/stmt"
) )
type PrettyPrinter struct { type PrettyPrinter struct {
@ -415,11 +414,12 @@ func (p *PrettyPrinter) printNode(n node.Node) {
// node // node
func (p *PrettyPrinter) printNodeRoot(n node.Node) { func (p *PrettyPrinter) printNodeRoot(n node.Node) {
var stmts []node.Node
v := n.(*node.Root) v := n.(*node.Root)
if len(v.Stmts) > 0 { if len(v.Stmts) > 0 {
firstStmt := v.Stmts[0] firstStmt := v.Stmts[0]
v.Stmts = v.Stmts[1:] stmts = v.Stmts[1:]
switch fs := firstStmt.(type) { switch fs := firstStmt.(type) {
case *stmt.InlineHtml: case *stmt.InlineHtml:
@ -433,7 +433,7 @@ func (p *PrettyPrinter) printNodeRoot(n node.Node) {
} }
} }
p.indentDepth-- p.indentDepth--
p.printNodes(v.Stmts) p.printNodes(stmts)
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
} }

View File

@ -16,10 +16,17 @@ import (
) )
func TestPrintFile(t *testing.T) { func TestPrintFile(t *testing.T) {
o := bytes.NewBufferString("") expected := `<?php
namespace Foo;
p := printer.NewPrettyPrinter(o, "\t") abstract class Bar extends Baz
p.Print(&node.Root{ {
public function greet()
{
echo 'Hello world';
}
}
`
rootNode := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Namespace{ &stmt.Namespace{
NamespaceName: &name.Name{ NamespaceName: &name.Name{
@ -59,22 +66,20 @@ func TestPrintFile(t *testing.T) {
}, },
}, },
}, },
})
expected := `<?php
namespace Foo;
abstract class Bar extends Baz
{
public function greet()
{
echo 'Hello world';
} }
}
`
actual := o.String()
if expected != actual { o1 := bytes.NewBufferString("")
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) p1 := printer.NewPrettyPrinter(o1, "\t")
p1.Print(rootNode)
if actual := o1.String(); expected != actual {
t.Errorf("\nPrint the 1st time\nexpected: %s\ngot: %s\n", expected, actual)
}
o2 := bytes.NewBufferString("")
p2 := printer.NewPrettyPrinter(o2, "\t")
p2.Print(rootNode)
if actual := o2.String(); expected != actual {
t.Errorf("\nPrint the 2nd time\nexpected: %s\ngot: %s\n", expected, actual)
} }
} }