Update depguard version to 1.0.0 (Performance improvements)

This commit is contained in:
Hernan Bandura 2019-06-26 17:42:12 -03:00 committed by Isaev Denis
parent 22d1ef65c8
commit f8a5a8cb01
6 changed files with 131 additions and 51 deletions

2
go.mod
View File

@ -2,7 +2,7 @@ module github.com/golangci/golangci-lint
require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/OpenPeeDeeP/depguard v0.0.0-20180806142446-a69c782687b2
github.com/OpenPeeDeeP/depguard v1.0.0
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/fatih/color v1.6.0
github.com/go-critic/go-critic v0.0.0-20181204210945-1df300866540

4
go.sum
View File

@ -1,7 +1,7 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/OpenPeeDeeP/depguard v0.0.0-20180806142446-a69c782687b2 h1:HTOmFEEYrWi4MW5ZKUx6xfeyM10Sx3kQF65xiQJMPYA=
github.com/OpenPeeDeeP/depguard v0.0.0-20180806142446-a69c782687b2/go.mod h1:7/4sitnI9YlQgTLLk734QlzXT8DuHVnAyztLplQjk+o=
github.com/OpenPeeDeeP/depguard v1.0.0 h1:k9QF73nrHT3nPLz3lu6G5s+3Hi8Je36ODr1F5gjAXXM=
github.com/OpenPeeDeeP/depguard v1.0.0/go.mod h1:7/4sitnI9YlQgTLLk734QlzXT8DuHVnAyztLplQjk+o=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

View File

@ -28,6 +28,9 @@ The following is an example configuration file.
"packages": [
"github.com/OpenPeeDeeP/depguard"
],
"inTests": [
"github.com/stretchr/testify"
],
"includeGoRoot": true
}
```
@ -35,6 +38,7 @@ The following is an example configuration file.
- `type` can be either `whitelist` or `blacklist`. This check is case insensitive.
If not specified the default is `blacklist`.
- `packages` is a list of packages for the list type specified.
- `inTests` is a list of packages allowed/disallowed only in test files.
- Set `includeGoRoot` to true if you want to check the list against standard lib.
If not specified the default is false.

View File

@ -1,7 +1,6 @@
package depguard
import (
"go/build"
"go/token"
"os"
"sort"
@ -37,11 +36,17 @@ type Issue struct {
// Depguard checks imports to make sure they follow the given list and constraints.
type Depguard struct {
ListType ListType
Packages []string
IncludeGoRoot bool
Packages []string
prefixPackages []string
globPackages []glob.Glob
buildCtx *build.Context
TestPackages []string
prefixTestPackages []string
globTestPackages []glob.Glob
rootChecker *RootChecker
cwd string
}
@ -56,15 +61,20 @@ func (dg *Depguard) Run(config *loader.Config, prog *loader.Program) ([]*Issue,
if err := dg.initialize(config, prog); err != nil {
return nil, err
}
directImports, err := dg.createImportMap(prog)
if err != nil {
return nil, err
}
var issues []*Issue
for pkg, positions := range directImports {
if dg.flagIt(pkg) {
for _, pos := range positions {
prefixList, globList := dg.prefixPackages, dg.globPackages
if len(dg.TestPackages) > 0 && strings.Index(pos.Filename, "_test.go") != -1 {
prefixList, globList = dg.prefixTestPackages, dg.globTestPackages
}
if dg.flagIt(pkg, prefixList, globList) {
issues = append(issues, &Issue{
PackageName: pkg,
Position: pos,
@ -86,12 +96,9 @@ func (dg *Depguard) initialize(config *loader.Config, prog *loader.Program) erro
}
}
//Use the &build.Default if one is not specified
dg.buildCtx = config.Build
if dg.buildCtx == nil {
dg.buildCtx = &build.Default
}
dg.rootChecker = NewRootChecker(config.Build)
// parse ordinary guarded packages
for _, pkg := range dg.Packages {
if strings.ContainsAny(pkg, "!?*[]{}") {
g, err := glob.Compile(pkg, '/')
@ -106,6 +113,23 @@ func (dg *Depguard) initialize(config *loader.Config, prog *loader.Program) erro
// Sort the packages so we can have a faster search in the array
sort.Strings(dg.prefixPackages)
// parse guarded tests packages
for _, pkg := range dg.TestPackages {
if strings.ContainsAny(pkg, "!?*[]{}") {
g, err := glob.Compile(pkg, '/')
if err != nil {
return err
}
dg.globTestPackages = append(dg.globTestPackages, g)
} else {
dg.prefixTestPackages = append(dg.prefixTestPackages, pkg)
}
}
// Sort the test packages so we can have a faster search in the array
sort.Strings(dg.prefixTestPackages)
return nil
}
@ -120,11 +144,11 @@ func (dg *Depguard) createImportMap(prog *loader.Program) (map[string][]token.Po
for _, fileImport := range file.Imports {
fileImportPath := cleanBasicLitString(fileImport.Path.Value)
if !dg.IncludeGoRoot {
pkg, err := dg.buildCtx.Import(fileImportPath, dg.cwd, 0)
isRoot, err := dg.rootChecker.IsRoot(fileImportPath, dg.cwd)
if err != nil {
return nil, err
}
if pkg.Goroot {
if isRoot {
continue
}
}
@ -143,29 +167,29 @@ func (dg *Depguard) createImportMap(prog *loader.Program) (map[string][]token.Po
return importMap, nil
}
func (dg *Depguard) pkgInList(pkg string) bool {
if dg.pkgInPrefixList(pkg) {
func (dg *Depguard) pkgInList(pkg string, prefixList []string, globList []glob.Glob) bool {
if dg.pkgInPrefixList(pkg, prefixList) {
return true
}
return dg.pkgInGlobList(pkg)
return dg.pkgInGlobList(pkg, globList)
}
func (dg *Depguard) pkgInPrefixList(pkg string) bool {
func (dg *Depguard) pkgInPrefixList(pkg string, prefixList []string) bool {
// Idx represents where in the package slice the passed in package would go
// when sorted. -1 Just means that it would be at the very front of the slice.
idx := sort.Search(len(dg.prefixPackages), func(i int) bool {
return dg.prefixPackages[i] > pkg
idx := sort.Search(len(prefixList), func(i int) bool {
return prefixList[i] > pkg
}) - 1
// This means that the package passed in has no way to be prefixed by anything
// in the package list as it is already smaller then everything
if idx == -1 {
return false
}
return strings.HasPrefix(pkg, dg.prefixPackages[idx])
return strings.HasPrefix(pkg, prefixList[idx])
}
func (dg *Depguard) pkgInGlobList(pkg string) bool {
for _, g := range dg.globPackages {
func (dg *Depguard) pkgInGlobList(pkg string, globList []glob.Glob) bool {
for _, g := range globList {
if g.Match(pkg) {
return true
}
@ -176,8 +200,8 @@ func (dg *Depguard) pkgInGlobList(pkg string) bool {
// InList | WhiteList | BlackList
// y | | x
// n | x |
func (dg *Depguard) flagIt(pkg string) bool {
return dg.pkgInList(pkg) == (dg.ListType == LTBlacklist)
func (dg *Depguard) flagIt(pkg string, prefixList []string, globList []glob.Glob) bool {
return dg.pkgInList(pkg, prefixList, globList) == (dg.ListType == LTBlacklist)
}
func cleanBasicLitString(value string) string {

52
vendor/github.com/OpenPeeDeeP/depguard/rootchecker.go generated vendored Normal file
View File

@ -0,0 +1,52 @@
package depguard
import (
"go/build"
)
// RootChecker checks if import paths point to root packages.
type RootChecker struct {
buildCtx *build.Context
cache map[string]bool
}
// NewRootChecker creates a new RootChecker instance using the build.Context
// given, or build.Default.
func NewRootChecker(buildCtx *build.Context) *RootChecker {
// Use the &build.Default if build.Context is not specified
ctx := buildCtx
if ctx == nil {
ctx = &build.Default
}
return &RootChecker{
buildCtx: ctx,
cache: make(map[string]bool, 0),
}
}
// IsRoot checks if the given import path (imported from sourceDir)
// points to a a root package. Subsequent calls with the same arguments
// are cached. This is not thread-safe.
func (rc *RootChecker) IsRoot(path, sourceDir string) (bool, error) {
key := path + ":::" + sourceDir
isRoot, ok := rc.cache[key]
if ok {
return isRoot, nil
}
isRoot, err := rc.calcIsRoot(path, sourceDir)
if err != nil {
return false, err
}
rc.cache[key] = isRoot
return isRoot, nil
}
// calcIsRoot performs the call to the build context to check if
// the import path points to a root package.
func (rc *RootChecker) calcIsRoot(path, sourceDir string) (bool, error) {
pkg, err := rc.buildCtx.Import(path, sourceDir, build.FindOnly)
if err != nil {
return false, err
}
return pkg.Goroot, nil
}

2
vendor/modules.txt vendored
View File

@ -1,6 +1,6 @@
# github.com/BurntSushi/toml v0.3.1
github.com/BurntSushi/toml
# github.com/OpenPeeDeeP/depguard v0.0.0-20180806142446-a69c782687b2
# github.com/OpenPeeDeeP/depguard v1.0.0
github.com/OpenPeeDeeP/depguard
# github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6
github.com/StackExchange/wmi