-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.go
75 lines (61 loc) · 1.6 KB
/
example.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
package linters
import (
"go/ast"
"strings"
"github.com/golangci/plugin-module-register/register"
"golang.org/x/tools/go/analysis"
)
func init() {
register.Plugin("example", New)
}
type MySettings struct {
One string `json:"one"`
Two []Element `json:"two"`
Three Element `json:"three"`
}
type Element struct {
Name string `json:"name"`
}
type PluginExample struct {
settings MySettings
}
func New(settings any) (register.LinterPlugin, error) {
// The configuration type will be map[string]any or []interface, it depends on your configuration.
// You can use https://github.com/go-viper/mapstructure to convert map to struct.
s, err := register.DecodeSettings[MySettings](settings)
if err != nil {
return nil, err
}
return &PluginExample{settings: s}, nil
}
func (f *PluginExample) BuildAnalyzers() ([]*analysis.Analyzer, error) {
return []*analysis.Analyzer{
{
Name: "todo",
Doc: "finds todos without author",
Run: f.run,
},
}, nil
}
func (f *PluginExample) GetLoadMode() string {
return register.LoadModeSyntax
}
func (f *PluginExample) run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool {
if comment, ok := n.(*ast.Comment); ok {
if strings.HasPrefix(comment.Text, "// TODO:") || strings.HasPrefix(comment.Text, "// TODO():") {
pass.Report(analysis.Diagnostic{
Pos: comment.Pos(),
End: 0,
Category: "todo",
Message: "TODO comment has no author",
SuggestedFixes: nil,
})
}
}
return true
})
}
return nil, nil
}