forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgodot.go
102 lines (83 loc) · 2.23 KB
/
godot.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
package golinters
import (
"sync"
"github.com/tetafro/godot"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)
const godotName = "godot"
func NewGodot(settings *config.GodotSettings) *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue
var dotSettings godot.Settings
if settings != nil {
dotSettings = godot.Settings{
Scope: godot.Scope(settings.Scope),
Exclude: settings.Exclude,
Period: settings.Period,
Capital: settings.Capital,
}
// Convert deprecated setting
// todo(butuzov): remove on v2 release
if settings.CheckAll { //nolint:staticcheck // Keep for retro-compatibility.
dotSettings.Scope = godot.AllScope
}
}
if dotSettings.Scope == "" {
dotSettings.Scope = godot.DeclScope
}
analyzer := &analysis.Analyzer{
Name: godotName,
Doc: goanalysis.TheOnlyanalyzerDoc,
Run: func(pass *analysis.Pass) (interface{}, error) {
issues, err := runGodot(pass, dotSettings)
if err != nil {
return nil, err
}
if len(issues) == 0 {
return nil, nil
}
mu.Lock()
resIssues = append(resIssues, issues...)
mu.Unlock()
return nil, nil
},
}
return goanalysis.NewLinter(
godotName,
"Check if comments end in a period",
[]*analysis.Analyzer{analyzer},
nil,
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}
func runGodot(pass *analysis.Pass, settings godot.Settings) ([]goanalysis.Issue, error) {
var lintIssues []godot.Issue
for _, file := range pass.Files {
iss, err := godot.Run(file, pass.Fset, settings)
if err != nil {
return nil, err
}
lintIssues = append(lintIssues, iss...)
}
if len(lintIssues) == 0 {
return nil, nil
}
issues := make([]goanalysis.Issue, len(lintIssues))
for k, i := range lintIssues {
issue := result.Issue{
Pos: i.Pos,
Text: i.Message,
FromLinter: godotName,
Replacement: &result.Replacement{
NewLines: []string{i.Replacement},
},
}
issues[k] = goanalysis.NewIssue(&issue, pass)
}
return issues, nil
}