-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinspect.go
144 lines (115 loc) · 2.64 KB
/
inspect.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
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
package goast
import (
"go/ast"
"go/token"
"reflect"
"github.com/m-mizutani/goerr"
)
type InspectOption func(options *inspectOptions)
func newInspectOptions() *inspectOptions {
return &inspectOptions{
Lines: map[int]struct{}{},
FuncNames: map[string]struct{}{},
viewedLine: map[int]struct{}{},
}
}
type inspectOptions struct {
Lines map[int]struct{}
FuncNames map[string]struct{}
ObjectDepth int
AllMatched bool
RootOnly bool
viewedLine map[int]struct{}
}
func (x *inspectOptions) shouldInspect(n ast.Node, fs *token.FileSet) bool {
pos := fs.Position(n.Pos())
if len(x.Lines) > 0 || len(x.FuncNames) > 0 {
if _, ok := x.Lines[pos.Line]; ok {
if _, ok := x.viewedLine[pos.Line]; !x.AllMatched && ok {
return false
}
x.viewedLine[pos.Line] = struct{}{}
return true
}
if f, ok := n.(*ast.FuncDecl); ok {
if _, ok := x.FuncNames[f.Name.Name]; ok {
return true
}
}
return false
}
return true
}
func WithLine(line int) InspectOption {
return func(opt *inspectOptions) {
opt.Lines[line] = struct{}{}
}
}
func WithFuncName(funcName string) InspectOption {
return func(opt *inspectOptions) {
opt.FuncNames[funcName] = struct{}{}
}
}
func WithObjectDepth(depth int) InspectOption {
return func(opt *inspectOptions) {
opt.ObjectDepth = depth
}
}
func WithRootOnly() InspectOption {
return func(opt *inspectOptions) {
opt.RootOnly = true
}
}
func WithAllMatched() InspectOption {
return func(opt *inspectOptions) {
opt.AllMatched = true
}
}
type callback func(data *Node) error
func Inspect(f *ast.File, fSet *token.FileSet, cb callback, options ...InspectOption) error {
option := newInspectOptions()
for _, f := range options {
f(option)
}
ctx := newCloneContext()
ctx.objectDepth = option.ObjectDepth
pos := fSet.Position(f.Pos())
filePath := pos.Filename
if option.RootOnly {
obj := clone(ctx, reflect.ValueOf(f))
output, ok := obj.Interface().(ast.Node)
if !ok {
return goerr.New("consistency error, obj must be *ast.File")
}
node := newNode(filePath, output, fSet)
if err := cb(node); err != nil {
return err
}
} else {
var cbErr error
ast.Inspect(f, func(n ast.Node) bool {
if n == nil {
return true
}
if !option.shouldInspect(n, fSet) {
return true
}
obj := clone(ctx, reflect.ValueOf(n))
output, ok := obj.Interface().(ast.Node)
if !ok {
cbErr = goerr.New("consistency error, obj must be *ast.File")
return false
}
node := newNode(filePath, output, fSet)
if err := cb(node); err != nil {
cbErr = err
return false
}
return true
})
if cbErr != nil {
return cbErr
}
}
return nil
}