-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgomoddirectives.go
More file actions
293 lines (232 loc) · 7.33 KB
/
Copy pathgomoddirectives.go
File metadata and controls
293 lines (232 loc) · 7.33 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Package gomoddirectives a linter that handle directives into `go.mod`.
package gomoddirectives
import (
"context"
"fmt"
"go/token"
"regexp"
"slices"
"strings"
"github.com/ldez/grignotin/gomod"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/tools/go/analysis"
)
const (
reasonExclude = "exclude directive is not allowed"
reasonGoDebug = "godebug directive is not allowed"
reasonGoVersion = "go directive (%s) doesn't match the pattern '%s'"
reasonIgnore = "ignore directive is not allowed"
reasonReplace = "replacement are not allowed"
reasonReplaceDuplicate = "multiple replacement of the same module"
reasonReplaceIdentical = "the original module and the replacement are identical"
reasonReplaceLocal = "local replacement are not allowed"
reasonRetract = "a comment is mandatory to explain why the version has been retracted"
reasonTool = "tool directive is not allowed"
reasonToolchain = "toolchain directive is not allowed"
reasonToolchainPattern = "toolchain directive (%s) doesn't match the pattern '%s'"
)
// Result the analysis result.
type Result struct {
Reason string
Start token.Position
End token.Position
}
// NewResult creates a new Result.
func NewResult(file *modfile.File, line *modfile.Line, reason string) Result {
return Result{
Start: token.Position{Filename: file.Syntax.Name, Line: line.Start.Line, Column: line.Start.LineRune},
End: token.Position{Filename: file.Syntax.Name, Line: line.End.Line, Column: line.End.LineRune},
Reason: reason,
}
}
func (r Result) String() string {
return fmt.Sprintf("%s: %s", r.Start, r.Reason)
}
// Options the analyzer options.
type Options struct {
ReplaceAllowAll bool
ReplaceAllowList []string
ReplaceAllowLocal bool
ExcludeForbidden bool
IgnoreForbidden bool
RetractAllowNoExplanation bool
ToolchainForbidden bool
ToolchainPattern *regexp.Regexp
ToolForbidden bool
GoDebugForbidden bool
GoVersionPattern *regexp.Regexp
CheckModulePath bool
}
// AnalyzePass analyzes a pass.
func AnalyzePass(pass *analysis.Pass, opts Options) ([]Result, error) {
info, err := gomod.GetModuleInfo(context.Background())
if err != nil {
return nil, fmt.Errorf("get information about modules: %w", err)
}
goMod := info[0].GoMod
if pass.Module != nil && pass.Module.Path != "" {
for _, m := range info {
if m.Path == pass.Module.Path {
goMod = m.GoMod
break
}
}
}
f, err := parseGoMod(goMod)
if err != nil {
return nil, fmt.Errorf("parse %s: %w", goMod, err)
}
return AnalyzeFile(f, opts), nil
}
// Analyze analyzes a project.
func Analyze(opts Options) ([]Result, error) {
f, err := GetModuleFile()
if err != nil {
return nil, fmt.Errorf("failed to get module file: %w", err)
}
return AnalyzeFile(f, opts), nil
}
// AnalyzeFile analyzes a mod file.
func AnalyzeFile(file *modfile.File, opts Options) []Result {
checks := []func(file *modfile.File, opts Options) []Result{
checkModulePath,
checkRetractDirectives,
checkExcludeDirectives,
checkToolDirectives,
checkIgnoreDirectives,
checkReplaceDirectives,
checkToolchainDirective,
checkGoDebugDirectives,
checkGoVersionDirectives,
}
var results []Result
for _, check := range checks {
results = append(results, check(file, opts)...)
}
return results
}
func checkModulePath(file *modfile.File, opts Options) []Result {
if file.Module == nil || !opts.CheckModulePath {
return nil
}
err := module.CheckPath(file.Module.Mod.Path)
if err != nil {
return []Result{NewResult(file, file.Module.Syntax, err.Error())}
}
return nil
}
func checkGoVersionDirectives(file *modfile.File, opts Options) []Result {
if file == nil || file.Go == nil || opts.GoVersionPattern == nil || opts.GoVersionPattern.MatchString(file.Go.Version) {
return nil
}
return []Result{NewResult(file, file.Go.Syntax, fmt.Sprintf(reasonGoVersion, file.Go.Version, opts.GoVersionPattern.String()))}
}
func checkToolchainDirective(file *modfile.File, opts Options) []Result {
if file.Toolchain == nil {
return nil
}
if opts.ToolchainForbidden {
return []Result{NewResult(file, file.Toolchain.Syntax, reasonToolchain)}
}
if opts.ToolchainPattern == nil {
return nil
}
if !opts.ToolchainPattern.MatchString(file.Toolchain.Name) {
return []Result{NewResult(file, file.Toolchain.Syntax, fmt.Sprintf(reasonToolchainPattern, file.Toolchain.Name, opts.ToolchainPattern.String()))}
}
return nil
}
func checkRetractDirectives(file *modfile.File, opts Options) []Result {
if opts.RetractAllowNoExplanation {
return nil
}
var results []Result
for _, retract := range file.Retract {
if retract.Rationale != "" {
continue
}
results = append(results, NewResult(file, retract.Syntax, reasonRetract))
}
return results
}
func checkExcludeDirectives(file *modfile.File, opts Options) []Result {
if !opts.ExcludeForbidden {
return nil
}
var results []Result
for _, exclude := range file.Exclude {
results = append(results, NewResult(file, exclude.Syntax, reasonExclude))
}
return results
}
func checkIgnoreDirectives(file *modfile.File, opts Options) []Result {
if !opts.IgnoreForbidden {
return nil
}
var results []Result
for _, exclude := range file.Ignore {
results = append(results, NewResult(file, exclude.Syntax, reasonIgnore))
}
return results
}
func checkToolDirectives(file *modfile.File, opts Options) []Result {
if !opts.ToolForbidden {
return nil
}
var results []Result
for _, tool := range file.Tool {
results = append(results, NewResult(file, tool.Syntax, reasonTool))
}
return results
}
func checkReplaceDirectives(file *modfile.File, opts Options) []Result {
var results []Result
uniqReplace := map[string]struct{}{}
for _, replace := range file.Replace {
reason := checkReplaceDirective(opts, replace)
if reason != "" {
results = append(results, NewResult(file, replace.Syntax, reason))
continue
}
if replace.Old.Path == replace.New.Path && replace.Old.Version == replace.New.Version {
results = append(results, NewResult(file, replace.Syntax, reasonReplaceIdentical))
continue
}
if _, ok := uniqReplace[replace.Old.Path+replace.Old.Version]; ok {
results = append(results, NewResult(file, replace.Syntax, reasonReplaceDuplicate))
}
uniqReplace[replace.Old.Path+replace.Old.Version] = struct{}{}
}
return results
}
func checkReplaceDirective(opts Options, r *modfile.Replace) string {
if opts.ReplaceAllowAll {
return ""
}
if isLocal(r) {
if opts.ReplaceAllowLocal {
return ""
}
return fmt.Sprintf("%s: %s", reasonReplaceLocal, r.Old.Path)
}
if slices.Contains(opts.ReplaceAllowList, r.Old.Path) {
return ""
}
return fmt.Sprintf("%s: %s", reasonReplace, r.Old.Path)
}
func checkGoDebugDirectives(file *modfile.File, opts Options) []Result {
if !opts.GoDebugForbidden {
return nil
}
var results []Result
for _, goDebug := range file.Godebug {
results = append(results, NewResult(file, goDebug.Syntax, reasonGoDebug))
}
return results
}
// Filesystem paths found in "replace" directives are represented by a path with an empty version.
// https://github.com/golang/mod/blob/bc388b264a244501debfb9caea700c6dcaff10e2/module/module.go#L122-L124
func isLocal(r *modfile.Replace) bool {
return strings.TrimSpace(r.New.Version) == ""
}