Add user supplied error messages in depguard issues (#662)

This commit is contained in:
Cody Ley-Han 2019-09-09 10:46:55 -07:00 committed by Isaev Denis
parent a02e3f573b
commit a8f2c27d1d
6 changed files with 38 additions and 7 deletions

View File

@ -25,6 +25,8 @@ linters-settings:
# logging is allowed only by logutils.Log, logrus # logging is allowed only by logutils.Log, logrus
# is allowed to use only in logutils package # is allowed to use only in logutils package
- github.com/sirupsen/logrus - github.com/sirupsen/logrus
packages-with-error-messages:
github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
misspell: misspell:
locale: US locale: US
lll: lll:

View File

@ -845,6 +845,8 @@ linters-settings:
# logging is allowed only by logutils.Log, logrus # logging is allowed only by logutils.Log, logrus
# is allowed to use only in logutils package # is allowed to use only in logutils package
- github.com/sirupsen/logrus - github.com/sirupsen/logrus
packages-with-error-messages:
github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
misspell: misspell:
locale: US locale: US
lll: lll:

View File

@ -156,6 +156,7 @@ type LintersSettings struct {
ListType string `mapstructure:"list-type"` ListType string `mapstructure:"list-type"`
Packages []string Packages []string
IncludeGoRoot bool `mapstructure:"include-go-root"` IncludeGoRoot bool `mapstructure:"include-go-root"`
PackagesWithErrorMessage map[string]string `mapstructure:"packages-with-error-message"`
} }
Misspell struct { Misspell struct {
Locale string Locale string

View File

@ -36,6 +36,22 @@ func (d Depguard) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Is
dg.ListType = depguardAPI.LTBlacklist dg.ListType = depguardAPI.LTBlacklist
} }
if dg.ListType == depguardAPI.LTBlacklist {
// if the list type was a blacklist the packages with error messages should
// be included in the blacklist package list
noMessagePackages := make(map[string]bool)
for _, pkg := range dg.Packages {
noMessagePackages[pkg] = true
}
for pkg := range lintCtx.Settings().Depguard.PackagesWithErrorMessage {
if _, ok := noMessagePackages[pkg]; !ok {
dg.Packages = append(dg.Packages, pkg)
}
}
}
issues, err := dg.Run(lintCtx.LoaderConfig, lintCtx.Program) issues, err := dg.Run(lintCtx.LoaderConfig, lintCtx.Program)
if err != nil { if err != nil {
return nil, err return nil, err
@ -49,9 +65,13 @@ func (d Depguard) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Is
} }
res := make([]result.Issue, 0, len(issues)) res := make([]result.Issue, 0, len(issues))
for _, i := range issues { for _, i := range issues {
userSuppliedMsgSuffix := lintCtx.Settings().Depguard.PackagesWithErrorMessage[i.PackageName]
if userSuppliedMsgSuffix != "" {
userSuppliedMsgSuffix = ": " + userSuppliedMsgSuffix
}
res = append(res, result.Issue{ res = append(res, result.Issue{
Pos: i.Position, Pos: i.Position,
Text: fmt.Sprintf("%s %s", formatCode(i.PackageName, lintCtx.Cfg), msgSuffix), Text: fmt.Sprintf("%s %s%s", formatCode(i.PackageName, lintCtx.Cfg), msgSuffix, userSuppliedMsgSuffix),
FromLinter: d.Name(), FromLinter: d.Name(),
}) })
} }

7
test/testdata/configs/depguard.yml vendored Normal file
View File

@ -0,0 +1,7 @@
linters-settings:
depguard:
include-go-root: true
packages:
- compress/*
packages-with-error-message:
log: "don't use log"

View File

@ -1,11 +1,10 @@
//args: -Edepguard //args: -Edepguard
//config: linters-settings.depguard.include-go-root=true //config_path: testdata/configs/depguard.yml
//config: linters-settings.depguard.packages=compress/*,log
package testdata package testdata
import ( import (
"compress/gzip" // ERROR "`compress/gzip` is in the blacklist" "compress/gzip" // ERROR "`compress/gzip` is in the blacklist"
"log" // ERROR "`log` is in the blacklist" "log" // ERROR "`log` is in the blacklist: don't use log"
) )
func SpewDebugInfo() { func SpewDebugInfo() {