collect aliases
This commit is contained in:
parent
f2846dd859
commit
b9f158e7f4
@ -2,22 +2,50 @@
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/z7zmey/php-parser/node"
|
||||
"github.com/z7zmey/php-parser/node/name"
|
||||
"github.com/z7zmey/php-parser/node/stmt"
|
||||
"github.com/z7zmey/php-parser/walker"
|
||||
)
|
||||
|
||||
type Namespace struct {
|
||||
Namespace string
|
||||
Aliases map[string]map[string]string
|
||||
}
|
||||
|
||||
func NewNamespace(NSName string) *Namespace {
|
||||
return &Namespace{
|
||||
Namespace: "",
|
||||
Aliases: map[string]map[string]string{
|
||||
"": {},
|
||||
"const": {},
|
||||
"function": {},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (ns *Namespace) AddAlias(aliasType string, aliasName string, alias string) {
|
||||
aliasType = strings.ToLower(aliasType)
|
||||
|
||||
if aliasType == "const" {
|
||||
ns.Aliases[aliasType][alias] = aliasName
|
||||
} else {
|
||||
ns.Aliases[aliasType][strings.ToLower(alias)] = aliasName
|
||||
}
|
||||
}
|
||||
|
||||
type NsResolver struct {
|
||||
Namespace string
|
||||
Namespace *Namespace
|
||||
ResolvedNames map[node.Node]string
|
||||
Aliases map[string]map[string]string
|
||||
}
|
||||
|
||||
// NewNsResolver NsResolver type constructor
|
||||
func NewNsResolver() *NsResolver {
|
||||
return &NsResolver{
|
||||
Namespace: "",
|
||||
Namespace: NewNamespace(""),
|
||||
ResolvedNames: map[node.Node]string{},
|
||||
}
|
||||
}
|
||||
@ -26,9 +54,60 @@ func NewNsResolver() *NsResolver {
|
||||
func (nsr *NsResolver) EnterNode(w walker.Walkable) bool {
|
||||
switch n := w.(type) {
|
||||
case *stmt.Namespace:
|
||||
nsr.Namespace = concatNameParts(n.NamespaceName.(*name.Name).Parts)
|
||||
case *stmt.Use:
|
||||
if n.NamespaceName == nil {
|
||||
nsr.Namespace = NewNamespace("")
|
||||
} else {
|
||||
NSParts := n.NamespaceName.(*name.Name).Parts
|
||||
nsr.Namespace = NewNamespace(concatNameParts(NSParts))
|
||||
}
|
||||
case *stmt.UseList:
|
||||
useType := ""
|
||||
if n.UseType != nil {
|
||||
useType = n.UseType.(*node.Identifier).Value
|
||||
}
|
||||
for _, nn := range n.Uses {
|
||||
switch use := nn.(type) {
|
||||
case *stmt.Use:
|
||||
if use.UseType != nil {
|
||||
useType = use.UseType.(*node.Identifier).Value
|
||||
}
|
||||
|
||||
useNameParts := use.Use.(*name.Name).Parts
|
||||
var alias string
|
||||
if use.Alias == nil {
|
||||
alias = useNameParts[len(useNameParts)-1].(*name.NamePart).Value
|
||||
} else {
|
||||
alias = use.Alias.(*node.Identifier).Value
|
||||
}
|
||||
|
||||
nsr.Namespace.AddAlias(useType, concatNameParts(useNameParts), alias)
|
||||
}
|
||||
}
|
||||
case *stmt.GroupUse:
|
||||
useType := ""
|
||||
if n.UseType != nil {
|
||||
useType = n.UseType.(*node.Identifier).Value
|
||||
}
|
||||
for _, nn := range n.UseList {
|
||||
switch use := nn.(type) {
|
||||
case *stmt.Use:
|
||||
if use.UseType != nil {
|
||||
useType = use.UseType.(*node.Identifier).Value
|
||||
}
|
||||
|
||||
useNameParts := use.Use.(*name.Name).Parts
|
||||
var alias string
|
||||
if use.Alias == nil {
|
||||
alias = useNameParts[len(useNameParts)-1].(*name.NamePart).Value
|
||||
} else {
|
||||
alias = use.Alias.(*node.Identifier).Value
|
||||
}
|
||||
|
||||
aliasName := concatNameParts(n.Prefix.(*name.Name).Parts, useNameParts)
|
||||
nsr.Namespace.AddAlias(useType, aliasName, alias)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
@ -44,19 +123,22 @@ func (nsr *NsResolver) LeaveNode(w walker.Walkable) {
|
||||
switch n := w.(type) {
|
||||
case *stmt.Namespace:
|
||||
if n.Stmts != nil {
|
||||
nsr.Namespace = ""
|
||||
fmt.Printf("%+v \n", nsr.Namespace.Aliases)
|
||||
nsr.Namespace = NewNamespace("")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func concatNameParts(parts []node.Node) string {
|
||||
func concatNameParts(parts ...[]node.Node) string {
|
||||
str := ""
|
||||
|
||||
for _, n := range parts {
|
||||
if str == "" {
|
||||
str = n.(*name.NamePart).Value
|
||||
} else {
|
||||
str = str + "\\" + n.(*name.NamePart).Value
|
||||
for _, p := range parts {
|
||||
for _, n := range p {
|
||||
if str == "" {
|
||||
str = n.(*name.NamePart).Value
|
||||
} else {
|
||||
str = str + "\\" + n.(*name.NamePart).Value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user