-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspectors.go
More file actions
293 lines (252 loc) · 9.56 KB
/
inspectors.go
File metadata and controls
293 lines (252 loc) · 9.56 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package codescout
import (
"go/ast"
"go/token"
"strings"
"github.com/galactixx/codescout/internal/pkgutils"
)
// inspector is a generic interface for inspecting and collecting AST nodes of type T.
type inspector[T any] interface {
isNodeMatch(name *T) bool
appendNode(node *T)
inspector(n ast.Node) bool
inspect()
getNodes() []*T
}
// baseInspector provides shared utilities for AST traversal and node metadata extraction.
type baseInspector struct {
Path string
Fset *token.FileSet
}
// inspect traverses the AST and applies a list of inspector functions to each node.
func (i baseInspector) inspect(node ast.Node, inspectors []func(n ast.Node) bool) {
ast.Inspect(node, func(n ast.Node) bool {
for _, inspector := range inspectors {
inspector(n)
}
return true
})
}
// newNode constructs a BaseNode with name, position, export status, and comment.
func (i baseInspector) newNode(name string, node ast.Node, comment string) BaseNode {
line, characters := i.getPos(node)
return BaseNode{
Name: name,
Path: i.Path,
Line: line,
Characters: characters,
Exported: token.IsExported(name),
Comment: strings.TrimSpace(comment),
}
}
// getCallableNodes extracts metadata and casts the node to *ast.FuncDecl.
func (i baseInspector) getCallableNodes(name string, node ast.Node, comment string) (BaseNode, *ast.FuncDecl) {
baseNode := i.newNode(name, node, comment)
funcNode := node.(*ast.FuncDecl)
return baseNode, funcNode
}
// getPos returns the line and column position of the given AST node.
func (i baseInspector) getPos(node ast.Node) (int, int) {
pos := i.Fset.Position(node.Pos())
line := pos.Line
characters := pos.Column
return line, characters
}
// structInspector inspects struct declarations and associates their methods.
type structInspector struct {
Nodes map[string]*StructNode
Config StructConfig
Base baseInspector
}
// isNodeMatch determines whether a StructNode matches the struct inspection configuration.
func (i structInspector) isNodeMatch(node *StructNode) bool {
nameEquals := !(i.Config.Name != "" && i.Config.Name != node.Node.Name)
matchFields := astMatch(i.Config.FieldTypes, node.Fields(), i.Config.Exact, i.Config.NoFields, namedTypesMatch)
return nameEquals && matchFields.validate()
}
// appendNode stores a matched StructNode in the inspector's map.
func (i *structInspector) appendNode(node *StructNode) {
i.Nodes[node.Node.Name] = node
}
// inspect performs the struct inspection and attaches discovered methods to their respective structs.
func (i *structInspector) inspect() {
methodsInspect := methodInspector{
Nodes: []*MethodNode{},
Config: MethodConfig{},
Base: i.Base,
}
node := pkgutils.ParseFile(i.Base.Path, i.Base.Fset)
i.Base.inspect(node, []func(n ast.Node) bool{i.inspector, methodsInspect.inspector})
for _, methodNode := range methodsInspect.Nodes {
if structNode, ok := i.Nodes[methodNode.ReceiverType()]; ok {
structNode.Methods = append(structNode.Methods, methodNode)
}
}
}
// getNodes returns a slice of all matched StructNode instances.
func (i *structInspector) getNodes() []*StructNode {
structNodes := make([]*StructNode, 0, len(i.Nodes))
for _, node := range i.Nodes {
structNodes = append(structNodes, node)
}
return structNodes
}
// newStruct constructs a StructNode from its AST components.
func (i structInspector) newStruct(node ast.Node, gen *ast.GenDecl, spec *ast.TypeSpec) *StructNode {
var comment string = ""
if gen.Doc != nil {
comment = gen.Doc.Text()
}
baseNode := i.Base.newNode(spec.Name.Name, node, comment)
structNode := node.(*ast.StructType)
return &StructNode{
Node: baseNode, node: structNode, spec: spec, genNode: gen, fset: i.Base.Fset,
}
}
// inspector checks if the current AST node is a struct declaration, then stores it if matched.
func (i *structInspector) inspector(node ast.Node) bool {
genDecl, ok := node.(*ast.GenDecl)
if !ok {
return true
}
for _, spec := range genDecl.Specs {
if typeSpec, ok := spec.(*ast.TypeSpec); ok {
if structType, ok := typeSpec.Type.(*ast.StructType); ok {
structNode := i.newStruct(structType, genDecl, typeSpec)
if i.isNodeMatch(structNode) {
i.appendNode(structNode)
}
}
}
}
return true
}
// methodInspector inspects methods and captures metadata such as accessed fields and called methods.
type methodInspector struct {
Nodes []*MethodNode
Config MethodConfig
Base baseInspector
}
// isNodeMatch determines whether a MethodNode matches method inspection criteria.
func (i methodInspector) isNodeMatch(node *MethodNode) bool {
nameEquals := !(i.Config.Name != "" && i.Config.Name != node.Node.Name)
matchReturn := astMatch(i.Config.ReturnTypes, node.CallableOps.ReturnTypes(), i.Config.Exact, i.Config.NoReturn, returnMatch)
matchParams := astMatch(i.Config.ParamTypes, node.CallableOps.Parameters(), i.Config.Exact, i.Config.NoParams, namedTypesMatch)
validReceiver := !(i.Config.Receiver != "" && i.Config.Receiver != node.ReceiverType())
validPtr := i.Config.IsPointerRec == nil || *i.Config.IsPointerRec == node.HasPointerReceiver()
return nameEquals && matchReturn.validate() && matchParams.validate() && validReceiver && validPtr
}
// isAttrsMatch validates the fields accessed and methods called by the method node.
func (i methodInspector) isAttrsMatch(node *MethodNode) bool {
matchAccessed := astMatch(i.Config.Fields, node.FieldsAccessed(), i.Config.Exact, i.Config.NoFields, accessedMatch)
matchCalled := astMatch(i.Config.Methods, node.MethodsCalled(), i.Config.Exact, i.Config.NoMethods, accessedMatch)
return matchAccessed.validate() && matchCalled.validate()
}
// appendNode stores a matched MethodNode.
func (i *methodInspector) appendNode(node *MethodNode) { i.Nodes = append(i.Nodes, node) }
// inspect parses and traverses the file to extract method nodes.
func (i *methodInspector) inspect() {
node := pkgutils.ParseFile(i.Base.Path, i.Base.Fset)
i.Base.inspect(node, []func(n ast.Node) bool{i.inspector})
}
// getNodes returns all matched MethodNode instances.
func (i methodInspector) getNodes() []*MethodNode { return i.Nodes }
// newMethod constructs a MethodNode with tracking fields and associated callable operations.
func (i methodInspector) newMethod(name string, node ast.Node, comment string) *MethodNode {
baseNode, funcNode := i.Base.getCallableNodes(name, node, comment)
return &MethodNode{
Node: baseNode,
CallableOps: CallableOps{node: funcNode, fset: i.Base.Fset},
fieldsAccessed: make(map[string]*int),
methodsCalled: make(map[string]*int),
}
}
// inspector traverses AST to identify method declarations and captures method interactions.
func (i *methodInspector) inspector(n ast.Node) bool {
funcDecl, ok := n.(*ast.FuncDecl)
if !ok || funcDecl.Recv == nil {
return true
}
var comment string = ""
if funcDecl.Doc != nil {
comment = funcDecl.Doc.Text()
}
name := funcDecl.Name.Name
methodNode := i.newMethod(name, funcDecl, comment)
receiverName := methodNode.ReceiverName()
if i.isNodeMatch(methodNode) {
var parentStack []ast.Node
ast.Inspect(funcDecl.Body, func(n ast.Node) bool {
if n == nil {
parentStack = parentStack[:len(parentStack)-1]
return true
}
sel, ok := n.(*ast.SelectorExpr)
if ok {
if ident, ok := sel.X.(*ast.Ident); ok && ident.Name == receiverName {
curParent := parentStack[len(parentStack)-1]
if call, isCall := curParent.(*ast.CallExpr); isCall && call.Fun == sel {
methodNode.addMethodCall(sel.Sel.Name)
} else {
methodNode.addMethodField(sel.Sel.Name)
}
}
}
parentStack = append(parentStack, n)
return true
})
if i.isAttrsMatch(methodNode) {
i.appendNode(methodNode)
}
}
return true
}
// funcInspector inspects functions in the Go source.
type funcInspector struct {
Nodes []*FuncNode
Config FuncConfig
Base baseInspector
}
// isNodeMatch determines whether a function matches the criteria defined in FuncConfig.
func (i funcInspector) isNodeMatch(node *FuncNode) bool {
nameEquals := !(i.Config.Name != "" && i.Config.Name != node.Node.Name)
matchReturn := astMatch(i.Config.ReturnTypes, node.CallableOps.ReturnTypes(), i.Config.Exact, i.Config.NoReturn, returnMatch)
matchParams := astMatch(i.Config.ParamTypes, node.CallableOps.Parameters(), i.Config.Exact, i.Config.NoParams, namedTypesMatch)
return nameEquals && matchReturn.validate() && matchParams.validate()
}
// appendNode stores a matched FuncNode.
func (i *funcInspector) appendNode(node *FuncNode) {
i.Nodes = append(i.Nodes, node)
}
// inspect parses and traverses the file to find matching function declarations.
func (i *funcInspector) inspect() {
node := pkgutils.ParseFile(i.Base.Path, i.Base.Fset)
i.Base.inspect(node, []func(n ast.Node) bool{i.inspector})
}
// getNodes returns all matched FuncNode instances.
func (i funcInspector) getNodes() []*FuncNode { return i.Nodes }
// newFunction constructs a FuncNode from the given AST node.
func (i funcInspector) newFunction(name string, node ast.Node, comment string) *FuncNode {
baseNode, funcNode := i.Base.getCallableNodes(name, node, comment)
return &FuncNode{Node: baseNode, CallableOps: CallableOps{node: funcNode, fset: i.Base.Fset}}
}
// inspector visits each AST node and extracts top-level (non-method) functions that match the config.
func (i *funcInspector) inspector(n ast.Node) bool {
funcDecl, ok := n.(*ast.FuncDecl)
if !ok {
return true
}
if funcDecl.Recv != nil {
return true
}
var comment string = ""
if funcDecl.Doc != nil {
comment = funcDecl.Doc.Text()
}
name := funcDecl.Name.Name
funcNode := i.newFunction(name, funcDecl, comment)
if i.isNodeMatch(funcNode) {
i.appendNode(funcNode)
}
return true
}