Skip to content

Commit 2f333be

Browse files
author
golangci
authored
Merge pull request #47 from dixonwille/feature/add-depguard
Add Depguard to supported linters
2 parents 9c048da + d46a589 commit 2f333be

File tree

18 files changed

+1086
-4
lines changed

18 files changed

+1086
-4
lines changed

.golangci.example.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ linters-settings:
3131
goconst:
3232
min-len: 3
3333
min-occurrences: 3
34+
depguard:
35+
list-type: blacklist
36+
include-go-root: false
37+
packages:
38+
- github.com/davecgh/go-spew/spew
3439

3540
linters:
3641
enable:

Gopkg.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,7 @@
112112
[[override]]
113113
branch = "master"
114114
name = "github.com/golangci/lint"
115+
116+
[[constraint]]
117+
branch = "master"
118+
name = "github.com/OpenPeeDeeP/depguard"

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ gofmt: Gofmt checks whether code was gofmt-ed. By default this tool runs with -s
104104
goimports: Goimports does everything that gofmt does. Additionally it checks unused imports
105105
maligned: Tool to detect Go structs that would take less memory if their fields were sorted
106106
megacheck: 3 sub-linters in one: unused, gosimple and staticcheck
107+
depguard: Go linter that checks if package imports are in a list of acceptable packages
107108
```
108109

109110
Pass `-E/--enable` to enable linter and `-D/--disable` to disable:
@@ -196,6 +197,7 @@ golangci-lint linters
196197
- [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports): Goimports does everything that gofmt does. Additionally it checks unused imports
197198
- [maligned](https://github.com/mdempsky/maligned): Tool to detect Go structs that would take less memory if their fields were sorted
198199
- [megacheck](https://github.com/dominikh/go-tools/tree/master/cmd/megacheck): 3 sub-linters in one: unused, gosimple and staticcheck
200+
- [depguard](https://github.com/OpenPeeDeeP/depguard): Go linter that checks if package imports are in a list of acceptable packages
199201

200202
# Configuration
201203
## Command-Line Options
@@ -231,7 +233,7 @@ Linters presets:
231233
bugs: govet, errcheck, staticcheck, gas, megacheck
232234
unused: unused, structcheck, varcheck, ineffassign, deadcode, megacheck
233235
format: gofmt, goimports
234-
style: golint, gosimple, interfacer, unconvert, dupl, goconst, megacheck
236+
style: golint, gosimple, interfacer, unconvert, dupl, goconst, megacheck, depguard
235237
complexity: gocyclo
236238
performance: maligned
237239
```
@@ -427,6 +429,7 @@ Thanks to developers and authors of used linters:
427429
- [golang/x/tools/goimports](https://godoc.org/golang.org/x/tools/cmd/goimports)
428430
- [mdempsky/maligned](https://github.com/mdempsky/maligned)
429431
- [dominikh/go-tools/megacheck](https://github.com/dominikh/go-tools/tree/master/cmd/megacheck)
432+
- [OpenPeeDeeP/depguard](https://github.com/OpenPeeDeeP/depguard)
430433

431434
# Future Plans
432435
1. Upstream all changes of forked linters.

pkg/commands/run.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ func (e *Executor) initRun() {
8888
runCmd.Flags().IntVar(&lsc.Goconst.MinOccurrencesCount, "goconst.min-occurrences",
8989
3, "Goconst: minimum occurrences of constant string count to trigger issue")
9090

91+
// (@dixonwille) These flag is only used for testing purposes.
92+
runCmd.Flags().StringSliceVar(&lsc.Depguard.Packages, "depguard.packages", nil,
93+
"Depguard: packages to add to the list")
94+
if err := runCmd.Flags().MarkHidden("depguard.packages"); err != nil {
95+
panic(err) //Considering The only time this is called is if name does not exist
96+
}
97+
runCmd.Flags().BoolVar(&lsc.Depguard.IncludeGoRoot, "depguard.include-go-root", false,
98+
"Depguard: check list against standard lib")
99+
if err := runCmd.Flags().MarkHidden("depguard.include-go-root"); err != nil {
100+
panic(err) //Considering The only time this is called is if name does not exist
101+
}
102+
91103
// Linters config
92104
lc := &e.cfg.Linters
93105
runCmd.Flags().StringSliceVarP(&lc.Enable, "enable", "E", []string{}, "Enable specific linter")

pkg/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ type LintersSettings struct {
9191
MinStringLen int `mapstructure:"min-len"`
9292
MinOccurrencesCount int `mapstructure:"min-occurrences"`
9393
}
94+
Depguard struct {
95+
ListType string `mapstructure:"list-type"`
96+
Packages []string
97+
IncludeGoRoot bool `mapstructure:"inlude-go-root"`
98+
}
9499
}
95100

96101
type Linters struct {

pkg/enabled_linters.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func GetAllSupportedLinterConfigs() []LinterConfig {
125125
newLinterConfig(golinters.Maligned{}).WithFullImport().WithPresets(PresetPerformance).WithSpeed(10),
126126
newLinterConfig(golinters.Megacheck{GosimpleEnabled: true, UnusedEnabled: true, StaticcheckEnabled: true}).
127127
WithSSA().WithPresets(PresetStyle, PresetBugs, PresetUnused).WithSpeed(1),
128+
newLinterConfig(golinters.Depguard{}).WithFullImport().WithPresets(PresetStyle).WithSpeed(6),
128129
}
129130

130131
if os.Getenv("GOLANGCI_COM_RUN") == "1" {

pkg/enabled_linters_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ func testOneSource(t *testing.T, sourcePath string) {
100100
"--out-format=line-number",
101101
"--print-welcome=false",
102102
"--govet.check-shadowing=true",
103+
"--depguard.include-go-root",
104+
"--depguard.packages='log'",
103105
sourcePath)
104106
runGoErrchk(cmd, t)
105107
}

pkg/golinters/depguard.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package golinters
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
depguardAPI "github.com/OpenPeeDeeP/depguard"
9+
"github.com/golangci/golangci-lint/pkg/result"
10+
)
11+
12+
type Depguard struct{}
13+
14+
func (Depguard) Name() string {
15+
return "depguard"
16+
}
17+
18+
func (Depguard) Desc() string {
19+
return "Go linter that checks if package imports are in a list of acceptable packages"
20+
}
21+
22+
func (d Depguard) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) {
23+
dg := &depguardAPI.Depguard{
24+
Packages: lintCtx.Settings().Depguard.Packages,
25+
IncludeGoRoot: lintCtx.Settings().Depguard.IncludeGoRoot,
26+
}
27+
listType := lintCtx.Settings().Depguard.ListType
28+
var found bool
29+
dg.ListType, found = depguardAPI.StringToListType[strings.ToLower(listType)]
30+
if !found {
31+
if listType != "" {
32+
return nil, fmt.Errorf("unsure what list type %s is", listType)
33+
}
34+
dg.ListType = depguardAPI.LTBlacklist
35+
}
36+
37+
issues, err := dg.Run(lintCtx.LoaderConfig, lintCtx.Program)
38+
if err != nil {
39+
return nil, err
40+
}
41+
if len(issues) == 0 {
42+
return nil, nil
43+
}
44+
msgSuffix := "is in the blacklist"
45+
if dg.ListType == depguardAPI.LTWhitelist {
46+
msgSuffix = "is not in the whitelist"
47+
}
48+
res := make([]result.Issue, 0, len(issues))
49+
for _, i := range issues {
50+
res = append(res, result.Issue{
51+
Pos: i.Position,
52+
Text: fmt.Sprintf("%s %s", formatCode(i.PackageName, lintCtx.Cfg), msgSuffix),
53+
FromLinter: d.Name(),
54+
})
55+
}
56+
return res, nil
57+
}

pkg/testdata/with_issues/depguard.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package testdata
2+
3+
import (
4+
"log" // ERROR "`log` is in the blacklist"
5+
)
6+
7+
func SpewDebugInfo() {
8+
log.Println("Debug info")
9+
}

0 commit comments

Comments
 (0)