Merge pull request #88 from GoASTScanner/experimental

Initialize fresh import info for each file
This commit is contained in:
Grant Murphy 2016-11-15 12:01:53 -08:00 committed by GitHub
commit 5b3192b656
2 changed files with 21 additions and 5 deletions

View file

@ -34,6 +34,14 @@ type ImportInfo struct {
InitOnly map[string]bool InitOnly map[string]bool
} }
func NewImportInfo() *ImportInfo {
return &ImportInfo{
make(map[string]string),
make(map[string]string),
make(map[string]bool),
}
}
// The Context is populated with data parsed from the source code as it is scanned. // The Context is populated with data parsed from the source code as it is scanned.
// It is passed through to all rule functions as they are called. Rules may use // It is passed through to all rule functions as they are called. Rules may use
// this data in conjunction withe the encoutered AST node. // this data in conjunction withe the encoutered AST node.
@ -92,11 +100,7 @@ func NewAnalyzer(conf map[string]interface{}, logger *log.Logger) Analyzer {
nil, nil,
nil, nil,
nil, nil,
&ImportInfo{ nil,
make(map[string]string),
make(map[string]string),
make(map[string]bool),
},
}, },
logger: logger, logger: logger,
} }
@ -130,6 +134,7 @@ func (gas *Analyzer) process(filename string, source interface{}) error {
return err return err
} }
gas.context.Imports = NewImportInfo()
for _, pkg := range gas.context.Pkg.Imports() { for _, pkg := range gas.context.Pkg.Imports() {
gas.context.Imports.Imported[pkg.Path()] = pkg.Name() gas.context.Imports.Imported[pkg.Path()] = pkg.Name()
} }

View file

@ -15,6 +15,7 @@
package rules package rules
import ( import (
"fmt"
"go/ast" "go/ast"
gas "github.com/GoASTScanner/gas/core" gas "github.com/GoASTScanner/gas/core"
@ -28,6 +29,16 @@ type WeakRand struct {
func (w *WeakRand) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) { func (w *WeakRand) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
if _, matched := gas.MatchCallByPackage(n, c, w.packagePath, w.funcName); matched { if _, matched := gas.MatchCallByPackage(n, c, w.packagePath, w.funcName); matched {
fmt.Println("Imported:")
for k, v := range c.Imports.Imported {
fmt.Printf("%s => %s\n", k, v)
}
fmt.Println("Aliased:")
for k, v := range c.Imports.Aliased {
fmt.Printf("%s => %s\n", k, v)
}
fmt.Println("----------------------------------------")
return gas.NewIssue(c, n, w.What, w.Severity, w.Confidence), nil return gas.NewIssue(c, n, w.What, w.Severity, w.Confidence), nil
} }