refactoring: update ast structure of "Class", "Interface" and "Trait" nodes
This commit is contained in:
@@ -31,8 +31,6 @@ type NodeVisitor interface {
|
||||
StmtCatch(n *StmtCatch)
|
||||
StmtClass(n *StmtClass)
|
||||
StmtClassConstList(n *StmtClassConstList)
|
||||
StmtClassExtends(n *StmtClassExtends)
|
||||
StmtClassImplements(n *StmtClassImplements)
|
||||
StmtClassMethod(n *StmtClassMethod)
|
||||
StmtConstList(n *StmtConstList)
|
||||
StmtConstant(n *StmtConstant)
|
||||
@@ -54,7 +52,6 @@ type NodeVisitor interface {
|
||||
StmtIf(n *StmtIf)
|
||||
StmtInlineHtml(n *StmtInlineHtml)
|
||||
StmtInterface(n *StmtInterface)
|
||||
StmtInterfaceExtends(n *StmtInterfaceExtends)
|
||||
StmtLabel(n *StmtLabel)
|
||||
StmtNamespace(n *StmtNamespace)
|
||||
StmtNop(n *StmtNop)
|
||||
|
||||
@@ -285,19 +285,22 @@ func (n *StmtCatch) GetPosition() *position.Position {
|
||||
|
||||
// StmtClass node
|
||||
type StmtClass struct {
|
||||
Position *position.Position
|
||||
Modifiers []Vertex
|
||||
ClassTkn *token.Token
|
||||
ClassName Vertex
|
||||
OpenParenthesisTkn *token.Token
|
||||
Arguments []Vertex
|
||||
SeparatorTkns []*token.Token
|
||||
CloseParenthesisTkn *token.Token
|
||||
Extends Vertex
|
||||
Implements Vertex
|
||||
OpenCurlyBracketTkn *token.Token
|
||||
Stmts []Vertex
|
||||
CloseCurlyBracketTkn *token.Token
|
||||
Position *position.Position
|
||||
Modifiers []Vertex
|
||||
ClassTkn *token.Token
|
||||
ClassName Vertex
|
||||
OpenParenthesisTkn *token.Token
|
||||
Arguments []Vertex
|
||||
SeparatorTkns []*token.Token
|
||||
CloseParenthesisTkn *token.Token
|
||||
ExtendsTkn *token.Token
|
||||
Extends Vertex
|
||||
ImplementsTkn *token.Token
|
||||
Implements []Vertex
|
||||
ImplementsSeparatorTkns []*token.Token
|
||||
OpenCurlyBracketTkn *token.Token
|
||||
Stmts []Vertex
|
||||
CloseCurlyBracketTkn *token.Token
|
||||
}
|
||||
|
||||
func (n *StmtClass) Accept(v NodeVisitor) {
|
||||
@@ -326,37 +329,6 @@ func (n *StmtClassConstList) GetPosition() *position.Position {
|
||||
return n.Position
|
||||
}
|
||||
|
||||
// StmtClassExtends node
|
||||
type StmtClassExtends struct {
|
||||
Position *position.Position
|
||||
ExtendTkn *token.Token
|
||||
ClassName Vertex
|
||||
}
|
||||
|
||||
func (n *StmtClassExtends) Accept(v NodeVisitor) {
|
||||
v.StmtClassExtends(n)
|
||||
}
|
||||
|
||||
func (n *StmtClassExtends) GetPosition() *position.Position {
|
||||
return n.Position
|
||||
}
|
||||
|
||||
// StmtClassImplements node
|
||||
type StmtClassImplements struct {
|
||||
Position *position.Position
|
||||
ImplementsTkn *token.Token
|
||||
InterfaceNames []Vertex
|
||||
SeparatorTkns []*token.Token
|
||||
}
|
||||
|
||||
func (n *StmtClassImplements) Accept(v NodeVisitor) {
|
||||
v.StmtClassImplements(n)
|
||||
}
|
||||
|
||||
func (n *StmtClassImplements) GetPosition() *position.Position {
|
||||
return n.Position
|
||||
}
|
||||
|
||||
// StmtClassMethod node
|
||||
type StmtClassMethod struct {
|
||||
Position *position.Position
|
||||
@@ -744,7 +716,9 @@ type StmtInterface struct {
|
||||
Position *position.Position
|
||||
InterfaceTkn *token.Token
|
||||
InterfaceName Vertex
|
||||
Extends Vertex
|
||||
ExtendsTkn *token.Token
|
||||
Extends []Vertex
|
||||
ExtendsSeparatorTkns []*token.Token
|
||||
OpenCurlyBracketTkn *token.Token
|
||||
Stmts []Vertex
|
||||
CloseCurlyBracketTkn *token.Token
|
||||
@@ -758,22 +732,6 @@ func (n *StmtInterface) GetPosition() *position.Position {
|
||||
return n.Position
|
||||
}
|
||||
|
||||
// StmtInterfaceExtends node
|
||||
type StmtInterfaceExtends struct {
|
||||
Position *position.Position
|
||||
ExtendsTkn *token.Token
|
||||
InterfaceNames []Vertex
|
||||
SeparatorTkns []*token.Token
|
||||
}
|
||||
|
||||
func (n *StmtInterfaceExtends) Accept(v NodeVisitor) {
|
||||
v.StmtInterfaceExtends(n)
|
||||
}
|
||||
|
||||
func (n *StmtInterfaceExtends) GetPosition() *position.Position {
|
||||
return n.Position
|
||||
}
|
||||
|
||||
// StmtLabel node
|
||||
type StmtLabel struct {
|
||||
Position *position.Position
|
||||
@@ -966,8 +924,6 @@ type StmtTrait struct {
|
||||
Position *position.Position
|
||||
TraitTkn *token.Token
|
||||
TraitName Vertex
|
||||
Extends Vertex
|
||||
Implements Vertex
|
||||
OpenCurlyBracketTkn *token.Token
|
||||
Stmts []Vertex
|
||||
CloseCurlyBracketTkn *token.Token
|
||||
|
||||
@@ -170,9 +170,11 @@ func (t *DFS) Traverse(n ast.Vertex) {
|
||||
t.visitor.Leave("Extends", true)
|
||||
}
|
||||
if nn.Implements != nil {
|
||||
t.visitor.Enter("Implements", true)
|
||||
t.Traverse(nn.Implements)
|
||||
t.visitor.Leave("Implements", true)
|
||||
t.visitor.Enter("Implements", false)
|
||||
for _, c := range nn.Implements {
|
||||
t.Traverse(c)
|
||||
}
|
||||
t.visitor.Leave("Implements", false)
|
||||
}
|
||||
if nn.Stmts != nil {
|
||||
t.visitor.Enter("Stmts", false)
|
||||
@@ -202,32 +204,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
|
||||
}
|
||||
t.visitor.Leave("Consts", false)
|
||||
}
|
||||
case *ast.StmtClassExtends:
|
||||
if nn == nil {
|
||||
return
|
||||
}
|
||||
if !t.visitor.EnterNode(nn) {
|
||||
return
|
||||
}
|
||||
if nn.ClassName != nil {
|
||||
t.visitor.Enter("ClassName", true)
|
||||
t.Traverse(nn.ClassName)
|
||||
t.visitor.Leave("ClassName", true)
|
||||
}
|
||||
case *ast.StmtClassImplements:
|
||||
if nn == nil {
|
||||
return
|
||||
}
|
||||
if !t.visitor.EnterNode(nn) {
|
||||
return
|
||||
}
|
||||
if nn.InterfaceNames != nil {
|
||||
t.visitor.Enter("InterfaceNames", false)
|
||||
for _, c := range nn.InterfaceNames {
|
||||
t.Traverse(c)
|
||||
}
|
||||
t.visitor.Leave("InterfaceNames", false)
|
||||
}
|
||||
case *ast.StmtClassMethod:
|
||||
if nn == nil {
|
||||
return
|
||||
@@ -599,9 +575,11 @@ func (t *DFS) Traverse(n ast.Vertex) {
|
||||
t.visitor.Leave("InterfaceName", true)
|
||||
}
|
||||
if nn.Extends != nil {
|
||||
t.visitor.Enter("Extends", true)
|
||||
t.Traverse(nn.Extends)
|
||||
t.visitor.Leave("Extends", true)
|
||||
t.visitor.Enter("Extends", false)
|
||||
for _, c := range nn.Extends {
|
||||
t.Traverse(c)
|
||||
}
|
||||
t.visitor.Leave("Extends", false)
|
||||
}
|
||||
if nn.Stmts != nil {
|
||||
t.visitor.Enter("Stmts", false)
|
||||
@@ -610,20 +588,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
|
||||
}
|
||||
t.visitor.Leave("Stmts", false)
|
||||
}
|
||||
case *ast.StmtInterfaceExtends:
|
||||
if nn == nil {
|
||||
return
|
||||
}
|
||||
if !t.visitor.EnterNode(nn) {
|
||||
return
|
||||
}
|
||||
if nn.InterfaceNames != nil {
|
||||
t.visitor.Enter("InterfaceNames", false)
|
||||
for _, c := range nn.InterfaceNames {
|
||||
t.Traverse(c)
|
||||
}
|
||||
t.visitor.Leave("InterfaceNames", false)
|
||||
}
|
||||
case *ast.StmtLabel:
|
||||
if nn == nil {
|
||||
return
|
||||
|
||||
@@ -286,8 +286,11 @@ func (v *Dumper) StmtClass(n *ast.StmtClass) {
|
||||
v.dumpVertexList("Arguments", n.Arguments)
|
||||
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
|
||||
v.dumpToken("CloseParenthesisTkn", n.CloseParenthesisTkn)
|
||||
v.dumpToken("ExtendsTkn", n.ExtendsTkn)
|
||||
v.dumpVertex("Extends", n.Extends)
|
||||
v.dumpVertex("Implements", n.Implements)
|
||||
v.dumpToken("ImplementsTkn", n.ImplementsTkn)
|
||||
v.dumpVertexList("Implements", n.Implements)
|
||||
v.dumpTokenList("ImplementsSeparatorTkns", n.ImplementsSeparatorTkns)
|
||||
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
|
||||
v.dumpVertexList("Stmts", n.Stmts)
|
||||
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
|
||||
@@ -311,31 +314,6 @@ func (v *Dumper) StmtClassConstList(n *ast.StmtClassConstList) {
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) StmtClassExtends(n *ast.StmtClassExtends) {
|
||||
v.print(0, "&ast.StmtClassExtends{\n")
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpToken("ExtendTkn", n.ExtendTkn)
|
||||
v.dumpVertex("ClassName", n.ClassName)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) StmtClassImplements(n *ast.StmtClassImplements) {
|
||||
v.print(0, "&ast.StmtClassImplements{\n")
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpToken("ImplementsTkn", n.ImplementsTkn)
|
||||
v.dumpVertexList("InterfaceNames", n.InterfaceNames)
|
||||
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) StmtClassMethod(n *ast.StmtClassMethod) {
|
||||
v.print(0, "&ast.StmtClassMethod{\n")
|
||||
v.indent++
|
||||
@@ -666,7 +644,9 @@ func (v *Dumper) StmtInterface(n *ast.StmtInterface) {
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpToken("InterfaceTkn", n.InterfaceTkn)
|
||||
v.dumpVertex("InterfaceName", n.InterfaceName)
|
||||
v.dumpVertex("Extends", n.Extends)
|
||||
v.dumpToken("ExtendsTkn", n.ExtendsTkn)
|
||||
v.dumpVertexList("Extends", n.Extends)
|
||||
v.dumpTokenList("ExtendsSeparatorTkns", n.ExtendsSeparatorTkns)
|
||||
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
|
||||
v.dumpVertexList("Stmts", n.Stmts)
|
||||
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
|
||||
@@ -675,19 +655,6 @@ func (v *Dumper) StmtInterface(n *ast.StmtInterface) {
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) StmtInterfaceExtends(n *ast.StmtInterfaceExtends) {
|
||||
v.print(0, "&ast.StmtInterfaceExtends{\n")
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpToken("ExtendsTkn", n.ExtendsTkn)
|
||||
v.dumpVertexList("InterfaceNames", n.InterfaceNames)
|
||||
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) StmtLabel(n *ast.StmtLabel) {
|
||||
v.print(0, "&ast.StmtLabel{\n")
|
||||
v.indent++
|
||||
@@ -849,8 +816,6 @@ func (v *Dumper) StmtTrait(n *ast.StmtTrait) {
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpToken("TraitTkn", n.TraitTkn)
|
||||
v.dumpVertex("TraitName", n.TraitName)
|
||||
v.dumpVertex("Extends", n.Extends)
|
||||
v.dumpVertex("Implements", n.Implements)
|
||||
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
|
||||
v.dumpVertexList("Stmts", n.Stmts)
|
||||
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
|
||||
|
||||
@@ -281,12 +281,14 @@ func (f *formatter) StmtClass(n *ast.StmtClass) {
|
||||
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
if n.Extends != nil {
|
||||
n.ExtendsTkn = f.newToken(token.T_EXTENDS, []byte("extends"))
|
||||
n.Extends.Accept(f)
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
}
|
||||
|
||||
if n.Implements != nil {
|
||||
n.Implements.Accept(f)
|
||||
n.ImplementsTkn = f.newToken(token.T_IMPLEMENTS, []byte("implements"))
|
||||
n.ImplementsSeparatorTkns = f.formatList(n.Implements, ',')
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
}
|
||||
|
||||
@@ -318,20 +320,6 @@ func (f *formatter) StmtClassConstList(n *ast.StmtClassConstList) {
|
||||
n.SemiColonTkn = f.newSemicolonTkn()
|
||||
}
|
||||
|
||||
func (f *formatter) StmtClassExtends(n *ast.StmtClassExtends) {
|
||||
n.ExtendTkn = f.newToken(token.T_EXTENDS, []byte("extends"))
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
|
||||
n.ClassName.Accept(f)
|
||||
}
|
||||
|
||||
func (f *formatter) StmtClassImplements(n *ast.StmtClassImplements) {
|
||||
n.ImplementsTkn = f.newToken(token.T_IMPLEMENTS, []byte("implements"))
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
|
||||
n.SeparatorTkns = f.formatList(n.InterfaceNames, ',')
|
||||
}
|
||||
|
||||
func (f *formatter) StmtClassMethod(n *ast.StmtClassMethod) {
|
||||
for _, m := range n.Modifiers {
|
||||
m.Accept(f)
|
||||
@@ -677,7 +665,8 @@ func (f *formatter) StmtInterface(n *ast.StmtInterface) {
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
|
||||
if n.Extends != nil {
|
||||
n.Extends.Accept(f)
|
||||
n.ExtendsTkn = f.newToken(token.T_EXTENDS, []byte("extends"))
|
||||
n.ExtendsSeparatorTkns = f.formatList(n.Extends, ',')
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
}
|
||||
|
||||
@@ -695,16 +684,6 @@ func (f *formatter) StmtInterface(n *ast.StmtInterface) {
|
||||
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
|
||||
}
|
||||
|
||||
func (f *formatter) StmtInterfaceExtends(n *ast.StmtInterfaceExtends) {
|
||||
n.ExtendsTkn = f.newToken(token.T_EXTENDS, []byte("extends"))
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
|
||||
n.SeparatorTkns = nil
|
||||
if len(n.InterfaceNames) > 0 {
|
||||
n.SeparatorTkns = f.formatList(n.InterfaceNames, ',')
|
||||
}
|
||||
}
|
||||
|
||||
func (f *formatter) StmtLabel(n *ast.StmtLabel) {
|
||||
n.LabelName.Accept(f)
|
||||
n.ColonTkn = f.newToken(':', []byte(":"))
|
||||
@@ -865,16 +844,6 @@ func (f *formatter) StmtTrait(n *ast.StmtTrait) {
|
||||
n.TraitName.Accept(f)
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
|
||||
if n.Extends != nil {
|
||||
n.Extends.Accept(f)
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
}
|
||||
|
||||
if n.Implements != nil {
|
||||
n.Implements.Accept(f)
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
}
|
||||
|
||||
n.OpenCurlyBracketTkn = f.newToken('{', []byte("{"))
|
||||
|
||||
if len(n.Stmts) > 0 {
|
||||
|
||||
@@ -519,12 +519,10 @@ func TestFormatter_Class_Extends(t *testing.T) {
|
||||
ClassName: &ast.Identifier{
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
Extends: &ast.StmtClassExtends{
|
||||
ClassName: &ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("bar"),
|
||||
},
|
||||
Extends: &ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("bar"),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -556,13 +554,11 @@ func TestFormatter_Class_Implements(t *testing.T) {
|
||||
ClassName: &ast.Identifier{
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
Implements: &ast.StmtClassImplements{
|
||||
InterfaceNames: []ast.Vertex{
|
||||
&ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("bar"),
|
||||
},
|
||||
Implements: []ast.Vertex{
|
||||
&ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("bar"),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -669,69 +665,6 @@ func TestFormatter_StmtClassConstList_Modifier(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatter_ClassExtends(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
n := &ast.StmtClassExtends{
|
||||
ClassName: &ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
|
||||
n.Accept(f)
|
||||
|
||||
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
|
||||
n.Accept(p)
|
||||
|
||||
expected := `extends foo`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatter_ClassImplements(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
n := &ast.StmtClassImplements{
|
||||
InterfaceNames: []ast.Vertex{
|
||||
&ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
},
|
||||
},
|
||||
&ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("bar"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
|
||||
n.Accept(f)
|
||||
|
||||
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
|
||||
n.Accept(p)
|
||||
|
||||
expected := `implements foo, bar`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatter_ClassMethod(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
@@ -1881,13 +1814,11 @@ func TestFormatter_StmtInterface_Extends(t *testing.T) {
|
||||
InterfaceName: &ast.Identifier{
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
Extends: &ast.StmtInterfaceExtends{
|
||||
InterfaceNames: []ast.Vertex{
|
||||
&ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("bar"),
|
||||
},
|
||||
Extends: []ast.Vertex{
|
||||
&ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("bar"),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -1913,42 +1844,6 @@ func TestFormatter_StmtInterface_Extends(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatter_StmtInterfaceExtends(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
n := &ast.StmtInterfaceExtends{
|
||||
InterfaceNames: []ast.Vertex{
|
||||
&ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
},
|
||||
},
|
||||
&ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("bar"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
|
||||
n.Accept(f)
|
||||
|
||||
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
|
||||
n.Accept(p)
|
||||
|
||||
expected := `extends foo, bar`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatter_StmtLabel(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
@@ -2465,82 +2360,6 @@ func TestFormatter_StmtTrait(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatter_StmtTrait_Extends(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
n := &ast.StmtTrait{
|
||||
TraitName: &ast.Identifier{
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
Extends: &ast.StmtClassExtends{
|
||||
ClassName: &ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("bar"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.StmtNop{},
|
||||
},
|
||||
}
|
||||
|
||||
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
|
||||
n.Accept(f)
|
||||
|
||||
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
|
||||
n.Accept(p)
|
||||
|
||||
expected := `trait foo extends bar {
|
||||
;
|
||||
}`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatter_StmtTrait_Implements(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
n := &ast.StmtTrait{
|
||||
TraitName: &ast.Identifier{
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
Implements: &ast.StmtClassImplements{
|
||||
InterfaceNames: []ast.Vertex{
|
||||
&ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Value: []byte("bar"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.StmtNop{},
|
||||
},
|
||||
}
|
||||
|
||||
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
|
||||
n.Accept(f)
|
||||
|
||||
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
|
||||
n.Accept(p)
|
||||
|
||||
expected := `trait foo implements bar {
|
||||
;
|
||||
}`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatter_StmtTraitMethodRef(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
|
||||
@@ -73,11 +73,11 @@ func (nsr *NamespaceResolver) StmtGroupUse(n *ast.StmtGroupUse) {
|
||||
|
||||
func (nsr *NamespaceResolver) StmtClass(n *ast.StmtClass) {
|
||||
if n.Extends != nil {
|
||||
nsr.ResolveName(n.Extends.(*ast.StmtClassExtends).ClassName, "")
|
||||
nsr.ResolveName(n.Extends, "")
|
||||
}
|
||||
|
||||
if n.Implements != nil {
|
||||
for _, interfaceName := range n.Implements.(*ast.StmtClassImplements).InterfaceNames {
|
||||
for _, interfaceName := range n.Implements {
|
||||
nsr.ResolveName(interfaceName, "")
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func (nsr *NamespaceResolver) StmtClass(n *ast.StmtClass) {
|
||||
|
||||
func (nsr *NamespaceResolver) StmtInterface(n *ast.StmtInterface) {
|
||||
if n.Extends != nil {
|
||||
for _, interfaceName := range n.Extends.(*ast.StmtInterfaceExtends).InterfaceNames {
|
||||
for _, interfaceName := range n.Extends {
|
||||
nsr.ResolveName(interfaceName, "")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -401,13 +401,9 @@ func TestResolveClassName(t *testing.T) {
|
||||
|
||||
class := &ast.StmtClass{
|
||||
ClassName: &ast.Identifier{Value: []byte("A")},
|
||||
Extends: &ast.StmtClassExtends{
|
||||
ClassName: nameAB,
|
||||
},
|
||||
Implements: &ast.StmtClassImplements{
|
||||
InterfaceNames: []ast.Vertex{
|
||||
nameBC,
|
||||
},
|
||||
Extends: nameAB,
|
||||
Implements: []ast.Vertex{
|
||||
nameBC,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -436,11 +432,9 @@ func TestResolveInterfaceName(t *testing.T) {
|
||||
|
||||
interfaceNode := &ast.StmtInterface{
|
||||
InterfaceName: &ast.Identifier{Value: []byte("A")},
|
||||
Extends: &ast.StmtInterfaceExtends{
|
||||
InterfaceNames: []ast.Vertex{
|
||||
nameAB,
|
||||
nameBC,
|
||||
},
|
||||
Extends: []ast.Vertex{
|
||||
nameAB,
|
||||
nameBC,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -62,14 +62,6 @@ func (v *Null) StmtClassConstList(_ *ast.StmtClassConstList) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtClassExtends(_ *ast.StmtClassExtends) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtClassImplements(_ *ast.StmtClassImplements) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtClassMethod(_ *ast.StmtClassMethod) {
|
||||
// do nothing
|
||||
}
|
||||
@@ -154,10 +146,6 @@ func (v *Null) StmtInterface(_ *ast.StmtInterface) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtInterfaceExtends(_ *ast.StmtInterfaceExtends) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtLabel(_ *ast.StmtLabel) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@@ -194,8 +194,10 @@ func (p *printer) StmtClass(n *ast.StmtClass) {
|
||||
p.printToken(n.OpenParenthesisTkn, p.ifNodeList(n.Arguments, []byte("(")))
|
||||
p.printSeparatedList(n.Arguments, n.SeparatorTkns, []byte(","))
|
||||
p.printToken(n.CloseParenthesisTkn, p.ifNodeList(n.Arguments, []byte(")")))
|
||||
p.printToken(n.ExtendsTkn, p.ifNode(n.Extends, []byte("extends")))
|
||||
p.printNode(n.Extends)
|
||||
p.printNode(n.Implements)
|
||||
p.printToken(n.ImplementsTkn, p.ifNodeList(n.Implements, []byte("implements")))
|
||||
p.printSeparatedList(n.Implements, n.ImplementsSeparatorTkns, []byte(","))
|
||||
p.printToken(n.OpenCurlyBracketTkn, []byte("{"))
|
||||
p.printList(n.Stmts)
|
||||
p.printToken(n.CloseCurlyBracketTkn, []byte("}"))
|
||||
@@ -208,16 +210,6 @@ func (p *printer) StmtClassConstList(n *ast.StmtClassConstList) {
|
||||
p.printToken(n.SemiColonTkn, []byte(";"))
|
||||
}
|
||||
|
||||
func (p *printer) StmtClassExtends(n *ast.StmtClassExtends) {
|
||||
p.printToken(n.ExtendTkn, []byte("extends"))
|
||||
p.printNode(n.ClassName)
|
||||
}
|
||||
|
||||
func (p *printer) StmtClassImplements(n *ast.StmtClassImplements) {
|
||||
p.printToken(n.ImplementsTkn, []byte("implements"))
|
||||
p.printSeparatedList(n.InterfaceNames, n.SeparatorTkns, []byte(","))
|
||||
}
|
||||
|
||||
func (p *printer) StmtClassMethod(n *ast.StmtClassMethod) {
|
||||
p.printList(n.Modifiers)
|
||||
p.printToken(n.FunctionTkn, []byte("function"))
|
||||
@@ -436,17 +428,13 @@ func (p *printer) StmtInlineHtml(n *ast.StmtInlineHtml) {
|
||||
func (p *printer) StmtInterface(n *ast.StmtInterface) {
|
||||
p.printToken(n.InterfaceTkn, []byte("interface"))
|
||||
p.printNode(n.InterfaceName)
|
||||
p.printNode(n.Extends)
|
||||
p.printToken(n.ExtendsTkn, p.ifNodeList(n.Extends, []byte("extends")))
|
||||
p.printSeparatedList(n.Extends, n.ExtendsSeparatorTkns, []byte(","))
|
||||
p.printToken(n.OpenCurlyBracketTkn, []byte("{"))
|
||||
p.printList(n.Stmts)
|
||||
p.printToken(n.CloseCurlyBracketTkn, []byte("}"))
|
||||
}
|
||||
|
||||
func (p *printer) StmtInterfaceExtends(n *ast.StmtInterfaceExtends) {
|
||||
p.printToken(n.ExtendsTkn, []byte("extends"))
|
||||
p.printSeparatedList(n.InterfaceNames, n.SeparatorTkns, []byte(","))
|
||||
}
|
||||
|
||||
func (p *printer) StmtLabel(n *ast.StmtLabel) {
|
||||
p.printNode(n.LabelName)
|
||||
p.printToken(n.ColonTkn, []byte(":"))
|
||||
@@ -525,8 +513,6 @@ func (p *printer) StmtThrow(n *ast.StmtThrow) {
|
||||
func (p *printer) StmtTrait(n *ast.StmtTrait) {
|
||||
p.printToken(n.TraitTkn, []byte("trait"))
|
||||
p.printNode(n.TraitName)
|
||||
p.printNode(n.Extends)
|
||||
p.printNode(n.Implements)
|
||||
p.printToken(n.OpenCurlyBracketTkn, []byte("{"))
|
||||
p.printList(n.Stmts)
|
||||
p.printToken(n.CloseCurlyBracketTkn, []byte("}"))
|
||||
|
||||
@@ -29,11 +29,9 @@ func TestPrinterPrintFile(t *testing.T) {
|
||||
&ast.NameNamePart{Value: []byte("Bar")},
|
||||
},
|
||||
},
|
||||
Extends: &ast.StmtClassExtends{
|
||||
ClassName: &ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{Value: []byte("Baz")},
|
||||
},
|
||||
Extends: &ast.NameName{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{Value: []byte("Baz")},
|
||||
},
|
||||
},
|
||||
Stmts: []ast.Vertex{
|
||||
@@ -3255,14 +3253,10 @@ func TestPrinterPrintStmtClass(t *testing.T) {
|
||||
n := &ast.StmtClass{
|
||||
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("abstract")}},
|
||||
ClassName: &ast.Identifier{Value: []byte("Foo")},
|
||||
Extends: &ast.StmtClassExtends{
|
||||
ClassName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
||||
},
|
||||
Implements: &ast.StmtClassImplements{
|
||||
InterfaceNames: []ast.Vertex{
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Quuz")}}},
|
||||
},
|
||||
Extends: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
||||
Implements: []ast.Vertex{
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Quuz")}}},
|
||||
},
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.StmtClassConstList{
|
||||
@@ -3307,14 +3301,10 @@ func TestPrinterPrintStmtAnonymousClass(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Extends: &ast.StmtClassExtends{
|
||||
ClassName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
||||
},
|
||||
Implements: &ast.StmtClassImplements{
|
||||
InterfaceNames: []ast.Vertex{
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Quuz")}}},
|
||||
},
|
||||
Extends: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
||||
Implements: []ast.Vertex{
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Quuz")}}},
|
||||
},
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.StmtClassConstList{
|
||||
@@ -4122,11 +4112,9 @@ func TestPrinterPrintInterface(t *testing.T) {
|
||||
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
|
||||
n := &ast.StmtInterface{
|
||||
InterfaceName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
||||
Extends: &ast.StmtInterfaceExtends{
|
||||
InterfaceNames: []ast.Vertex{
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
||||
},
|
||||
Extends: []ast.Vertex{
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
||||
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
||||
},
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.StmtClassMethod{
|
||||
|
||||
Reference in New Issue
Block a user