Merge branch 'master' into refactoring

This commit is contained in:
Vadym Slizov
2020-07-29 22:23:44 +02:00
9 changed files with 5899 additions and 4548 deletions

View File

@@ -133,6 +133,12 @@ func (nsr *NamespaceResolver) ExprClosure(n *ast.ExprClosure) {
}
}
func (nsr *NamespaceResolver) StmtPropertyList(n *ast.StmtPropertyList) {
if n.Type != nil {
nsr.ResolveType(n.Type)
}
}
func (nsr *NamespaceResolver) StmtConstList(n *ast.StmtConstList) {
for _, constant := range n.Consts {
nsr.AddNamespacedName(constant, string(constant.(*ast.StmtConstant).ConstantName.(*ast.Identifier).Value))

View File

@@ -978,3 +978,55 @@ func TestDoNotResolveReservedSpecialNames(t *testing.T) {
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
}
func TestResolvePropertyTypeName(t *testing.T) {
nameSimple := &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("A")}, &ast.NameNamePart{Value: []byte("B")}}}
nameRelative := &ast.NameRelative{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("A")}, &ast.NameNamePart{Value: []byte("B")}}}
nameFullyQualified := &ast.NameFullyQualified{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("A")}, &ast.NameNamePart{Value: []byte("B")}}}
propertyNodeSimple := &ast.StmtPropertyList{
Type: nameSimple,
}
propertyNodeRelative := &ast.StmtPropertyList{
Type: nameRelative,
}
propertyNodeFullyQualified := &ast.StmtPropertyList{
Type: nameFullyQualified,
}
classNode := &ast.StmtClass{
ClassName: &ast.Identifier{Value: []byte("Bar")},
Stmts: []ast.Vertex{
propertyNodeSimple,
propertyNodeRelative,
propertyNodeFullyQualified,
},
}
stmts := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtNamespace{
NamespaceName: &ast.NameName{
Parts: []ast.Vertex{
&ast.NameNamePart{Value: []byte("Foo")},
},
},
},
classNode,
},
}
expected := map[ast.Vertex]string{
nameSimple: "Foo\\A\\B",
nameRelative: "Foo\\A\\B",
nameFullyQualified: "A\\B",
classNode: "Foo\\Bar",
}
nsResolver := visitor.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver)
dfsTraverser.Traverse(stmts)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
}

View File

@@ -411,11 +411,12 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
// node
func (p *PrettyPrinter) printNodeRoot(n ast.Vertex) {
var stmts []ast.Vertex
v := n.(*ast.Root)
if len(v.Stmts) > 0 {
firstStmt := v.Stmts[0]
v.Stmts = v.Stmts[1:]
stmts = v.Stmts[1:]
switch fs := firstStmt.(type) {
case *ast.StmtInlineHtml:
@@ -429,7 +430,7 @@ func (p *PrettyPrinter) printNodeRoot(n ast.Vertex) {
}
}
p.indentDepth--
p.printNodes(v.Stmts)
p.printNodes(stmts)
io.WriteString(p.w, "\n")
}

View File

@@ -9,10 +9,17 @@ import (
)
func TestPrintFile(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, "\t")
p.Print(&ast.Root{
expected := `<?php
namespace Foo;
abstract class Bar extends Baz
{
public function greet()
{
echo 'Hello world';
}
}
`
rootNode := &ast.Root{
Stmts: []ast.Vertex{
&ast.StmtNamespace{
NamespaceName: &ast.NameName{
@@ -52,22 +59,20 @@ func TestPrintFile(t *testing.T) {
},
},
},
})
expected := `<?php
namespace Foo;
abstract class Bar extends Baz
{
public function greet()
{
echo 'Hello world';
}
}
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
o1 := bytes.NewBufferString("")
p1 := printer.NewPrettyPrinter(o1, "\t")
p1.Print(rootNode)
if actual := o1.String(); expected != actual {
t.Errorf("\nPrint the 1st time\nexpected: %s\ngot: %s\n", expected, actual)
}
o2 := bytes.NewBufferString("")
p2 := printer.NewPrettyPrinter(o2, "\t")
p2.Print(rootNode)
if actual := o2.String(); expected != actual {
t.Errorf("\nPrint the 2nd time\nexpected: %s\ngot: %s\n", expected, actual)
}
}

View File

@@ -2925,7 +2925,7 @@ func TestPrinterPrintStmtClassMethod(t *testing.T) {
MethodName: &ast.Identifier{Value: []byte("foo")},
Params: []ast.Vertex{
&ast.Parameter{
Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}},
Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}},
Var: &ast.Reference{
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},
@@ -2974,7 +2974,7 @@ func TestPrinterPrintStmtAbstractClassMethod(t *testing.T) {
MethodName: &ast.Identifier{Value: []byte("foo")},
Params: []ast.Vertex{
&ast.Parameter{
Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}},
Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}},
Var: &ast.Reference{
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},