php8: resolve attributes in NamespaceResolver

This commit is contained in:
Tyler Christensen 2023-04-24 13:10:37 -06:00 committed by Laytan Laats
parent 6d1eee5a79
commit 5baa28bb27
2 changed files with 32 additions and 0 deletions

View File

@ -222,6 +222,10 @@ func (nsr *NamespaceResolver) StmtTraitUse(n *ast.StmtTraitUse) {
} }
} }
func (nsr *NamespaceResolver) Attribute(n *ast.Attribute) {
nsr.ResolveName(n.Name, "")
}
// LeaveNode is invoked after node process // LeaveNode is invoked after node process
func (nsr *NamespaceResolver) LeaveNode(n ast.Vertex) { func (nsr *NamespaceResolver) LeaveNode(n ast.Vertex) {
switch nn := n.(type) { switch nn := n.(type) {

View File

@ -385,6 +385,7 @@ func TestResolveTraitUse(t *testing.T) {
func TestResolveClassName(t *testing.T) { func TestResolveClassName(t *testing.T) {
nameAB := &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("A")}, &ast.NamePart{Value: []byte("B")}}} nameAB := &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("A")}, &ast.NamePart{Value: []byte("B")}}}
nameBC := &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("B")}, &ast.NamePart{Value: []byte("C")}}} nameBC := &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("B")}, &ast.NamePart{Value: []byte("C")}}}
nameCD := &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("C")}, &ast.NamePart{Value: []byte("D")}}}
class := &ast.StmtClass{ class := &ast.StmtClass{
Name: &ast.Identifier{Value: []byte("A")}, Name: &ast.Identifier{Value: []byte("A")},
@ -392,6 +393,13 @@ func TestResolveClassName(t *testing.T) {
Implements: []ast.Vertex{ Implements: []ast.Vertex{
nameBC, nameBC,
}, },
AttrGroups: []ast.Vertex{
&ast.AttributeGroup{
Attrs: []ast.Vertex{
&ast.Attribute{Name: nameCD},
},
},
},
} }
stxTree := &ast.StmtStmtList{ stxTree := &ast.StmtStmtList{
@ -404,6 +412,7 @@ func TestResolveClassName(t *testing.T) {
class: "A", class: "A",
nameAB: "A\\B", nameAB: "A\\B",
nameBC: "B\\C", nameBC: "B\\C",
nameCD: "C\\D",
} }
nsResolver := nsresolver.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
@ -629,6 +638,19 @@ func TestResolveNamespaces(t *testing.T) {
nameFG := &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("F")}, &ast.NamePart{Value: []byte("G")}}} nameFG := &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("F")}, &ast.NamePart{Value: []byte("G")}}}
relativeNameCE := &ast.NameRelative{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("C")}, &ast.NamePart{Value: []byte("E")}}} relativeNameCE := &ast.NameRelative{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("C")}, &ast.NamePart{Value: []byte("E")}}}
relativeNameCA := &ast.NameRelative{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("C")}, &ast.NamePart{Value: []byte("A")}}}
classA := &ast.StmtClass{
Name: &ast.Identifier{Value: []byte("A")},
AttrGroups: []ast.Vertex{
&ast.AttributeGroup{
Attrs: []ast.Vertex{
&ast.Attribute{Name: relativeNameCA},
},
},
},
}
constantB := &ast.StmtConstant{ constantB := &ast.StmtConstant{
Name: &ast.Identifier{Value: []byte("B")}, Name: &ast.Identifier{Value: []byte("B")},
Expr: &ast.ScalarLnumber{Value: []byte("1")}, Expr: &ast.ScalarLnumber{Value: []byte("1")},
@ -643,6 +665,9 @@ func TestResolveNamespaces(t *testing.T) {
&ast.StmtNamespace{ &ast.StmtNamespace{
Name: namespaceAB, Name: namespaceAB,
}, },
classA,
&ast.StmtConstList{ &ast.StmtConstList{
Consts: []ast.Vertex{ Consts: []ast.Vertex{
constantB, constantB,
@ -653,6 +678,7 @@ func TestResolveNamespaces(t *testing.T) {
Class: nameFG, Class: nameFG,
Call: &ast.Identifier{Value: []byte("foo")}, Call: &ast.Identifier{Value: []byte("foo")},
}, },
&ast.StmtNamespace{ &ast.StmtNamespace{
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
@ -680,6 +706,8 @@ func TestResolveNamespaces(t *testing.T) {
} }
expected := map[ast.Vertex]string{ expected := map[ast.Vertex]string{
classA: "A\\B\\A",
relativeNameCA: "A\\B\\C\\A",
constantB: "A\\B\\B", constantB: "A\\B\\B",
constantC: "A\\B\\C", constantC: "A\\B\\C",
nameFG: "A\\B\\F\\G", nameFG: "A\\B\\F\\G",