-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathident.go
107 lines (93 loc) · 2.09 KB
/
ident.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package astrav
import (
"go/ast"
)
// Identifier defines a node element with an ident attached to it or ident itself
type Identifier interface {
GetIdent() *Ident
}
// GetIdent returns the name of the node
func (s *FuncDecl) GetIdent() *Ident {
if s.Name == nil {
return nil
}
return s.findChildByAstNode(s.Name).(*Ident)
}
// GetIdent returns the name of the node
func (s *LabeledStmt) GetIdent() *Ident {
if s.Label == nil {
return nil
}
return s.findChildByAstNode(s.Label).(*Ident)
}
// GetIdent returns the name of the node
func (s *BranchStmt) GetIdent() *Ident {
if s.Label == nil {
return nil
}
return s.findChildByAstNode(s.Label).(*Ident)
}
// GetIdent returns the name of the node
func (s *ImportSpec) GetIdent() *Ident {
if s.Name == nil {
return nil
}
return s.findChildByAstNode(s.Name).(*Ident)
}
// GetIdent returns the name of the node
func (s *File) GetIdent() *Ident {
if s.Name == nil {
return nil
}
return s.findChildByAstNode(s.Name).(*Ident)
}
// GetIdent returns the name of the node
func (s *SelectorExpr) GetIdent() *Ident {
if s.Sel == nil {
return nil
}
return s.findChildByAstNode(s.Sel).(*Ident)
}
// GetIdent returns the name of the node
func (s *TypeSpec) GetIdent() *Ident {
if s.Name == nil {
return nil
}
return s.findChildByAstNode(s.Name).(*Ident)
}
// GetIdent returns the name of the node
func (s *CallExpr) GetIdent() *Ident {
if s.Fun == nil {
return nil
}
switch t := s.Fun.(type) {
case *ast.Ident:
return s.findChildByAstNode(t).(*Ident)
case *ast.ArrayType:
arrType := s.findChildByAstNode(t).(*ArrayType)
return arrType.GetIdent()
}
return nil
}
// GetIdent returns the name of the node
func (s *Ident) GetIdent() *Ident {
return s
}
// GetIdent returns the name of the node
func (s *Field) GetIdent() *Ident {
if len(s.Names) == 0 {
return nil
}
return s.findChildByAstNode(s.Names[0]).(*Ident)
}
// GetIdent returns the name of the node
func (s *ArrayType) GetIdent() *Ident {
if s.Elt == nil {
return nil
}
ident, ok := s.Elt.(*ast.Ident)
if !ok {
return nil
}
return s.findChildByAstNode(ident).(*Ident)
}