File tree Expand file tree Collapse file tree 2 files changed +13
-5
lines changed Expand file tree Collapse file tree 2 files changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -175,7 +175,7 @@ func (gosec *Analyzer) check(pkg *packages.Package) {
175
175
gosec .context .Pkg = pkg .Types
176
176
gosec .context .PkgFiles = pkg .Syntax
177
177
gosec .context .Imports = NewImportTracker ()
178
- gosec .context .Imports .TrackPackages ( gosec . context . Pkg . Imports () ... )
178
+ gosec .context .Imports .TrackFile ( file )
179
179
ast .Walk (gosec , file )
180
180
gosec .stats .NumFiles ++
181
181
gosec .stats .NumLines += pkg .Fset .File (file .Pos ()).LineCount ()
Original file line number Diff line number Diff line change @@ -36,14 +36,22 @@ func NewImportTracker() *ImportTracker {
36
36
}
37
37
}
38
38
39
+ // TrackFile track all the imports used by the supplied file
40
+ func (t * ImportTracker ) TrackFile (file * ast.File ) {
41
+ for _ , imp := range file .Imports {
42
+ path := strings .Trim (imp .Path .Value , `"` )
43
+ parts := strings .Split (path , "/" )
44
+ if len (parts ) > 0 {
45
+ name := parts [len (parts )- 1 ]
46
+ t .Imported [path ] = name
47
+ }
48
+ }
49
+ }
50
+
39
51
// TrackPackages tracks all the imports used by the supplied packages
40
52
func (t * ImportTracker ) TrackPackages (pkgs ... * types.Package ) {
41
53
for _ , pkg := range pkgs {
42
54
t .Imported [pkg .Path ()] = pkg .Name ()
43
- // Transient imports
44
- //for _, imp := range pkg.Imports() {
45
- // t.Imported[imp.Path()] = imp.Name()
46
- //}
47
55
}
48
56
}
49
57
You can’t perform that action at this time.
0 commit comments