refactoring: remove "StmtTraitMethodRef" node

This commit is contained in:
Vadym Slizov 2020-12-28 10:47:09 +02:00
parent c29e8ec0e0
commit c0465f9605
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
17 changed files with 456 additions and 555 deletions

View File

@ -24032,13 +24032,6 @@ func TestStmtTraitUse_Modifier(t *testing.T) {
StartPos: 30,
EndPos: 43,
},
Ref: &ast.StmtTraitMethodRef{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 33,
},
Method: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
@ -24070,7 +24063,6 @@ func TestStmtTraitUse_Modifier(t *testing.T) {
},
Value: []byte("one"),
},
},
AsTkn: &token.Token{
ID: token.T_AS,
Value: []byte("as"),
@ -24453,13 +24445,6 @@ func TestStmtTraitUse_AliasModifier(t *testing.T) {
StartPos: 30,
EndPos: 47,
},
Ref: &ast.StmtTraitMethodRef{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 33,
},
Method: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
@ -24491,7 +24476,6 @@ func TestStmtTraitUse_AliasModifier(t *testing.T) {
},
Value: []byte("one"),
},
},
AsTkn: &token.Token{
ID: token.T_AS,
Value: []byte("as"),
@ -24905,13 +24889,6 @@ func TestStmtTraitUse_Adaptions(t *testing.T) {
StartPos: 30,
EndPos: 58,
},
Ref: &ast.StmtTraitMethodRef{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 38,
},
Trait: &ast.NameName{
Position: &position.Position{
StartLine: 1,
@ -24982,7 +24959,6 @@ func TestStmtTraitUse_Adaptions(t *testing.T) {
},
Value: []byte("one"),
},
},
InsteadofTkn: &token.Token{
ID: token.T_INSTEADOF,
Value: []byte("insteadof"),
@ -25119,13 +25095,6 @@ func TestStmtTraitUse_Adaptions(t *testing.T) {
StartPos: 60,
EndPos: 75,
},
Ref: &ast.StmtTraitMethodRef{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 60,
EndPos: 68,
},
Trait: &ast.NameName{
Position: &position.Position{
StartLine: 1,
@ -25196,7 +25165,6 @@ func TestStmtTraitUse_Adaptions(t *testing.T) {
},
Value: []byte("one"),
},
},
AsTkn: &token.Token{
ID: token.T_AS,
Value: []byte("as"),

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -2295,7 +2295,9 @@ trait_precedence:
{
$$ = &ast.StmtTraitUsePrecedence{
Position: yylex.(*Parser).builder.NewNodeNodeListPosition($1, $3.(*ast.ParserSeparatedList).Items),
Ref: $1,
Trait: $1.(*ast.TraitMethodRef).Trait,
DoubleColonTkn: $1.(*ast.TraitMethodRef).DoubleColonTkn,
Method: $1.(*ast.TraitMethodRef).Method,
InsteadofTkn: $2,
Insteadof: $3.(*ast.ParserSeparatedList).Items,
SeparatorTkns: $3.(*ast.ParserSeparatedList).SeparatorTkns,
@ -2322,7 +2324,7 @@ trait_reference_list:
trait_method_reference:
T_STRING
{
$$ = &ast.StmtTraitMethodRef{
$$ = &ast.TraitMethodRef{
Position: yylex.(*Parser).builder.NewTokenPosition($1),
Method: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($1),
@ -2340,7 +2342,7 @@ trait_method_reference:
trait_method_reference_fully_qualified:
fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING
{
$$ = &ast.StmtTraitMethodRef{
$$ = &ast.TraitMethodRef{
Position: yylex.(*Parser).builder.NewNodeTokenPosition($1, $3),
Trait: $1,
DoubleColonTkn: $2,
@ -2358,7 +2360,9 @@ trait_alias:
{
$$ = &ast.StmtTraitUseAlias{
Position: yylex.(*Parser).builder.NewNodeTokenPosition($1, $4),
Ref: $1,
Trait: $1.(*ast.TraitMethodRef).Trait,
DoubleColonTkn: $1.(*ast.TraitMethodRef).DoubleColonTkn,
Method: $1.(*ast.TraitMethodRef).Method,
AsTkn: $2,
Modifier: $3,
Alias: &ast.Identifier{
@ -2372,7 +2376,9 @@ trait_alias:
{
$$ = &ast.StmtTraitUseAlias{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $3),
Ref: $1,
Trait: $1.(*ast.TraitMethodRef).Trait,
DoubleColonTkn: $1.(*ast.TraitMethodRef).DoubleColonTkn,
Method: $1.(*ast.TraitMethodRef).Method,
AsTkn: $2,
Modifier: $3,
}

View File

@ -26000,13 +26000,6 @@ func TestStmtTraitUse_Modifier(t *testing.T) {
StartPos: 30,
EndPos: 43,
},
Ref: &ast.StmtTraitMethodRef{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 33,
},
Method: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
@ -26038,7 +26031,6 @@ func TestStmtTraitUse_Modifier(t *testing.T) {
},
Value: []byte("one"),
},
},
AsTkn: &token.Token{
ID: token.T_AS,
Value: []byte("as"),
@ -26421,13 +26413,6 @@ func TestStmtTraitUse_AliasModifier(t *testing.T) {
StartPos: 30,
EndPos: 47,
},
Ref: &ast.StmtTraitMethodRef{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 33,
},
Method: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
@ -26459,7 +26444,6 @@ func TestStmtTraitUse_AliasModifier(t *testing.T) {
},
Value: []byte("one"),
},
},
AsTkn: &token.Token{
ID: token.T_AS,
Value: []byte("as"),
@ -26873,13 +26857,6 @@ func TestStmtTraitUse_Adaptions(t *testing.T) {
StartPos: 30,
EndPos: 58,
},
Ref: &ast.StmtTraitMethodRef{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 38,
},
Trait: &ast.NameName{
Position: &position.Position{
StartLine: 1,
@ -26950,7 +26927,6 @@ func TestStmtTraitUse_Adaptions(t *testing.T) {
},
Value: []byte("one"),
},
},
InsteadofTkn: &token.Token{
ID: token.T_INSTEADOF,
Value: []byte("insteadof"),
@ -27087,13 +27063,6 @@ func TestStmtTraitUse_Adaptions(t *testing.T) {
StartPos: 60,
EndPos: 75,
},
Ref: &ast.StmtTraitMethodRef{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 60,
EndPos: 68,
},
Trait: &ast.NameName{
Position: &position.Position{
StartLine: 1,
@ -27164,7 +27133,6 @@ func TestStmtTraitUse_Adaptions(t *testing.T) {
},
Value: []byte("one"),
},
},
AsTkn: &token.Token{
ID: token.T_AS,
Value: []byte("as"),

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -2133,7 +2133,9 @@ trait_precedence:
{
$$ = &ast.StmtTraitUsePrecedence{
Position: yylex.(*Parser).builder.NewNodeNodeListPosition($1, $3.(*ast.ParserSeparatedList).Items),
Ref: $1,
Trait: $1.(*ast.TraitMethodRef).Trait,
DoubleColonTkn: $1.(*ast.TraitMethodRef).DoubleColonTkn,
Method: $1.(*ast.TraitMethodRef).Method,
InsteadofTkn: $2,
Insteadof: $3.(*ast.ParserSeparatedList).Items,
SeparatorTkns: $3.(*ast.ParserSeparatedList).SeparatorTkns,
@ -2146,7 +2148,9 @@ trait_alias:
{
$$ = &ast.StmtTraitUseAlias{
Position: yylex.(*Parser).builder.NewNodeTokenPosition($1, $3),
Ref: $1,
Trait: $1.(*ast.TraitMethodRef).Trait,
DoubleColonTkn: $1.(*ast.TraitMethodRef).DoubleColonTkn,
Method: $1.(*ast.TraitMethodRef).Method,
AsTkn: $2,
Alias: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($3),
@ -2159,7 +2163,9 @@ trait_alias:
{
$$ = &ast.StmtTraitUseAlias{
Position: yylex.(*Parser).builder.NewNodeTokenPosition($1, $3),
Ref: $1,
Trait: $1.(*ast.TraitMethodRef).Trait,
DoubleColonTkn: $1.(*ast.TraitMethodRef).DoubleColonTkn,
Method: $1.(*ast.TraitMethodRef).Method,
AsTkn: $2,
Alias: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($3),
@ -2172,7 +2178,9 @@ trait_alias:
{
$$ = &ast.StmtTraitUseAlias{
Position: yylex.(*Parser).builder.NewNodeTokenPosition($1, $4),
Ref: $1,
Trait: $1.(*ast.TraitMethodRef).Trait,
DoubleColonTkn: $1.(*ast.TraitMethodRef).DoubleColonTkn,
Method: $1.(*ast.TraitMethodRef).Method,
AsTkn: $2,
Modifier: $3,
Alias: &ast.Identifier{
@ -2186,7 +2194,9 @@ trait_alias:
{
$$ = &ast.StmtTraitUseAlias{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $3),
Ref: $1,
Trait: $1.(*ast.TraitMethodRef).Trait,
DoubleColonTkn: $1.(*ast.TraitMethodRef).DoubleColonTkn,
Method: $1.(*ast.TraitMethodRef).Method,
AsTkn: $2,
Modifier: $3,
}
@ -2196,7 +2206,7 @@ trait_alias:
trait_method_reference:
identifier
{
$$ = &ast.StmtTraitMethodRef{
$$ = &ast.TraitMethodRef{
Position: yylex.(*Parser).builder.NewTokenPosition($1),
Method: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($1),
@ -2214,7 +2224,7 @@ trait_method_reference:
absolute_trait_method_reference:
name T_PAAMAYIM_NEKUDOTAYIM identifier
{
$$ = &ast.StmtTraitMethodRef{
$$ = &ast.TraitMethodRef{
Position: yylex.(*Parser).builder.NewNodeTokenPosition($1, $3),
Trait: $1,
DoubleColonTkn: $2,

View File

@ -64,7 +64,6 @@ type NodeVisitor interface {
StmtSwitch(n *StmtSwitch)
StmtThrow(n *StmtThrow)
StmtTrait(n *StmtTrait)
StmtTraitMethodRef(n *StmtTraitMethodRef)
StmtTraitUse(n *StmtTraitUse)
StmtTraitUseAlias(n *StmtTraitUseAlias)
StmtTraitUsePrecedence(n *StmtTraitUsePrecedence)

View File

@ -937,22 +937,6 @@ func (n *StmtTrait) GetPosition() *position.Position {
return n.Position
}
// StmtTraitMethodRef node
type StmtTraitMethodRef struct {
Position *position.Position
Trait Vertex
DoubleColonTkn *token.Token
Method Vertex
}
func (n *StmtTraitMethodRef) Accept(v NodeVisitor) {
v.StmtTraitMethodRef(n)
}
func (n *StmtTraitMethodRef) GetPosition() *position.Position {
return n.Position
}
// StmtTraitUse node
type StmtTraitUse struct {
Position *position.Position
@ -976,7 +960,9 @@ func (n *StmtTraitUse) GetPosition() *position.Position {
// StmtTraitUseAlias node
type StmtTraitUseAlias struct {
Position *position.Position
Ref Vertex
Trait Vertex
DoubleColonTkn *token.Token
Method Vertex
AsTkn *token.Token
Modifier Vertex
Alias Vertex
@ -994,7 +980,9 @@ func (n *StmtTraitUseAlias) GetPosition() *position.Position {
// StmtTraitUsePrecedence node
type StmtTraitUsePrecedence struct {
Position *position.Position
Ref Vertex
Trait Vertex
DoubleColonTkn *token.Token
Method Vertex
InsteadofTkn *token.Token
Insteadof []Vertex
SeparatorTkns []*token.Token
@ -2734,3 +2722,19 @@ func (n *ReturnType) Accept(v NodeVisitor) {
func (n *ReturnType) GetPosition() *position.Position {
return n.Position
}
// TraitMethodRef node
type TraitMethodRef struct {
Position *position.Position
Trait Vertex
DoubleColonTkn *token.Token
Method Vertex
}
func (n *TraitMethodRef) Accept(v NodeVisitor) {
// do nothing
}
func (n *TraitMethodRef) GetPosition() *position.Position {
return n.Position
}

View File

@ -776,23 +776,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
}
t.visitor.Leave("Stmts", false)
}
case *ast.StmtTraitMethodRef:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Trait != nil {
t.visitor.Enter("Trait", true)
t.Traverse(nn.Trait)
t.visitor.Leave("Trait", true)
}
if nn.Method != nil {
t.visitor.Enter("Method", true)
t.Traverse(nn.Method)
t.visitor.Leave("Method", true)
}
case *ast.StmtTraitUse:
if nn == nil {
return
@ -821,10 +804,15 @@ func (t *DFS) Traverse(n ast.Vertex) {
if !t.visitor.EnterNode(nn) {
return
}
if nn.Ref != nil {
t.visitor.Enter("Ref", true)
t.Traverse(nn.Ref)
t.visitor.Leave("Ref", true)
if nn.Trait != nil {
t.visitor.Enter("Trait", true)
t.Traverse(nn.Trait)
t.visitor.Leave("Trait", true)
}
if nn.Method != nil {
t.visitor.Enter("Method", true)
t.Traverse(nn.Method)
t.visitor.Leave("Method", true)
}
if nn.Modifier != nil {
t.visitor.Enter("Modifier", true)
@ -843,10 +831,15 @@ func (t *DFS) Traverse(n ast.Vertex) {
if !t.visitor.EnterNode(nn) {
return
}
if nn.Ref != nil {
t.visitor.Enter("Ref", true)
t.Traverse(nn.Ref)
t.visitor.Leave("Ref", true)
if nn.Trait != nil {
t.visitor.Enter("Trait", true)
t.Traverse(nn.Trait)
t.visitor.Leave("Trait", true)
}
if nn.Method != nil {
t.visitor.Enter("Method", true)
t.Traverse(nn.Method)
t.visitor.Leave("Method", true)
}
if nn.Insteadof != nil {
t.visitor.Enter("Insteadof", false)

View File

@ -824,19 +824,6 @@ func (v *Dumper) StmtTrait(n *ast.StmtTrait) {
v.print(v.indent, "},\n")
}
func (v *Dumper) StmtTraitMethodRef(n *ast.StmtTraitMethodRef) {
v.print(0, "&ast.StmtTraitMethodRef{\n")
v.indent++
v.dumpPosition(n.Position)
v.dumpVertex("Trait", n.Trait)
v.dumpToken("DoubleColonTkn", n.DoubleColonTkn)
v.dumpVertex("Method", n.Method)
v.indent--
v.print(v.indent, "},\n")
}
func (v *Dumper) StmtTraitUse(n *ast.StmtTraitUse) {
v.print(0, "&ast.StmtTraitUse{\n")
v.indent++
@ -859,7 +846,9 @@ func (v *Dumper) StmtTraitUseAlias(n *ast.StmtTraitUseAlias) {
v.indent++
v.dumpPosition(n.Position)
v.dumpVertex("Ref", n.Ref)
v.dumpVertex("Trait", n.Trait)
v.dumpToken("DoubleColonTkn", n.DoubleColonTkn)
v.dumpVertex("Method", n.Method)
v.dumpToken("AsTkn", n.AsTkn)
v.dumpVertex("Modifier", n.Modifier)
v.dumpVertex("Alias", n.Alias)
@ -874,7 +863,9 @@ func (v *Dumper) StmtTraitUsePrecedence(n *ast.StmtTraitUsePrecedence) {
v.indent++
v.dumpPosition(n.Position)
v.dumpVertex("Ref", n.Ref)
v.dumpVertex("Trait", n.Trait)
v.dumpToken("DoubleColonTkn", n.DoubleColonTkn)
v.dumpVertex("Method", n.Method)
v.dumpToken("InsteadofTkn", n.InsteadofTkn)
v.dumpVertexList("Insteadof", n.Insteadof)
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)

View File

@ -858,15 +858,6 @@ func (f *formatter) StmtTrait(n *ast.StmtTrait) {
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
}
func (f *formatter) StmtTraitMethodRef(n *ast.StmtTraitMethodRef) {
if n.Trait != nil {
n.Trait.Accept(f)
n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::"))
}
n.Method.Accept(f)
}
func (f *formatter) StmtTraitUse(n *ast.StmtTraitUse) {
n.UseTkn = f.newToken(token.T_USE, []byte("use"))
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
@ -897,7 +888,12 @@ func (f *formatter) StmtTraitUse(n *ast.StmtTraitUse) {
}
func (f *formatter) StmtTraitUseAlias(n *ast.StmtTraitUseAlias) {
n.Ref.Accept(f)
if n.Trait != nil {
n.Trait.Accept(f)
n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::"))
}
n.Method.Accept(f)
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
n.AsTkn = f.newToken(token.T_AS, []byte("as"))
@ -915,7 +911,12 @@ func (f *formatter) StmtTraitUseAlias(n *ast.StmtTraitUseAlias) {
}
func (f *formatter) StmtTraitUsePrecedence(n *ast.StmtTraitUsePrecedence) {
n.Ref.Accept(f)
if n.Trait != nil {
n.Trait.Accept(f)
n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::"))
}
n.Method.Accept(f)
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
n.InsteadofTkn = f.newToken(token.T_INSTEADOF, []byte("insteadof"))
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))

View File

@ -2360,59 +2360,6 @@ func TestFormatter_StmtTrait(t *testing.T) {
}
}
func TestFormatter_StmtTraitMethodRef(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.StmtTraitMethodRef{
Method: &ast.Identifier{
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 := `foo`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestFormatter_StmtTraitMethodRef_Trait(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.StmtTraitMethodRef{
Trait: &ast.NameName{
Parts: []ast.Vertex{
&ast.NameNamePart{
Value: []byte("foo"),
},
},
},
Method: &ast.Identifier{
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 := `foo::bar`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestFormatter_StmtTraitUse(t *testing.T) {
o := bytes.NewBufferString("")
@ -2471,11 +2418,9 @@ func TestFormatter_StmtTraitUse_Adaptations(t *testing.T) {
},
Adaptations: []ast.Vertex{
&ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Method: &ast.Identifier{
Value: []byte("foo"),
},
},
Alias: &ast.Identifier{
Value: []byte("baz"),
},
@ -2503,11 +2448,9 @@ func TestFormatter_StmtTraitUseAlias(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Method: &ast.Identifier{
Value: []byte("foo"),
},
},
Modifier: &ast.Identifier{
Value: []byte("public"),
},
@ -2530,15 +2473,49 @@ func TestFormatter_StmtTraitUseAlias(t *testing.T) {
}
}
func TestFormatter_StmtTraitUseAlias_Trait(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.StmtTraitUseAlias{
Trait: &ast.NameName{
Parts: []ast.Vertex{
&ast.NameNamePart{
Value: []byte("foo"),
},
},
},
Method: &ast.Identifier{
Value: []byte("bar"),
},
Modifier: &ast.Identifier{
Value: []byte("public"),
},
Alias: &ast.Identifier{
Value: []byte("baz"),
},
}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
n.Accept(f)
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n.Accept(p)
expected := `foo::bar as public baz;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestFormatter_StmtTraitUseAlias_Alias(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Method: &ast.Identifier{
Value: []byte("foo"),
},
},
Alias: &ast.Identifier{
Value: []byte("bar"),
},
@ -2562,11 +2539,9 @@ func TestFormatter_StmtTraitUseAlias_Modifier(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Method: &ast.Identifier{
Value: []byte("foo"),
},
},
Modifier: &ast.Identifier{
Value: []byte("public"),
},
@ -2590,11 +2565,9 @@ func TestFormatter_StmtTraitUsePrecedence(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.StmtTraitUsePrecedence{
Ref: &ast.StmtTraitMethodRef{
Method: &ast.Identifier{
Value: []byte("foo"),
},
},
Insteadof: []ast.Vertex{
&ast.NameName{
Parts: []ast.Vertex{
@ -2627,6 +2600,45 @@ func TestFormatter_StmtTraitUsePrecedence(t *testing.T) {
}
}
func TestFormatter_StmtTraitUsePrecedence_Trait(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.StmtTraitUsePrecedence{
Trait: &ast.NameName{
Parts: []ast.Vertex{
&ast.NameNamePart{
Value: []byte("foo"),
},
},
},
Method: &ast.Identifier{
Value: []byte("bar"),
},
Insteadof: []ast.Vertex{
&ast.NameName{
Parts: []ast.Vertex{
&ast.NameNamePart{
Value: []byte("baz"),
},
},
},
},
}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
n.Accept(f)
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n.Accept(p)
expected := `foo::bar insteadof baz;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestFormatter_StmtTry(t *testing.T) {
o := bytes.NewBufferString("")

View File

@ -187,7 +187,7 @@ func (nsr *NamespaceResolver) StmtTraitUse(n *ast.StmtTraitUse) {
for _, a := range n.Adaptations {
switch aa := a.(type) {
case *ast.StmtTraitUsePrecedence:
refTrait := aa.Ref.(*ast.StmtTraitMethodRef).Trait
refTrait := aa.Trait
if refTrait != nil {
nsr.ResolveName(refTrait, "")
}
@ -196,7 +196,7 @@ func (nsr *NamespaceResolver) StmtTraitUse(n *ast.StmtTraitUse) {
}
case *ast.StmtTraitUseAlias:
refTrait := aa.Ref.(*ast.StmtTraitMethodRef).Trait
refTrait := aa.Trait
if refTrait != nil {
nsr.ResolveName(refTrait, "")
}

View File

@ -356,17 +356,13 @@ func TestResolveTraitUse(t *testing.T) {
},
Adaptations: []ast.Vertex{
&ast.StmtTraitUsePrecedence{
Ref: &ast.StmtTraitMethodRef{
Trait: fullyQualifiedNameB,
Method: &ast.Identifier{Value: []byte("foo")},
},
Insteadof: []ast.Vertex{fullyQualifiedNameBC},
},
&ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Trait: relativeNameBC,
Method: &ast.Identifier{Value: []byte("foo")},
},
Alias: &ast.Identifier{Value: []byte("bar")},
},
},

View File

@ -194,10 +194,6 @@ func (v *Null) StmtTrait(_ *ast.StmtTrait) {
// do nothing
}
func (v *Null) StmtTraitMethodRef(_ *ast.StmtTraitMethodRef) {
// do nothing
}
func (v *Null) StmtTraitUse(_ *ast.StmtTraitUse) {
// do nothing
}

View File

@ -518,12 +518,6 @@ func (p *printer) StmtTrait(n *ast.StmtTrait) {
p.printToken(n.CloseCurlyBracketTkn, []byte("}"))
}
func (p *printer) StmtTraitMethodRef(n *ast.StmtTraitMethodRef) {
p.printNode(n.Trait)
p.printToken(n.DoubleColonTkn, p.ifNode(n.Trait, []byte("::")))
p.printNode(n.Method)
}
func (p *printer) StmtTraitUse(n *ast.StmtTraitUse) {
p.printToken(n.UseTkn, []byte("use"))
p.printSeparatedList(n.Traits, n.SeparatorTkns, []byte(","))
@ -534,7 +528,9 @@ func (p *printer) StmtTraitUse(n *ast.StmtTraitUse) {
}
func (p *printer) StmtTraitUseAlias(n *ast.StmtTraitUseAlias) {
p.printNode(n.Ref)
p.printNode(n.Trait)
p.printToken(n.DoubleColonTkn, p.ifNode(n.Trait, []byte("::")))
p.printNode(n.Method)
p.printToken(n.AsTkn, []byte("as"))
p.printNode(n.Modifier)
p.printNode(n.Alias)
@ -542,7 +538,9 @@ func (p *printer) StmtTraitUseAlias(n *ast.StmtTraitUseAlias) {
}
func (p *printer) StmtTraitUsePrecedence(n *ast.StmtTraitUsePrecedence) {
p.printNode(n.Ref)
p.printNode(n.Trait)
p.printToken(n.DoubleColonTkn, p.ifNode(n.Trait, []byte("::")))
p.printNode(n.Method)
p.printToken(n.InsteadofTkn, []byte("insteadof"))
p.printSeparatedList(n.Insteadof, n.SeparatorTkns, []byte(","))
p.printToken(n.SemiColonTkn, []byte(";"))

View File

@ -4452,50 +4452,13 @@ func TestPrinterPrintStmtThrow(t *testing.T) {
}
}
func TestPrinterPrintStmtTraitMethodRef(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.StmtTraitMethodRef{
Method: &ast.Identifier{Value: []byte("a")},
}
n.Accept(p)
expected := `a`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtTraitMethodRefFull(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.StmtTraitMethodRef{
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Method: &ast.Identifier{Value: []byte("a")},
}
n.Accept(p)
expected := `Foo::a`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtTraitUseAlias(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Method: &ast.Identifier{Value: []byte("a")},
},
Modifier: &ast.Identifier{Value: []byte("public")},
Alias: &ast.Identifier{Value: []byte("b")},
}
@ -4514,10 +4477,8 @@ func TestPrinterPrintStmtTraitUsePrecedence(t *testing.T) {
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.StmtTraitUsePrecedence{
Ref: &ast.StmtTraitMethodRef{
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Method: &ast.Identifier{Value: []byte("a")},
},
Insteadof: []ast.Vertex{
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
@ -4564,10 +4525,8 @@ func TestPrinterPrintStmtTraitAdaptations(t *testing.T) {
},
Adaptations: []ast.Vertex{
&ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Method: &ast.Identifier{Value: []byte("a")},
},
Alias: &ast.Identifier{Value: []byte("b")},
},
},