forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdepguard.go
57 lines (46 loc) · 1.42 KB
/
depguard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package depguard
import (
"strings"
"github.com/OpenPeeDeeP/depguard/v2"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/v2/pkg/config"
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
"github.com/golangci/golangci-lint/v2/pkg/lint/linter"
)
func New(settings *config.DepGuardSettings, replacer *strings.Replacer) *goanalysis.Linter {
conf := depguard.LinterSettings{}
if settings != nil {
for s, rule := range settings.Rules {
var extendedPatterns []string
for _, file := range rule.Files {
extendedPatterns = append(extendedPatterns, replacer.Replace(file))
}
list := &depguard.List{
ListMode: rule.ListMode,
Files: extendedPatterns,
Allow: rule.Allow,
}
// because of bug with Viper parsing (split on dot) we use a list of struct instead of a map.
// https://github.com/spf13/viper/issues/324
// https://github.com/golangci/golangci-lint/issues/3749#issuecomment-1492536630
deny := map[string]string{}
for _, r := range rule.Deny {
deny[r.Pkg] = r.Desc
}
list.Deny = deny
conf[s] = list
}
}
a := depguard.NewUncompiledAnalyzer(&conf)
return goanalysis.NewLinter(
a.Analyzer.Name,
a.Analyzer.Doc,
[]*analysis.Analyzer{a.Analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
err := a.Compile()
if err != nil {
lintCtx.Log.Errorf("create analyzer: %v", err)
}
}).WithLoadMode(goanalysis.LoadModeSyntax)
}