[refactoring] update ast structure of "Echo" node

This commit is contained in:
Vadym Slizov
2020-09-10 23:11:08 +03:00
parent 3f12ada311
commit 7678303cb9
13 changed files with 1521 additions and 1614 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -45,7 +45,6 @@ func TestParseAndPrintPhp5Root(t *testing.T) {
}
func TestParseAndPrintPhp5Identifier(t *testing.T) {
// TODO: remove ; after <?php
src := `<? ;
/* Foo */
Foo ( ) ;
@@ -73,8 +72,7 @@ func TestParseAndPrintPhp5Parameter(t *testing.T) {
}
func TestParseAndPrintPhp5Argument(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
foo ( $a , $b
, $c
) ; `
@@ -89,8 +87,7 @@ func TestParseAndPrintPhp5Argument(t *testing.T) {
// test name
func TestParseAndPrintPhp5Names(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
foo ( ) ;
\ foo ( ) ;
namespace \ foo ( ) ;
@@ -169,8 +166,7 @@ func TestParseAndPrintPhp5String(t *testing.T) {
}
func TestParseAndPrintPhp5Heredoc(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
foo(<<<EAP
test
EAP
@@ -291,8 +287,7 @@ func TestParseAndPrintPhp5Cast(t *testing.T) {
// test expr
func TestParseAndPrintPhp5ArrayDimFetch(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
FOO [ ] ;
FOO [ 1 ] ;
$a [ ] ;
@@ -418,8 +413,7 @@ func TestParseAndPrintPhp5Closure(t *testing.T) {
}
func TestParseAndPrintPhp5ConstFetch(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
null ;
`
@@ -485,8 +479,7 @@ func TestParseAndPrintPhp5Exit(t *testing.T) {
}
func TestParseAndPrintPhp5FunctionCall(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
foo ( ) ;
$var ( $a , ... $b , $c ) ;
`
@@ -556,8 +549,7 @@ func TestParseAndPrintPhp5MethodCall(t *testing.T) {
}
func TestParseAndPrintPhp5New(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
new Foo ;
@@ -670,8 +662,7 @@ func TestParseAndPrintPhp5ShortArray(t *testing.T) {
}
func TestParseAndPrintPhp5StaticCall(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
Foo :: bar ( $a , $b ) ;`
actual := printPhp5(parsePhp5(src))
@@ -682,8 +673,7 @@ func TestParseAndPrintPhp5StaticCall(t *testing.T) {
}
func TestParseAndPrintPhp5StaticPropertyFetch(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
Foo :: $bar ;`
actual := printPhp5(parsePhp5(src))
@@ -743,8 +733,7 @@ func TestParseAndPrintPhp5Yield(t *testing.T) {
// test stmt
func TestParseAndPrintPhp5AltIf(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
if ( 1 ) :
// do nothing
elseif ( 2 ) :
@@ -762,8 +751,7 @@ func TestParseAndPrintPhp5AltIf(t *testing.T) {
}
func TestParseAndPrintPhp5AltFor(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
for ( $a ; $b ; $c ) :
endfor ;
@@ -791,8 +779,7 @@ func TestParseAndPrintPhp5AltForeach(t *testing.T) {
}
func TestParseAndPrintPhp5AltSwitch(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
switch ( $a ) :
case 1 :
@@ -822,8 +809,7 @@ func TestParseAndPrintPhp5AltSwitch(t *testing.T) {
}
func TestParseAndPrintPhp5AltWhile(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
while ( $a ) :
// do nothing
@@ -837,8 +823,7 @@ func TestParseAndPrintPhp5AltWhile(t *testing.T) {
}
func TestParseAndPrintPhp5Break(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
break ;
break 1 ;
@@ -901,8 +886,7 @@ func TestParseAndPrintPhp5ClassConstList(t *testing.T) {
}
func TestParseAndPrintPhp5ConstList(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
const FOO = 1 , BAR = 2 ;
`
@@ -914,8 +898,7 @@ func TestParseAndPrintPhp5ConstList(t *testing.T) {
}
func TestParseAndPrintPhp5Continue(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
continue ;
continue 1 ;
@@ -949,8 +932,7 @@ func TestParseAndPrintPhp5Declare(t *testing.T) {
}
func TestParseAndPrintPhp5DoWhile(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
do {
;
} while ( $a ) ;
@@ -992,8 +974,7 @@ func TestParseAndPrintPhp5IfExpression(t *testing.T) {
}
func TestParseAndPrintPhp5For(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
for ( $i = 0 ; $i < 3 ; $i ++ )
echo $i . PHP_EOL;
@@ -1036,8 +1017,7 @@ func TestParseAndPrintPhp5Function(t *testing.T) {
}
func TestParseAndPrintPhp5Global(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
global $a , $b ;`
actual := printPhp5(parsePhp5(src))
@@ -1059,8 +1039,7 @@ func TestParseAndPrintPhp5Goto(t *testing.T) {
}
func TestParseAndPrintPhp5HaltCompiler(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
__halt_compiler ( ) ;
this text is ignored by parser
`
@@ -1073,8 +1052,7 @@ func TestParseAndPrintPhp5HaltCompiler(t *testing.T) {
}
func TestParseAndPrintPhp5IfElseIfElse(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
if ( 1 ) ;
elseif ( 2 ) {
;
@@ -1137,8 +1115,7 @@ func TestParseAndPrintPhp5GotoLabel(t *testing.T) {
}
func TestParseAndPrintPhp5Namespace(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
namespace Foo \ Bar ;
namespace Baz {
@@ -1156,7 +1133,7 @@ func TestParseAndPrintPhp5Namespace(t *testing.T) {
}
func TestParseAndPrintPhp5Nop(t *testing.T) {
src := `<?php ; `
src := `<?php `
actual := printPhp5(parsePhp5(src))
@@ -1209,8 +1186,7 @@ func TestParseAndPrintPhp5Return(t *testing.T) {
}
func TestParseAndPrintPhp5StaticVar(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
static $a , $b = ' ' ;
`
@@ -1222,8 +1198,7 @@ func TestParseAndPrintPhp5StaticVar(t *testing.T) {
}
func TestParseAndPrintPhp5StmtList(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
{
;
}
@@ -1237,8 +1212,7 @@ func TestParseAndPrintPhp5StmtList(t *testing.T) {
}
func TestParseAndPrintPhp5Switch(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
switch ( $a ) {
case 1 : ;
@@ -1332,8 +1306,7 @@ func TestParseAndPrintPhp5Unset(t *testing.T) {
}
func TestParseAndPrintPhp5UseList(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
use Foo ;
use \ Foo as Bar ;
use function \ Foo as Bar ;
@@ -1348,8 +1321,7 @@ func TestParseAndPrintPhp5UseList(t *testing.T) {
}
func TestParseAndPrintPhp5While(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
while ( $a ) echo '' ;
while ( $a ) { }
while ( $a ) ;
@@ -1365,8 +1337,7 @@ func TestParseAndPrintPhp5While(t *testing.T) {
// other
func TestParseAndPrintPhp5Parentheses(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
global $ { $b } ;
$b = (($a));
$b = ( ($a) );

View File

@@ -13,8 +13,7 @@ import (
)
func ExamplePrinter() {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
namespace Foo;
@@ -38,7 +37,7 @@ abstract class Bar extends Baz
// change namespace
parts := &rootNode.(*ast.Root).Stmts[1].(*ast.StmtNamespace).Name.(*ast.NameName).Parts
parts := &rootNode.(*ast.Root).Stmts[0].(*ast.StmtNamespace).Name.(*ast.NameName).Parts
*parts = append(*parts, &ast.NameNamePart{Value: []byte("Quuz")})
// print
@@ -47,7 +46,7 @@ abstract class Bar extends Baz
p.Print(rootNode)
// Output:
//<?php ;
//<?php
//
// namespace Foo\Quuz;
//
@@ -95,7 +94,6 @@ func TestParseAndPrintRoot(t *testing.T) {
}
func TestParseAndPrintIdentifier(t *testing.T) {
// TODO: remove ; after <?php
src := `<? ;
/* Foo */
Foo ( ) ;
@@ -152,8 +150,7 @@ func TestParseAndPrintNullable(t *testing.T) {
}
func TestParseAndPrintArgument(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
foo ( $a , $b
, ... $c ,
) ; `
@@ -168,8 +165,7 @@ func TestParseAndPrintArgument(t *testing.T) {
// test name
func TestParseAndPrintNames(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
foo ( ) ;
\ foo ( ) ;
namespace \ foo ( ) ;
@@ -248,8 +244,7 @@ func TestParseAndPrintString(t *testing.T) {
}
func TestParseAndPrintHeredoc(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
foo(<<<EAP
test
EAP
@@ -373,8 +368,7 @@ func TestParseAndPrintCast(t *testing.T) {
// test expr
func TestParseAndPrintArrayDimFetch(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
FOO [ ] ;
FOO [ 1 ] ;
$a [ ] ;
@@ -513,8 +507,7 @@ func TestParseAndPrintArrowFunction(t *testing.T) {
}
func TestParseAndPrintConstFetch(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
null ;
`
@@ -580,8 +573,7 @@ func TestParseAndPrintExit(t *testing.T) {
}
func TestParseAndPrintFunctionCall(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
foo ( ) ;
$var ( $a , ... $b , $c ) ;
`
@@ -782,8 +774,7 @@ func TestParseAndPrintShortList(t *testing.T) {
}
func TestParseAndPrintStaticCall(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
Foo :: bar ( $a , $b ) ;`
actual := print(parse(src))
@@ -794,8 +785,7 @@ func TestParseAndPrintStaticCall(t *testing.T) {
}
func TestParseAndPrintStaticPropertyFetch(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
Foo :: $bar ;`
actual := print(parse(src))
@@ -856,8 +846,7 @@ func TestParseAndPrintYield(t *testing.T) {
// test stmt
func TestParseAndPrintAltIf(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
if ( 1 ) :
// do nothing
elseif ( 2 ) :
@@ -875,8 +864,7 @@ func TestParseAndPrintAltIf(t *testing.T) {
}
func TestParseAndPrintAltFor(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
for ( $a ; $b ; $c ) :
endfor ;
@@ -904,8 +892,7 @@ func TestParseAndPrintAltForeach(t *testing.T) {
}
func TestParseAndPrintAltSwitch(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
switch ( $a ) :
case 1 :
@@ -935,8 +922,7 @@ func TestParseAndPrintAltSwitch(t *testing.T) {
}
func TestParseAndPrintAltWhile(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
while ( $a ) :
// do nothing
@@ -950,8 +936,7 @@ func TestParseAndPrintAltWhile(t *testing.T) {
}
func TestParseAndPrintBreak(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
break ;
break 1 ;
@@ -1017,8 +1002,7 @@ func TestParseAndPrintClassConstList(t *testing.T) {
}
func TestParseAndPrintConstList(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
const FOO = 1 , BAR = 2 ;
`
@@ -1030,8 +1014,7 @@ func TestParseAndPrintConstList(t *testing.T) {
}
func TestParseAndPrintContinue(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
continue ;
continue 1 ;
@@ -1065,8 +1048,7 @@ func TestParseAndPrintDeclare(t *testing.T) {
}
func TestParseAndPrintDoWhile(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
do {
;
} while ( $a ) ;
@@ -1110,8 +1092,7 @@ func TestParseAndPrintIfExpression(t *testing.T) {
}
func TestParseAndPrintFor(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
for ( $i = 0 ; $i < 3 ; $i ++ )
echo $i . PHP_EOL;
@@ -1154,8 +1135,7 @@ func TestParseAndPrintFunction(t *testing.T) {
}
func TestParseAndPrintGlobal(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
global $a , $b ;`
actual := print(parse(src))
@@ -1177,8 +1157,7 @@ func TestParseAndPrintGoto(t *testing.T) {
}
func TestParseAndPrintGroupUse(t *testing.T) {
// TODO: remove ; after <?php
src := `<?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 , } ;
@@ -1192,8 +1171,7 @@ func TestParseAndPrintGroupUse(t *testing.T) {
}
func TestParseAndPrintHaltCompiler(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
__halt_compiler ( ) ;
this text is ignored by parser
`
@@ -1206,8 +1184,7 @@ func TestParseAndPrintHaltCompiler(t *testing.T) {
}
func TestParseAndPrintIfElseIfElse(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
if ( 1 ) ;
elseif ( 2 ) {
;
@@ -1270,8 +1247,7 @@ func TestParseAndPrintGotoLabel(t *testing.T) {
}
func TestParseAndPrintNamespace(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
namespace Foo \ Bar ;
namespace Baz {
@@ -1289,7 +1265,7 @@ func TestParseAndPrintNamespace(t *testing.T) {
}
func TestParseAndPrintNop(t *testing.T) {
src := `<?php ; `
src := `<?php `
actual := print(parse(src))
@@ -1342,8 +1318,7 @@ func TestParseAndPrintReturn(t *testing.T) {
}
func TestParseAndPrintStaticVar(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
static $a , $b = foo ( ) ;
`
@@ -1355,8 +1330,7 @@ func TestParseAndPrintStaticVar(t *testing.T) {
}
func TestParseAndPrintStmtList(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
{
;
}
@@ -1370,8 +1344,7 @@ func TestParseAndPrintStmtList(t *testing.T) {
}
func TestParseAndPrintSwitch(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
switch ( $a ) {
case 1 : ;
@@ -1465,8 +1438,7 @@ func TestParseAndPrintUnset(t *testing.T) {
}
func TestParseAndPrintUseList(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
use Foo ;
use \ Foo as Bar ;
use function \ Foo as Bar ;
@@ -1481,8 +1453,7 @@ func TestParseAndPrintUseList(t *testing.T) {
}
func TestParseAndPrintWhile(t *testing.T) {
// TODO: remove ; after <?php
src := `<?php ;
src := `<?php
while ( $a ) echo '' ;
while ( $a ) { }
while ( $a ) ;

View File

@@ -5,8 +5,6 @@ import (
"testing"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/token"
"github.com/z7zmey/php-parser/pkg/printer"
)
@@ -71,45 +69,25 @@ func TestPrinterPrintFileInlineHtml(t *testing.T) {
p.Print(&ast.Root{
Stmts: []ast.Vertex{
&ast.StmtInlineHtml{Value: []byte("<div>HTML</div>")},
&ast.StmtExpression{
Expr: &ast.ExprVariable{
Node: ast.Node{
Tokens: token.Collection{
token.Start: []*token.Token{
{
ID: token.ID('$'),
Value: []byte("$"),
},
},
},
},
VarName: &ast.Identifier{
Value: []byte("a"),
&ast.StmtEcho{
Exprs: []ast.Vertex{
&ast.ScalarString{
Value: []byte(`"a"`),
},
},
},
&ast.StmtInlineHtml{Value: []byte("<div>HTML</div>")},
&ast.StmtExpression{
Expr: &ast.ExprVariable{
Node: ast.Node{
Tokens: token.Collection{
token.Start: []*token.Token{
{
ID: token.ID('$'),
Value: []byte("$"),
},
},
},
},
VarName: &ast.Identifier{
Value: []byte("a"),
&ast.StmtEcho{
Exprs: []ast.Vertex{
&ast.ScalarString{
Value: []byte(`"b"`),
},
},
},
},
})
expected := `<div>HTML</div><?php $a;?><div>HTML</div><?php $a;`
expected := `<div>HTML</div><?php echo "a";?><div>HTML</div><?php echo "b";`
actual := o.String()
if expected != actual {