Skip to content

Commit 5b3192b

Browse files
authored
Merge pull request #88 from GoASTScanner/experimental
Initialize fresh import info for each file
2 parents 6ef59ba + ca42de2 commit 5b3192b

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

core/analyzer.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ type ImportInfo struct {
3434
InitOnly map[string]bool
3535
}
3636

37+
func NewImportInfo() *ImportInfo {
38+
return &ImportInfo{
39+
make(map[string]string),
40+
make(map[string]string),
41+
make(map[string]bool),
42+
}
43+
}
44+
3745
// The Context is populated with data parsed from the source code as it is scanned.
3846
// It is passed through to all rule functions as they are called. Rules may use
3947
// this data in conjunction withe the encoutered AST node.
@@ -92,11 +100,7 @@ func NewAnalyzer(conf map[string]interface{}, logger *log.Logger) Analyzer {
92100
nil,
93101
nil,
94102
nil,
95-
&ImportInfo{
96-
make(map[string]string),
97-
make(map[string]string),
98-
make(map[string]bool),
99-
},
103+
nil,
100104
},
101105
logger: logger,
102106
}
@@ -130,6 +134,7 @@ func (gas *Analyzer) process(filename string, source interface{}) error {
130134
return err
131135
}
132136

137+
gas.context.Imports = NewImportInfo()
133138
for _, pkg := range gas.context.Pkg.Imports() {
134139
gas.context.Imports.Imported[pkg.Path()] = pkg.Name()
135140
}

rules/rand.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package rules
1616

1717
import (
18+
"fmt"
1819
"go/ast"
1920

2021
gas "github.com/GoASTScanner/gas/core"
@@ -28,6 +29,16 @@ type WeakRand struct {
2829

2930
func (w *WeakRand) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
3031
if _, matched := gas.MatchCallByPackage(n, c, w.packagePath, w.funcName); matched {
32+
fmt.Println("Imported:")
33+
for k, v := range c.Imports.Imported {
34+
fmt.Printf("%s => %s\n", k, v)
35+
}
36+
fmt.Println("Aliased:")
37+
for k, v := range c.Imports.Aliased {
38+
fmt.Printf("%s => %s\n", k, v)
39+
}
40+
fmt.Println("----------------------------------------")
41+
3142
return gas.NewIssue(c, n, w.What, w.Severity, w.Confidence), nil
3243
}
3344

0 commit comments

Comments
 (0)