github.com/nozzle/golangci-lint@v1.49.0-nz3/pkg/golinters/godot.go (about) 1 package golinters 2 3 import ( 4 "sync" 5 6 "github.com/tetafro/godot" 7 "golang.org/x/tools/go/analysis" 8 9 "github.com/golangci/golangci-lint/pkg/config" 10 "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" 11 "github.com/golangci/golangci-lint/pkg/lint/linter" 12 "github.com/golangci/golangci-lint/pkg/result" 13 ) 14 15 const godotName = "godot" 16 17 func NewGodot(settings *config.GodotSettings) *goanalysis.Linter { 18 var mu sync.Mutex 19 var resIssues []goanalysis.Issue 20 21 var dotSettings godot.Settings 22 23 if settings != nil { 24 dotSettings = godot.Settings{ 25 Scope: godot.Scope(settings.Scope), 26 Exclude: settings.Exclude, 27 Period: settings.Period, 28 Capital: settings.Capital, 29 } 30 31 // Convert deprecated setting 32 // todo(butuzov): remove on v2 release 33 if settings.CheckAll { //nolint:staticcheck // Keep for retro-compatibility. 34 dotSettings.Scope = godot.AllScope 35 } 36 } 37 38 if dotSettings.Scope == "" { 39 dotSettings.Scope = godot.DeclScope 40 } 41 42 analyzer := &analysis.Analyzer{ 43 Name: godotName, 44 Doc: goanalysis.TheOnlyanalyzerDoc, 45 Run: func(pass *analysis.Pass) (interface{}, error) { 46 issues, err := runGodot(pass, dotSettings) 47 if err != nil { 48 return nil, err 49 } 50 51 if len(issues) == 0 { 52 return nil, nil 53 } 54 55 mu.Lock() 56 resIssues = append(resIssues, issues...) 57 mu.Unlock() 58 59 return nil, nil 60 }, 61 } 62 63 return goanalysis.NewLinter( 64 godotName, 65 "Check if comments end in a period", 66 []*analysis.Analyzer{analyzer}, 67 nil, 68 ).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { 69 return resIssues 70 }).WithLoadMode(goanalysis.LoadModeSyntax) 71 } 72 73 func runGodot(pass *analysis.Pass, settings godot.Settings) ([]goanalysis.Issue, error) { 74 var lintIssues []godot.Issue 75 for _, file := range pass.Files { 76 iss, err := godot.Run(file, pass.Fset, settings) 77 if err != nil { 78 return nil, err 79 } 80 lintIssues = append(lintIssues, iss...) 81 } 82 83 if len(lintIssues) == 0 { 84 return nil, nil 85 } 86 87 issues := make([]goanalysis.Issue, len(lintIssues)) 88 for k, i := range lintIssues { 89 issue := result.Issue{ 90 Pos: i.Pos, 91 Text: i.Message, 92 FromLinter: godotName, 93 Replacement: &result.Replacement{ 94 NewLines: []string{i.Replacement}, 95 }, 96 } 97 98 issues[k] = goanalysis.NewIssue(&issue, pass) 99 } 100 101 return issues, nil 102 }