From d3358674bd75995fff87b452304d8761e5fa56a0 Mon Sep 17 00:00:00 2001 From: kkHAIKE Date: Tue, 11 Oct 2022 20:45:56 +0800 Subject: [PATCH] fix 3rd pkgpath filter --- contextcheck.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/contextcheck.go b/contextcheck.go index 2ba7338..c9ad010 100644 --- a/contextcheck.go +++ b/contextcheck.go @@ -97,14 +97,25 @@ type runner struct { disableFact bool } +func getPkgRoot(pkg string) string { + arr := strings.Split(pkg, "/") + if len(arr) < 3 { + return arr[0] + } + if strings.IndexByte(arr[0], '.') == -1 { + return arr[0] + } + return strings.Join(arr[:3], "/") +} + func NewRun(pkgs []*packages.Package, disableFact bool) func(pass *analysis.Pass) (interface{}, error) { m := make(map[string]bool) for _, pkg := range pkgs { - m[strings.Split(pkg.PkgPath, "/")[0]] = true + m[getPkgRoot(pkg.PkgPath)] = true } return func(pass *analysis.Pass) (interface{}, error) { // skip different repo - if len(m) > 0 && !m[strings.Split(pass.Pkg.Path(), "/")[0]] { + if len(m) > 0 && !m[getPkgRoot(pass.Pkg.Path())] { return nil, nil } if len(m) == 0 && pkgprefix != "" && !strings.HasPrefix(pass.Pkg.Path(), pkgprefix) {