-
Notifications
You must be signed in to change notification settings - Fork 6
/
inspect.go
50 lines (46 loc) · 1.03 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
package nrseg
import (
"errors"
"go/ast"
"go/parser"
"go/token"
)
func (nrseg *nrseg) Inspect(filename string, src []byte) error {
if len(src) != 0 && c.Match(src) {
return nil
}
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, filename, src, parser.ParseComments)
if err != nil {
return err
}
// import newrelic pkg
pkg := "newrelic"
name, err := findImport(fs, f) // importされたpkgの名前
if err != nil && !errors.Is(err, ErrNoImportNrPkg) {
return err
}
if len(name) != 0 {
// change name if named import.
pkg = name
}
ast.Inspect(f, func(n ast.Node) bool {
if fd, ok := n.(*ast.FuncDecl); ok {
if findIgnoreComment(fd.Doc) {
return false
}
if fd.Body != nil && len(fd.Body.List) > 0 {
if _, t := parseParams(f.Imports, fd.Type); !(t == TypeContext || t == TypeHttpRequest) {
return false
}
if !existFromContext(pkg, fd.Body.List[0]) {
nrseg.errFlag = true
nrseg.reportf(filename, fs, fd.Pos(), fd)
}
return false
}
}
return true
})
return nil
}