/
godot.go
83 lines (69 loc) · 1.84 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
package golinters
import (
"sync"
"github.com/tetafro/godot"
"golang.org/x/tools/go/analysis"
"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() *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue
analyzer := &analysis.Analyzer{
Name: godotName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
godotName,
"Check if comments end in a period",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
cfg := lintCtx.Cfg.LintersSettings.Godot
settings := godot.Settings{
Scope: godot.Scope(cfg.Scope),
Period: true,
Capital: cfg.Capital,
}
// Convert deprecated setting
if cfg.CheckAll { // nolint: staticcheck
settings.Scope = godot.TopLevelScope
}
if settings.Scope == "" {
settings.Scope = godot.DeclScope
}
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var issues []godot.Issue
for _, file := range pass.Files {
iss, err := godot.Run(file, pass.Fset, settings)
if err != nil {
return nil, err
}
issues = append(issues, iss...)
}
if len(issues) == 0 {
return nil, nil
}
res := make([]goanalysis.Issue, len(issues))
for k, i := range issues {
issue := result.Issue{
Pos: i.Pos,
Text: i.Message,
FromLinter: godotName,
Replacement: &result.Replacement{
NewLines: []string{i.Replacement},
},
}
res[k] = goanalysis.NewIssue(&issue, pass)
}
mu.Lock()
resIssues = append(resIssues, res...)
mu.Unlock()
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}