refactoring: update ast structure of "StaticCall" node

This commit is contained in:
Vadym Slizov
2020-12-27 23:19:49 +02:00
parent 0f2341bfa9
commit ad884c99df
9 changed files with 432 additions and 321 deletions

View File

@@ -1681,14 +1681,16 @@ func (n *ExprShellExec) GetPosition() *position.Position {
// ExprStaticCall node
type ExprStaticCall struct {
Position *position.Position
Class Vertex
DoubleColonTkn *token.Token
Call Vertex
OpenParenthesisTkn *token.Token
Arguments []Vertex
SeparatorTkns []*token.Token
CloseParenthesisTkn *token.Token
Position *position.Position
Class Vertex
DoubleColonTkn *token.Token
OpenCurlyBracketTkn *token.Token
Call Vertex
CloseCurlyBracketTkn *token.Token
OpenParenthesisTkn *token.Token
Arguments []Vertex
SeparatorTkns []*token.Token
CloseParenthesisTkn *token.Token
}
func (n *ExprStaticCall) Accept(v NodeVisitor) {

View File

@@ -1473,7 +1473,9 @@ func (v *Dumper) ExprStaticCall(n *ast.ExprStaticCall) {
v.dumpPosition(n.Position)
v.dumpVertex("Class", n.Class)
v.dumpToken("DoubleColonTkn", n.DoubleColonTkn)
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
v.dumpVertex("Call", n.Call)
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
v.dumpToken("OpenParenthesisTkn", n.OpenParenthesisTkn)
v.dumpVertexList("Arguments", n.Arguments)
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)

View File

@@ -1394,6 +1394,17 @@ func (f *formatter) ExprShellExec(n *ast.ExprShellExec) {
func (f *formatter) ExprStaticCall(n *ast.ExprStaticCall) {
n.Class.Accept(f)
n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::"))
n.OpenCurlyBracketTkn = nil
n.CloseCurlyBracketTkn = nil
switch n.Call.(type) {
case *ast.Identifier:
case *ast.ExprVariable:
default:
n.OpenCurlyBracketTkn = f.newToken('{', []byte("{"))
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
}
n.Call.Accept(f)
n.OpenParenthesisTkn = f.newToken('(', []byte("("))

View File

@@ -4692,6 +4692,36 @@ func TestFormatter_ExprStaticCall(t *testing.T) {
}
}
func TestFormatter_ExprStaticCall_Expr(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.ExprStaticCall{
Class: &ast.NameName{
Parts: []ast.Vertex{
&ast.NameNamePart{
Value: []byte("foo"),
},
},
},
Call: &ast.ScalarString{
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_ExprStaticCall_Arguments(t *testing.T) {
o := bytes.NewBufferString("")

View File

@@ -840,7 +840,9 @@ func (p *printer) ExprShellExec(n *ast.ExprShellExec) {
func (p *printer) ExprStaticCall(n *ast.ExprStaticCall) {
p.printNode(n.Class)
p.printToken(n.DoubleColonTkn, []byte("::"))
p.printToken(n.OpenCurlyBracketTkn, nil)
p.printNode(n.Call)
p.printToken(n.CloseCurlyBracketTkn, nil)
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(")")))