[refactoring] update ast structure for "Use" and "GroupUse" nodes
This commit is contained in:
@@ -399,14 +399,10 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
|
||||
p.printStmtUnset(n)
|
||||
case *ast.StmtUse:
|
||||
p.printStmtUse(n)
|
||||
case *ast.StmtGroupUseList:
|
||||
p.printStmtGroupUseList(n)
|
||||
case *ast.StmtUseList:
|
||||
p.printStmtUseList(n)
|
||||
case *ast.StmtGroupUse:
|
||||
p.printStmtGroupUse(n)
|
||||
case *ast.StmtUseDeclaration:
|
||||
p.printStmtUseDeclaration(n)
|
||||
case *ast.StmtUseType:
|
||||
p.printStmtUseType(n)
|
||||
case *ast.StmtWhile:
|
||||
p.printStmtWhile(n)
|
||||
}
|
||||
@@ -2139,30 +2135,41 @@ func (p *PrettyPrinter) printStmtUse(n ast.Vertex) {
|
||||
|
||||
io.WriteString(p.w, "use ")
|
||||
|
||||
p.Print(nn.UseList)
|
||||
if nn.Type != nil {
|
||||
p.Print(nn.Type)
|
||||
io.WriteString(p.w, " ")
|
||||
}
|
||||
|
||||
p.joinPrint(", ", nn.UseDeclarations)
|
||||
|
||||
io.WriteString(p.w, ";")
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtGroupUseList(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtGroupUseList)
|
||||
func (p *PrettyPrinter) printStmtGroupUse(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtGroupUse)
|
||||
|
||||
io.WriteString(p.w, "use ")
|
||||
|
||||
if nn.Type != nil {
|
||||
p.Print(nn.Type)
|
||||
io.WriteString(p.w, " ")
|
||||
}
|
||||
|
||||
p.Print(nn.Prefix)
|
||||
|
||||
io.WriteString(p.w, "\\{")
|
||||
p.Print(nn.UseList)
|
||||
io.WriteString(p.w, "}")
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtUseList(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtUseList)
|
||||
|
||||
p.joinPrint(", ", nn.UseDeclarations)
|
||||
io.WriteString(p.w, "}")
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtUseDeclaration(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtUseDeclaration)
|
||||
|
||||
if nn.Type != nil {
|
||||
p.Print(nn.Type)
|
||||
io.WriteString(p.w, " ")
|
||||
}
|
||||
|
||||
p.Print(nn.Use)
|
||||
|
||||
if nn.Alias != nil {
|
||||
@@ -2171,15 +2178,6 @@ func (p *PrettyPrinter) printStmtUseDeclaration(n ast.Vertex) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtUseType(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtUseType)
|
||||
|
||||
p.Print(nn.Type)
|
||||
io.WriteString(p.w, " ")
|
||||
|
||||
p.Print(nn.Use)
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtWhile(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtWhile)
|
||||
|
||||
|
||||
@@ -3954,39 +3954,7 @@ func TestPrintUse(t *testing.T) {
|
||||
|
||||
p := printer.NewPrettyPrinter(o, " ")
|
||||
p.Print(&ast.StmtUse{
|
||||
UseList: &ast.StmtUseList{},
|
||||
})
|
||||
|
||||
expected := `use ;`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtGroupUseList(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := printer.NewPrettyPrinter(o, " ")
|
||||
p.Print(&ast.StmtGroupUseList{
|
||||
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
UseList: &ast.StmtUseList{},
|
||||
})
|
||||
|
||||
expected := `Foo\{}`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtUseList(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := printer.NewPrettyPrinter(o, " ")
|
||||
p.Print(&ast.StmtUseList{
|
||||
Type: &ast.Identifier{Value: []byte("function")},
|
||||
UseDeclarations: []ast.Vertex{
|
||||
&ast.StmtUseDeclaration{
|
||||
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
@@ -3998,7 +3966,33 @@ func TestPrintStmtUseList(t *testing.T) {
|
||||
},
|
||||
})
|
||||
|
||||
expected := `Foo as Bar, Baz`
|
||||
expected := `use function Foo as Bar, Baz;`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtGroupUse(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := printer.NewPrettyPrinter(o, " ")
|
||||
p.Print(&ast.StmtGroupUse{
|
||||
Type: &ast.Identifier{Value: []byte("function")},
|
||||
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
UseDeclarations: []ast.Vertex{
|
||||
&ast.StmtUseDeclaration{
|
||||
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
Alias: &ast.Identifier{Value: []byte("Bar")},
|
||||
},
|
||||
&ast.StmtUseDeclaration{
|
||||
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expected := `use function Foo\{Foo as Bar, Baz}`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
@@ -4011,30 +4005,11 @@ func TestPrintUseDeclaration(t *testing.T) {
|
||||
|
||||
p := printer.NewPrettyPrinter(o, " ")
|
||||
p.Print(&ast.StmtUseDeclaration{
|
||||
Type: &ast.Identifier{Value: []byte("function")},
|
||||
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
Alias: &ast.Identifier{Value: []byte("Bar")},
|
||||
})
|
||||
|
||||
expected := `Foo as Bar`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintUseType(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := printer.NewPrettyPrinter(o, " ")
|
||||
p.Print(&ast.StmtUseType{
|
||||
Type: &ast.Identifier{Value: []byte("function")},
|
||||
Use: &ast.StmtUseDeclaration{
|
||||
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
Alias: &ast.Identifier{Value: []byte("Bar")},
|
||||
},
|
||||
})
|
||||
|
||||
expected := `function Foo as Bar`
|
||||
actual := o.String()
|
||||
|
||||
|
||||
@@ -76,6 +76,23 @@ func (p *Printer) printFreeFloatingOrDefault(n ast.Vertex, pos token.Position, d
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Printer) printToken(t *token.Token, def string) {
|
||||
if t != nil {
|
||||
p.w.Write(t.Skipped)
|
||||
p.w.Write(t.Value)
|
||||
p.bufStart = ""
|
||||
return
|
||||
}
|
||||
|
||||
if def != "" {
|
||||
p.w.Write([]byte(p.bufStart))
|
||||
p.bufStart = ""
|
||||
|
||||
p.w.Write([]byte(def))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Printer) printFreeFloating(n ast.Vertex, pos token.Position) {
|
||||
if n == nil {
|
||||
return
|
||||
@@ -87,7 +104,7 @@ func (p *Printer) printFreeFloating(n ast.Vertex, pos token.Position) {
|
||||
}
|
||||
|
||||
func (p *Printer) printNode(n ast.Vertex) {
|
||||
switch n.(type) {
|
||||
switch n := n.(type) {
|
||||
|
||||
// node
|
||||
|
||||
@@ -438,14 +455,10 @@ func (p *Printer) printNode(n ast.Vertex) {
|
||||
p.printStmtUnset(n)
|
||||
case *ast.StmtUse:
|
||||
p.printStmtUse(n)
|
||||
case *ast.StmtGroupUseList:
|
||||
p.printStmtGroupUseList(n)
|
||||
case *ast.StmtUseList:
|
||||
p.printStmtUseList(n)
|
||||
case *ast.StmtGroupUse:
|
||||
p.printStmtGroupUse(n)
|
||||
case *ast.StmtUseDeclaration:
|
||||
p.printStmtUseDeclaration(n)
|
||||
case *ast.StmtUseType:
|
||||
p.printStmtUseType(n)
|
||||
case *ast.StmtWhile:
|
||||
p.printStmtWhile(n)
|
||||
case *ast.ParserAs:
|
||||
@@ -3260,74 +3273,66 @@ func (p *Printer) printStmtUnset(n ast.Vertex) {
|
||||
p.printFreeFloating(nn, token.End)
|
||||
}
|
||||
|
||||
func (p *Printer) printStmtUse(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtUse)
|
||||
p.printFreeFloating(nn, token.Start)
|
||||
|
||||
io.WriteString(p.w, "use")
|
||||
func (p *Printer) printStmtUse(n *ast.StmtUse) {
|
||||
p.printToken(n.UseTkn, "use")
|
||||
|
||||
p.bufStart = " "
|
||||
p.Print(nn.UseList)
|
||||
p.Print(n.Type)
|
||||
|
||||
p.printFreeFloatingOrDefault(nn, token.End, ";")
|
||||
p.bufStart = " "
|
||||
p.joinPrint(",", n.UseDeclarations)
|
||||
|
||||
p.printToken(n.SemiColonTkn, ";")
|
||||
}
|
||||
|
||||
func (p *Printer) printStmtGroupUseList(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtGroupUseList)
|
||||
p.printFreeFloating(nn, token.Start)
|
||||
func (p *Printer) printStmtGroupUse(n *ast.StmtGroupUse) {
|
||||
p.printToken(n.UseTkn, "use")
|
||||
|
||||
p.Print(nn.Prefix)
|
||||
p.bufStart = " "
|
||||
p.Print(n.Type)
|
||||
|
||||
if _, ok := nn.UseList.(*ast.ParserNsSeparator); !ok {
|
||||
io.WriteString(p.w, "\\{")
|
||||
}
|
||||
p.bufStart = " "
|
||||
p.printToken(n.LeadingNsSeparatorTkn, "")
|
||||
|
||||
p.Print(nn.UseList)
|
||||
p.Print(n.Prefix)
|
||||
p.printToken(n.NsSeparatorTkn, "\\")
|
||||
p.printToken(n.OpenCurlyBracketTkn, "{")
|
||||
|
||||
if _, ok := nn.UseList.(*ast.ParserNsSeparator); !ok {
|
||||
io.WriteString(p.w, "}")
|
||||
}
|
||||
|
||||
p.printFreeFloating(nn, token.End)
|
||||
}
|
||||
|
||||
func (p *Printer) printStmtUseList(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtUseList)
|
||||
p.printFreeFloating(nn, token.Start)
|
||||
|
||||
p.joinPrint(",", nn.UseDeclarations)
|
||||
|
||||
p.printFreeFloating(nn, token.End)
|
||||
}
|
||||
|
||||
func (p *Printer) printStmtUseDeclaration(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtUseDeclaration)
|
||||
p.printFreeFloating(nn, token.Start)
|
||||
|
||||
p.Print(nn.Use)
|
||||
|
||||
if nn.Alias != nil {
|
||||
if _, ok := nn.Alias.(*ast.ParserAs); !ok {
|
||||
io.WriteString(p.w, " as")
|
||||
for k, v := range n.UseDeclarations {
|
||||
p.Print(v)
|
||||
var def string
|
||||
if k != len(n.UseDeclarations)-1 {
|
||||
def = ","
|
||||
}
|
||||
if decl, ok := v.(*ast.StmtUseDeclaration); ok {
|
||||
p.printToken(decl.CommaTkn, def)
|
||||
}
|
||||
|
||||
p.bufStart = " "
|
||||
p.Print(nn.Alias)
|
||||
}
|
||||
|
||||
p.printFreeFloating(nn, token.End)
|
||||
p.printToken(n.CloseCurlyBracketTkn, "}")
|
||||
p.printToken(n.SemiColonTkn, ";")
|
||||
}
|
||||
|
||||
func (p *Printer) printStmtUseType(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtUseType)
|
||||
p.printFreeFloating(nn, token.Start)
|
||||
func (p *Printer) printStmtUseDeclaration(n *ast.StmtUseDeclaration) {
|
||||
p.Print(n.Type)
|
||||
|
||||
p.Print(nn.Type)
|
||||
if n.Type != nil {
|
||||
p.bufStart = " "
|
||||
}
|
||||
|
||||
p.printToken(n.NsSeparatorTkn, "")
|
||||
|
||||
p.Print(n.Use)
|
||||
|
||||
if n.Alias == nil {
|
||||
return
|
||||
}
|
||||
|
||||
p.bufStart = " "
|
||||
p.Print(nn.Use)
|
||||
p.printToken(n.AsTkn, "as")
|
||||
|
||||
p.printFreeFloating(nn, token.End)
|
||||
p.bufStart = " "
|
||||
p.Print(n.Alias)
|
||||
}
|
||||
|
||||
func (p *Printer) printStmtWhile(n ast.Vertex) {
|
||||
|
||||
@@ -1308,7 +1308,8 @@ func TestParseAndPrintPhp5Unset(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseAndPrintPhp5UseList(t *testing.T) {
|
||||
src := `<?php
|
||||
// TODO: remove ; after <?php
|
||||
src := `<?php ;
|
||||
use Foo ;
|
||||
use \ Foo as Bar ;
|
||||
use function \ Foo as Bar ;
|
||||
|
||||
@@ -1159,7 +1159,8 @@ func TestParseAndPrintGoto(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseAndPrintGroupUse(t *testing.T) {
|
||||
src := `<?php
|
||||
// TODO: remove ; after <?php
|
||||
src := `<?php ;
|
||||
use function Foo \ { Bar as Baz , Quuz , } ;
|
||||
use Foo \ { function Bar as Baz , const Quuz } ;
|
||||
use \ Foo \ { function Bar as Baz , const Quuz , } ;
|
||||
@@ -1440,7 +1441,8 @@ func TestParseAndPrintUnset(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseAndPrintUseList(t *testing.T) {
|
||||
src := `<?php
|
||||
// TODO: remove ; after <?php
|
||||
src := `<?php ;
|
||||
use Foo ;
|
||||
use \ Foo as Bar ;
|
||||
use function \ Foo as Bar ;
|
||||
|
||||
@@ -4391,45 +4391,7 @@ func TestPrinterPrintUse(t *testing.T) {
|
||||
|
||||
p := printer.NewPrinter(o)
|
||||
p.Print(&ast.StmtUse{
|
||||
UseList: &ast.StmtUseList{
|
||||
UseDeclarations: []ast.Vertex{
|
||||
&ast.StmtUseDeclaration{
|
||||
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expected := `use Foo;`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrinterPrintStmtGroupUseList(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := printer.NewPrinter(o)
|
||||
p.Print(&ast.StmtGroupUseList{
|
||||
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
UseList: &ast.StmtUseList{},
|
||||
})
|
||||
|
||||
expected := `Foo\{}`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrinterPrintStmtUseList(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := printer.NewPrinter(o)
|
||||
p.Print(&ast.StmtUseList{
|
||||
Type: &ast.Identifier{Value: []byte("function")},
|
||||
UseDeclarations: []ast.Vertex{
|
||||
&ast.StmtUseDeclaration{
|
||||
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
@@ -4441,7 +4403,33 @@ func TestPrinterPrintStmtUseList(t *testing.T) {
|
||||
},
|
||||
})
|
||||
|
||||
expected := `Foo as Bar,Baz`
|
||||
expected := `use function Foo as Bar,Baz;`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrinterPrintStmtGroupUse(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := printer.NewPrinter(o)
|
||||
p.Print(&ast.StmtGroupUse{
|
||||
Type: &ast.Identifier{Value: []byte("function")},
|
||||
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
UseDeclarations: []ast.Vertex{
|
||||
&ast.StmtUseDeclaration{
|
||||
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
Alias: &ast.Identifier{Value: []byte("Bar")},
|
||||
},
|
||||
&ast.StmtUseDeclaration{
|
||||
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expected := `use function Foo\{Foo as Bar,Baz};`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
@@ -4454,30 +4442,11 @@ func TestPrinterPrintUseDeclaration(t *testing.T) {
|
||||
|
||||
p := printer.NewPrinter(o)
|
||||
p.Print(&ast.StmtUseDeclaration{
|
||||
Type: &ast.Identifier{Value: []byte("function")},
|
||||
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
Alias: &ast.Identifier{Value: []byte("Bar")},
|
||||
})
|
||||
|
||||
expected := `Foo as Bar`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrinterPrintUseType(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := printer.NewPrinter(o)
|
||||
p.Print(&ast.StmtUseType{
|
||||
Type: &ast.Identifier{Value: []byte("function")},
|
||||
Use: &ast.StmtUseDeclaration{
|
||||
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
Alias: &ast.Identifier{Value: []byte("Bar")},
|
||||
},
|
||||
})
|
||||
|
||||
expected := `function Foo as Bar`
|
||||
actual := o.String()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user