@@ -13,6 +13,9 @@ import (
13
13
14
14
const (
15
15
gomodguardName = "gomodguard"
16
+ gomodguardDesc = "Allow and block list linter for direct Go module dependencies. " +
17
+ "This is different from depguard where there are different block " +
18
+ "types for example version constraints and module recommendations."
16
19
)
17
20
18
21
// NewGomodguard returns a new Gomodguard linter.
@@ -28,68 +31,63 @@ func NewGomodguard() *goanalysis.Linter {
28
31
29
32
return goanalysis .NewLinter (
30
33
gomodguardName ,
31
- "Allow and block list linter for direct Go module dependencies. " +
32
- "This is different from depguard where there are different block " +
33
- "types for example version constraints and module recommendations." ,
34
+ gomodguardDesc ,
34
35
[]* analysis.Analyzer {analyzer },
35
36
nil ,
36
37
).WithContextSetter (func (lintCtx * linter.Context ) {
37
- analyzer .Run = func (pass * analysis.Pass ) (interface {}, error ) {
38
- var (
39
- files = []string {}
40
- linterCfg = lintCtx .Cfg .LintersSettings .Gomodguard
41
- processorCfg = & gomodguard.Configuration {}
42
- )
43
- processorCfg .Allowed .Modules = linterCfg .Allowed .Modules
44
- processorCfg .Allowed .Domains = linterCfg .Allowed .Domains
45
- for n := range linterCfg .Blocked .Modules {
46
- for k , v := range linterCfg .Blocked .Modules [n ] {
47
- m := map [string ]gomodguard.BlockedModule {k : {
48
- Recommendations : v .Recommendations ,
49
- Reason : v .Reason ,
50
- }}
51
- processorCfg .Blocked .Modules = append (processorCfg .Blocked .Modules , m )
52
- break
53
- }
54
- }
38
+ linterCfg := lintCtx .Cfg .LintersSettings .Gomodguard
55
39
56
- for n := range linterCfg .Blocked .Versions {
57
- for k , v := range linterCfg .Blocked .Versions [n ] {
58
- m := map [string ]gomodguard.BlockedVersion {k : {
59
- Version : v .Version ,
60
- Reason : v .Reason ,
61
- }}
62
- processorCfg .Blocked .Versions = append (processorCfg .Blocked .Versions , m )
63
- break
64
- }
40
+ processorCfg := & gomodguard.Configuration {}
41
+ processorCfg .Allowed .Modules = linterCfg .Allowed .Modules
42
+ processorCfg .Allowed .Domains = linterCfg .Allowed .Domains
43
+ processorCfg .Blocked .LocalReplaceDirectives = linterCfg .Blocked .LocalReplaceDirectives
44
+
45
+ for n := range linterCfg .Blocked .Modules {
46
+ for k , v := range linterCfg .Blocked .Modules [n ] {
47
+ m := map [string ]gomodguard.BlockedModule {k : {
48
+ Recommendations : v .Recommendations ,
49
+ Reason : v .Reason ,
50
+ }}
51
+ processorCfg .Blocked .Modules = append (processorCfg .Blocked .Modules , m )
52
+ break
65
53
}
54
+ }
66
55
67
- for _ , file := range pass .Files {
68
- files = append (files , pass .Fset .PositionFor (file .Pos (), false ).Filename )
56
+ for n := range linterCfg .Blocked .Versions {
57
+ for k , v := range linterCfg .Blocked .Versions [n ] {
58
+ m := map [string ]gomodguard.BlockedVersion {k : {
59
+ Version : v .Version ,
60
+ Reason : v .Reason ,
61
+ }}
62
+ processorCfg .Blocked .Versions = append (processorCfg .Blocked .Versions , m )
63
+ break
69
64
}
65
+ }
70
66
71
- processorCfg .Blocked .LocalReplaceDirectives = linterCfg .Blocked .LocalReplaceDirectives
67
+ processor , err := gomodguard .NewProcessor (processorCfg )
68
+ if err != nil {
69
+ lintCtx .Log .Warnf ("running gomodguard failed: %s: if you are not using go modules " +
70
+ "it is suggested to disable this linter" , err )
71
+ return
72
+ }
72
73
73
- processor , err := gomodguard .NewProcessor (processorCfg )
74
- if err != nil {
75
- lintCtx .Log .Warnf ("running gomodguard failed: %s: if you are not using go modules " +
76
- "it is suggested to disable this linter" , err )
77
- return nil , nil
78
- }
74
+ analyzer .Run = func (pass * analysis.Pass ) (interface {}, error ) {
75
+ var files []string
79
76
80
- gomodguardErrors := processor .ProcessFiles (files )
81
- if len (gomodguardErrors ) == 0 {
82
- return nil , nil
77
+ for _ , file := range pass .Files {
78
+ files = append (files , pass .Fset .PositionFor (file .Pos (), false ).Filename )
83
79
}
84
80
81
+ gomodguardIssues := processor .ProcessFiles (files )
82
+
85
83
mu .Lock ()
86
84
defer mu .Unlock ()
87
85
88
- for _ , err := range gomodguardErrors {
86
+ for _ , gomodguardIssue := range gomodguardIssues {
89
87
issues = append (issues , goanalysis .NewIssue (& result.Issue {
90
88
FromLinter : gomodguardName ,
91
- Pos : err .Position ,
92
- Text : err .Reason ,
89
+ Pos : gomodguardIssue .Position ,
90
+ Text : gomodguardIssue .Reason ,
93
91
}, pass ))
94
92
}
95
93
0 commit comments