github.com/elek/golangci-lint@v1.42.2-0.20211208090441-c05b7fcb3a9a/pkg/golinters/gomoddirectives.go (about) 1 package golinters 2 3 import ( 4 "sync" 5 6 "github.com/ldez/gomoddirectives" 7 "golang.org/x/tools/go/analysis" 8 9 "github.com/elek/golangci-lint/pkg/config" 10 "github.com/elek/golangci-lint/pkg/golinters/goanalysis" 11 "github.com/elek/golangci-lint/pkg/lint/linter" 12 "github.com/elek/golangci-lint/pkg/result" 13 ) 14 15 const goModDirectivesName = "gomoddirectives" 16 17 // NewGoModDirectives returns a new gomoddirectives linter. 18 func NewGoModDirectives(settings *config.GoModDirectivesSettings) *goanalysis.Linter { 19 var issues []goanalysis.Issue 20 var once sync.Once 21 22 var opts gomoddirectives.Options 23 if settings != nil { 24 opts.ReplaceAllowLocal = settings.ReplaceLocal 25 opts.ReplaceAllowList = settings.ReplaceAllowList 26 opts.RetractAllowNoExplanation = settings.RetractAllowNoExplanation 27 opts.ExcludeForbidden = settings.ExcludeForbidden 28 } 29 30 analyzer := &analysis.Analyzer{ 31 Name: goanalysis.TheOnlyAnalyzerName, 32 Doc: goanalysis.TheOnlyanalyzerDoc, 33 } 34 35 return goanalysis.NewLinter( 36 goModDirectivesName, 37 "Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.", 38 []*analysis.Analyzer{analyzer}, 39 nil, 40 ).WithContextSetter(func(lintCtx *linter.Context) { 41 analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { 42 once.Do(func() { 43 results, err := gomoddirectives.Analyze(opts) 44 if err != nil { 45 lintCtx.Log.Warnf("running %s failed: %s: "+ 46 "if you are not using go modules it is suggested to disable this linter", goModDirectivesName, err) 47 return 48 } 49 50 for _, p := range results { 51 issues = append(issues, goanalysis.NewIssue(&result.Issue{ 52 FromLinter: goModDirectivesName, 53 Pos: p.Start, 54 Text: p.Reason, 55 }, pass)) 56 } 57 }) 58 59 return nil, nil 60 } 61 }).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { 62 return issues 63 }).WithLoadMode(goanalysis.LoadModeSyntax) 64 }