-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from budougumi0617/add-inspect
add inspect
- Loading branch information
Showing
3 changed files
with
173 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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(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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters