Skip to content

Add user supplied error messages in depguard issues #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,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:
Expand Down
7 changes: 4 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion pkg/golinters/depguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
})
}
Expand Down
7 changes: 7 additions & 0 deletions test/testdata/configs/depguard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
linters-settings:
depguard:
include-go-root: true
packages:
- compress/*
packages-with-error-message:
log: "don't use log"
5 changes: 2 additions & 3 deletions test/testdata/depguard.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down