-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgomoddirectives.go
255 lines (203 loc) · 6.52 KB
/
gomoddirectives.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
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
// Package gomoddirectives a linter that handle directives into `go.mod`.
package gomoddirectives
import (
"context"
"fmt"
"go/token"
"regexp"
"strings"
"github.com/ldez/grignotin/gomod"
"golang.org/x/mod/modfile"
"golang.org/x/tools/go/analysis"
)
const (
reasonRetract = "a comment is mandatory to explain why the version has been retracted"
reasonExclude = "exclude directive is not allowed"
reasonToolchain = "toolchain directive is not allowed"
reasonToolchainPattern = "toolchain directive (%s) doesn't match the pattern '%s'"
reasonTool = "tool directive is not allowed"
reasonGoDebug = "godebug directive is not allowed"
reasonGoVersion = "go directive (%s) doesn't match the pattern '%s'"
reasonReplaceLocal = "local replacement are not allowed"
reasonReplace = "replacement are not allowed"
reasonReplaceIdentical = "the original module and the replacement are identical"
reasonReplaceDuplicate = "multiple replacement of the same module"
)
// 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 {
ReplaceAllowList []string
ReplaceAllowLocal bool
ExcludeForbidden bool
RetractAllowNoExplanation bool
ToolchainForbidden bool
ToolchainPattern *regexp.Regexp
ToolForbidden bool
GoDebugForbidden bool
GoVersionPattern *regexp.Regexp
}
// 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{
checkRetractDirectives,
checkExcludeDirectives,
checkToolDirectives,
checkReplaceDirectives,
checkToolchainDirective,
checkGoDebugDirectives,
checkGoVersionDirectives,
}
var results []Result
for _, check := range checks {
results = append(results, check(file, opts)...)
}
return results
}
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 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 isLocal(r) {
if opts.ReplaceAllowLocal {
return ""
}
return fmt.Sprintf("%s: %s", reasonReplaceLocal, r.Old.Path)
}
for _, v := range opts.ReplaceAllowList {
if r.Old.Path == v {
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) == ""
}