Add user supplied error messages in depguard issues (#662)
This commit is contained in:
parent
a02e3f573b
commit
a8f2c27d1d
@ -25,6 +25,8 @@ linters-settings:
|
||||
# logging is allowed only by logutils.Log, logrus
|
||||
# is allowed to use only in logutils package
|
||||
- github.com/sirupsen/logrus
|
||||
packages-with-error-messages:
|
||||
github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
|
||||
misspell:
|
||||
locale: US
|
||||
lll:
|
||||
|
@ -845,6 +845,8 @@ linters-settings:
|
||||
# logging is allowed only by logutils.Log, logrus
|
||||
# is allowed to use only in logutils package
|
||||
- github.com/sirupsen/logrus
|
||||
packages-with-error-messages:
|
||||
github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
|
||||
misspell:
|
||||
locale: US
|
||||
lll:
|
||||
|
@ -153,9 +153,10 @@ type LintersSettings struct {
|
||||
MinOccurrencesCount int `mapstructure:"min-occurrences"`
|
||||
}
|
||||
Depguard struct {
|
||||
ListType string `mapstructure:"list-type"`
|
||||
Packages []string
|
||||
IncludeGoRoot bool `mapstructure:"include-go-root"`
|
||||
ListType string `mapstructure:"list-type"`
|
||||
Packages []string
|
||||
IncludeGoRoot bool `mapstructure:"include-go-root"`
|
||||
PackagesWithErrorMessage map[string]string `mapstructure:"packages-with-error-message"`
|
||||
}
|
||||
Misspell struct {
|
||||
Locale string
|
||||
|
@ -36,6 +36,22 @@ func (d Depguard) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Is
|
||||
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)
|
||||
if err != nil {
|
||||
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))
|
||||
for _, i := range issues {
|
||||
userSuppliedMsgSuffix := lintCtx.Settings().Depguard.PackagesWithErrorMessage[i.PackageName]
|
||||
if userSuppliedMsgSuffix != "" {
|
||||
userSuppliedMsgSuffix = ": " + userSuppliedMsgSuffix
|
||||
}
|
||||
res = append(res, result.Issue{
|
||||
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(),
|
||||
})
|
||||
}
|
||||
|
7
test/testdata/configs/depguard.yml
vendored
Normal file
7
test/testdata/configs/depguard.yml
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
linters-settings:
|
||||
depguard:
|
||||
include-go-root: true
|
||||
packages:
|
||||
- compress/*
|
||||
packages-with-error-message:
|
||||
log: "don't use log"
|
5
test/testdata/depguard.go
vendored
5
test/testdata/depguard.go
vendored
@ -1,11 +1,10 @@
|
||||
//args: -Edepguard
|
||||
//config: linters-settings.depguard.include-go-root=true
|
||||
//config: linters-settings.depguard.packages=compress/*,log
|
||||
//config_path: testdata/configs/depguard.yml
|
||||
package testdata
|
||||
|
||||
import (
|
||||
"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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user