|
1 | 1 | package golinters |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "strings" |
6 | | - "sync" |
7 | | - |
8 | | - "github.com/OpenPeeDeeP/depguard" |
| 4 | + "github.com/OpenPeeDeeP/depguard/v2" |
9 | 5 | "golang.org/x/tools/go/analysis" |
10 | | - "golang.org/x/tools/go/loader" //nolint:staticcheck // require changes in github.com/OpenPeeDeeP/depguard |
11 | 6 |
|
12 | 7 | "github.com/golangci/golangci-lint/pkg/config" |
13 | | - "github.com/golangci/golangci-lint/pkg/fsutils" |
14 | 8 | "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" |
15 | | - "github.com/golangci/golangci-lint/pkg/lint/linter" |
16 | | - "github.com/golangci/golangci-lint/pkg/result" |
17 | 9 | ) |
18 | 10 |
|
19 | | -const depguardName = "depguard" |
20 | | - |
21 | 11 | func NewDepguard(settings *config.DepGuardSettings) *goanalysis.Linter { |
22 | | - var mu sync.Mutex |
23 | | - var resIssues []goanalysis.Issue |
24 | | - |
25 | | - analyzer := &analysis.Analyzer{ |
26 | | - Name: depguardName, |
27 | | - Doc: goanalysis.TheOnlyanalyzerDoc, |
28 | | - Run: goanalysis.DummyRun, |
29 | | - } |
30 | | - |
31 | | - return goanalysis.NewLinter( |
32 | | - depguardName, |
33 | | - "Go linter that checks if package imports are in a list of acceptable packages", |
34 | | - []*analysis.Analyzer{analyzer}, |
35 | | - nil, |
36 | | - ).WithContextSetter(func(lintCtx *linter.Context) { |
37 | | - dg, err := newDepGuard(settings) |
38 | | - |
39 | | - analyzer.Run = func(pass *analysis.Pass) (any, error) { |
40 | | - if err != nil { |
41 | | - return nil, err |
42 | | - } |
| 12 | + conf := depguard.LinterSettings{} |
43 | 13 |
|
44 | | - issues, errRun := dg.run(pass) |
45 | | - if errRun != nil { |
46 | | - return nil, errRun |
| 14 | + if settings != nil { |
| 15 | + for s, rule := range settings.Rules { |
| 16 | + list := &depguard.List{ |
| 17 | + Files: rule.Files, |
| 18 | + Allow: rule.Allow, |
47 | 19 | } |
48 | 20 |
|
49 | | - mu.Lock() |
50 | | - resIssues = append(resIssues, issues...) |
51 | | - mu.Unlock() |
52 | | - |
53 | | - return nil, nil |
54 | | - } |
55 | | - }).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { |
56 | | - return resIssues |
57 | | - }).WithLoadMode(goanalysis.LoadModeSyntax) |
58 | | -} |
59 | | - |
60 | | -type depGuard struct { |
61 | | - loadConfig *loader.Config |
62 | | - guardians []*guardian |
63 | | -} |
64 | | - |
65 | | -func newDepGuard(settings *config.DepGuardSettings) (*depGuard, error) { |
66 | | - ps, err := newGuardian(settings) |
67 | | - if err != nil { |
68 | | - return nil, err |
69 | | - } |
70 | | - |
71 | | - d := &depGuard{ |
72 | | - loadConfig: &loader.Config{ |
73 | | - Cwd: "", // fallbacked to os.Getcwd |
74 | | - Build: nil, // fallbacked to build.Default |
75 | | - }, |
76 | | - guardians: []*guardian{ps}, |
77 | | - } |
78 | | - |
79 | | - for _, additional := range settings.AdditionalGuards { |
80 | | - add := additional |
81 | | - ps, err = newGuardian(&add) |
82 | | - if err != nil { |
83 | | - return nil, err |
84 | | - } |
85 | | - |
86 | | - d.guardians = append(d.guardians, ps) |
87 | | - } |
88 | | - |
89 | | - return d, nil |
90 | | -} |
91 | | - |
92 | | -func (d depGuard) run(pass *analysis.Pass) ([]goanalysis.Issue, error) { |
93 | | - prog := goanalysis.MakeFakeLoaderProgram(pass) |
94 | | - |
95 | | - var resIssues []goanalysis.Issue |
96 | | - for _, g := range d.guardians { |
97 | | - issues, errRun := g.run(d.loadConfig, prog, pass) |
98 | | - if errRun != nil { |
99 | | - return nil, errRun |
100 | | - } |
101 | | - |
102 | | - resIssues = append(resIssues, issues...) |
103 | | - } |
104 | | - |
105 | | - return resIssues, nil |
106 | | -} |
107 | | - |
108 | | -type guardian struct { |
109 | | - *depguard.Depguard |
110 | | - pkgsWithErrorMessage map[string]string |
111 | | -} |
112 | | - |
113 | | -func newGuardian(settings *config.DepGuardSettings) (*guardian, error) { |
114 | | - var ignoreFileRules []string |
115 | | - for _, rule := range settings.IgnoreFileRules { |
116 | | - ignoreFileRules = append(ignoreFileRules, fsutils.NormalizePathInRegex(rule)) |
117 | | - } |
118 | | - |
119 | | - dg := &depguard.Depguard{ |
120 | | - Packages: settings.Packages, |
121 | | - IncludeGoRoot: settings.IncludeGoRoot, |
122 | | - IgnoreFileRules: ignoreFileRules, |
123 | | - } |
124 | | - |
125 | | - var err error |
126 | | - dg.ListType, err = getDepGuardListType(settings.ListType) |
127 | | - if err != nil { |
128 | | - return nil, err |
129 | | - } |
130 | | - |
131 | | - // if the list type was a denylist the packages with error messages should be included in the denylist package list |
132 | | - if dg.ListType == depguard.LTBlacklist { |
133 | | - noMessagePackages := make(map[string]bool) |
134 | | - for _, pkg := range dg.Packages { |
135 | | - noMessagePackages[pkg] = true |
136 | | - } |
| 21 | + // because of bug with Viper parsing (split on dot) we use a list of struct instead of a map. |
| 22 | + // https://github.com/spf13/viper/issues/324 |
| 23 | + // https://github.com/golangci/golangci-lint/issues/3749#issuecomment-1492536630 |
137 | 24 |
|
138 | | - for pkg := range settings.PackagesWithErrorMessage { |
139 | | - if _, ok := noMessagePackages[pkg]; !ok { |
140 | | - dg.Packages = append(dg.Packages, pkg) |
| 25 | + deny := map[string]string{} |
| 26 | + for _, r := range rule.Deny { |
| 27 | + deny[r.Pkg] = r.Desc |
141 | 28 | } |
142 | | - } |
143 | | - } |
144 | | - |
145 | | - return &guardian{ |
146 | | - Depguard: dg, |
147 | | - pkgsWithErrorMessage: settings.PackagesWithErrorMessage, |
148 | | - }, nil |
149 | | -} |
150 | | - |
151 | | -func (g guardian) run(loadConfig *loader.Config, prog *loader.Program, pass *analysis.Pass) ([]goanalysis.Issue, error) { |
152 | | - issues, err := g.Run(loadConfig, prog) |
153 | | - if err != nil { |
154 | | - return nil, err |
155 | | - } |
156 | | - |
157 | | - res := make([]goanalysis.Issue, 0, len(issues)) |
158 | | - |
159 | | - for _, issue := range issues { |
160 | | - res = append(res, |
161 | | - goanalysis.NewIssue(&result.Issue{ |
162 | | - Pos: issue.Position, |
163 | | - Text: g.createMsg(issue.PackageName), |
164 | | - FromLinter: depguardName, |
165 | | - }, pass), |
166 | | - ) |
167 | | - } |
168 | | - |
169 | | - return res, nil |
170 | | -} |
171 | | - |
172 | | -func (g guardian) createMsg(pkgName string) string { |
173 | | - msgSuffix := "is in the denylist" |
174 | | - if g.ListType == depguard.LTWhitelist { |
175 | | - msgSuffix = "is not in the allowlist" |
176 | | - } |
| 29 | + list.Deny = deny |
177 | 30 |
|
178 | | - var userSuppliedMsgSuffix string |
179 | | - if g.pkgsWithErrorMessage != nil { |
180 | | - userSuppliedMsgSuffix = g.pkgsWithErrorMessage[pkgName] |
181 | | - if userSuppliedMsgSuffix != "" { |
182 | | - userSuppliedMsgSuffix = ": " + userSuppliedMsgSuffix |
| 31 | + conf[s] = list |
183 | 32 | } |
184 | 33 | } |
185 | 34 |
|
186 | | - return fmt.Sprintf("%s %s%s", formatCode(pkgName, nil), msgSuffix, userSuppliedMsgSuffix) |
187 | | -} |
188 | | - |
189 | | -func getDepGuardListType(listType string) (depguard.ListType, error) { |
190 | | - if listType == "" { |
191 | | - return depguard.LTBlacklist, nil |
192 | | - } |
193 | | - |
194 | | - listT, found := depguard.StringToListType[strings.ToLower(listType)] |
195 | | - if !found { |
196 | | - return depguard.LTBlacklist, fmt.Errorf("unsure what list type %s is", listType) |
| 35 | + a, err := depguard.NewAnalyzer(&conf) |
| 36 | + if err != nil { |
| 37 | + linterLogger.Fatalf("depguard: create analyzer: %v", err) |
197 | 38 | } |
198 | 39 |
|
199 | | - return listT, nil |
| 40 | + return goanalysis.NewLinter( |
| 41 | + a.Name, |
| 42 | + a.Doc, |
| 43 | + []*analysis.Analyzer{a}, |
| 44 | + nil, |
| 45 | + ).WithLoadMode(goanalysis.LoadModeSyntax) |
200 | 46 | } |
0 commit comments