Skip to content

Commit 5ef2bee

Browse files
ccojocarCosmin Cojocar
authored andcommitted
Track only the import from the file which is checked
Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent f1ea7f6 commit 5ef2bee

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

analyzer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (gosec *Analyzer) check(pkg *packages.Package) {
175175
gosec.context.Pkg = pkg.Types
176176
gosec.context.PkgFiles = pkg.Syntax
177177
gosec.context.Imports = NewImportTracker()
178-
gosec.context.Imports.TrackPackages(gosec.context.Pkg.Imports()...)
178+
gosec.context.Imports.TrackFile(file)
179179
ast.Walk(gosec, file)
180180
gosec.stats.NumFiles++
181181
gosec.stats.NumLines += pkg.Fset.File(file.Pos()).LineCount()

import_tracker.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,22 @@ func NewImportTracker() *ImportTracker {
3636
}
3737
}
3838

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+
3951
// TrackPackages tracks all the imports used by the supplied packages
4052
func (t *ImportTracker) TrackPackages(pkgs ...*types.Package) {
4153
for _, pkg := range pkgs {
4254
t.Imported[pkg.Path()] = pkg.Name()
43-
// Transient imports
44-
//for _, imp := range pkg.Imports() {
45-
// t.Imported[imp.Path()] = imp.Name()
46-
//}
4755
}
4856
}
4957

0 commit comments

Comments
 (0)