refactoring: remove ExprReference node

This commit is contained in:
Vadym Slizov
2020-12-26 22:38:35 +02:00
parent 0f5f5e7dc7
commit a593760569
13 changed files with 65 additions and 121 deletions

View File

@@ -1425,18 +1425,6 @@ func (v *Dumper) ExprPropertyFetch(n *ast.ExprPropertyFetch) {
v.print(v.indent, "},\n")
}
func (v *Dumper) ExprReference(n *ast.ExprReference) {
v.print(0, "&ast.ExprReference{\n")
v.indent++
v.dumpPosition(n.Position)
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
v.dumpVertex("Var", n.Var)
v.indent--
v.print(v.indent, "},\n")
}
func (v *Dumper) ExprRequire(n *ast.ExprRequire) {
v.print(0, "&ast.ExprRequire{\n")
v.indent++

View File

@@ -1349,11 +1349,6 @@ func (f *formatter) ExprPropertyFetch(n *ast.ExprPropertyFetch) {
n.Property.Accept(f)
}
func (f *formatter) ExprReference(n *ast.ExprReference) {
n.AmpersandTkn = f.newToken('&', []byte("&"))
n.Var.Accept(f)
}
func (f *formatter) ExprRequire(n *ast.ExprRequire) {
n.RequireTkn = f.newToken(token.T_REQUIRE, []byte("require"))
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))

View File

@@ -4483,31 +4483,6 @@ func TestFormatter_ExprPropertyFetch(t *testing.T) {
}
}
func TestFormatter_ExprReference(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.ExprReference{
Var: &ast.ExprVariable{
VarName: &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_ExprRequire(t *testing.T) {
o := bytes.NewBufferString("")

View File

@@ -362,10 +362,6 @@ func (v *Null) ExprPropertyFetch(_ *ast.ExprPropertyFetch) {
// do nothing
}
func (v *Null) ExprReference(_ *ast.ExprReference) {
// do nothing
}
func (v *Null) ExprRequire(_ *ast.ExprRequire) {
// do nothing
}

View File

@@ -817,11 +817,6 @@ func (p *printer) ExprPropertyFetch(n *ast.ExprPropertyFetch) {
p.printNode(n.Property)
}
func (p *printer) ExprReference(n *ast.ExprReference) {
p.printToken(n.AmpersandTkn, []byte("&"))
p.printNode(n.Var)
}
func (p *printer) ExprRequire(n *ast.ExprRequire) {
p.printToken(n.RequireTkn, []byte("require"))
p.printNode(n.Expr)

View File

@@ -1634,9 +1634,12 @@ func TestPrinterPrintExprArray(t *testing.T) {
},
&ast.ExprArrayItem{
Key: &ast.ScalarLnumber{Value: []byte("2")},
Val: &ast.ExprReference{Var: &ast.ExprVariable{
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Val: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")},
}},
},
},
&ast.ExprArrayItem{
Val: &ast.ExprVariable{
@@ -2323,25 +2326,6 @@ func TestPrinterPrintPropertyFetch(t *testing.T) {
}
}
func TestPrinterPrintExprReference(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.ExprReference{
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$foo")},
},
}
n.Accept(p)
expected := `&$foo`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintRequire(t *testing.T) {
o := bytes.NewBufferString("")
@@ -2413,9 +2397,12 @@ func TestPrinterPrintExprShortArray(t *testing.T) {
},
&ast.ExprArrayItem{
Key: &ast.ScalarLnumber{Value: []byte("2")},
Val: &ast.ExprReference{Var: &ast.ExprVariable{
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Val: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")},
}},
},
},
&ast.ExprArrayItem{
Val: &ast.ExprVariable{
@@ -2848,11 +2835,47 @@ func TestPrinterPrintAltForeach(t *testing.T) {
Key: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$key")},
},
Var: &ast.ExprReference{
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$val")},
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$val")},
},
ColonTkn: &token.Token{
Value: []byte(":"),
},
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$d")},
}},
},
},
}
n.Accept(p)
expected := `foreach($var as$key=>$val):$d;endforeach;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAltForeach_Reference(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.StmtForeach{
Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")},
},
Key: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$key")},
},
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$val")},
},
ColonTkn: &token.Token{
Value: []byte(":"),
},