-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
goheader.go
74 lines (66 loc) · 1.82 KB
/
goheader.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
package golinters
import (
"go/token"
"sync"
goheader "github.com/denis-tingajkin/go-header"
"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 goHeaderName = "goheader"
func NewGoHeader() *goanalysis.Linter {
var mu sync.Mutex
var issues []goanalysis.Issue
analyzer := &analysis.Analyzer{
Name: goHeaderName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
goHeaderName,
"Checks is file header matches to pattern",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
cfg := lintCtx.Cfg.LintersSettings.Goheader
c := &goheader.Configuration{
Values: cfg.Values,
Template: cfg.Template,
TemplatePath: cfg.TemplatePath,
}
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
template, err := c.GetTemplate()
if err != nil {
return nil, err
}
values, err := c.GetValues()
if err != nil {
return nil, err
}
a := goheader.New(goheader.WithTemplate(template), goheader.WithValues(values))
var res []goanalysis.Issue
for _, file := range pass.Files {
i := a.Analyze(file)
issue := result.Issue{
Pos: token.Position{
Line: i.Location().Line + 1,
Column: i.Location().Position,
Filename: pass.Fset.Position(file.Pos()).Filename,
},
Text: i.Message(),
FromLinter: goHeaderName,
}
res = append(res, goanalysis.NewIssue(&issue, pass))
}
if len(res) == 0 {
return nil, nil
}
mu.Lock()
issues = append(issues, res...)
mu.Unlock()
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return issues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}