@@ -13,6 +13,9 @@ import (
1313
1414const (
1515 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."
1619)
1720
1821// NewGomodguard returns a new Gomodguard linter.
@@ -28,68 +31,63 @@ func NewGomodguard() *goanalysis.Linter {
2831
2932 return goanalysis .NewLinter (
3033 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 ,
3435 []* analysis.Analyzer {analyzer },
3536 nil ,
3637 ).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
5539
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
6553 }
54+ }
6655
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
6964 }
65+ }
7066
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+ }
7273
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
7976
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 )
8379 }
8480
81+ gomodguardIssues := processor .ProcessFiles (files )
82+
8583 mu .Lock ()
8684 defer mu .Unlock ()
8785
88- for _ , err := range gomodguardErrors {
86+ for _ , gomodguardIssue := range gomodguardIssues {
8987 issues = append (issues , goanalysis .NewIssue (& result.Issue {
9088 FromLinter : gomodguardName ,
91- Pos : err .Position ,
92- Text : err .Reason ,
89+ Pos : gomodguardIssue .Position ,
90+ Text : gomodguardIssue .Reason ,
9391 }, pass ))
9492 }
9593
0 commit comments