Skip to content

Commit fd31796

Browse files
add go-header linter
Signed-off-by: denis-tingajkin <denis.tingajkin@xored.com>
1 parent 36a5a40 commit fd31796

File tree

9 files changed

+144
-77
lines changed

9 files changed

+144
-77
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</p>
66

77
---
8-
8+
99
`golangci-lint` is a fast Go linters runner. It runs linters in parallel, uses caching, supports `yaml` config, has integrations
1010
with all major IDE and has dozens of linters included.
1111

go.mod

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
module github.com/golangci/golangci-lint
22

3-
go 1.12
3+
go 1.14
44

55
require (
6-
github.com/Djarvur/go-err113 v0.0.0-20200511133814-5174e21577d5
6+
github.com/Djarvur/go-err113 v0.1.0
77
github.com/OpenPeeDeeP/depguard v1.0.1
8+
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
89
github.com/bombsimon/wsl/v3 v3.1.0
10+
github.com/denis-tingajkin/go-header v0.3.1
911
github.com/fatih/color v1.9.0
1012
github.com/go-critic/go-critic v0.4.3
1113
github.com/go-lintpack/lintpack v0.5.2
14+
github.com/go-ole/go-ole v1.2.4 // indirect
1215
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b
1316
github.com/gofrs/flock v0.7.1
1417
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
@@ -18,18 +21,18 @@ require (
1821
github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3
1922
github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d
2023
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a
21-
github.com/golangci/ineffassign v0.0.0-20190609212857-42439a7714cc
22-
github.com/golangci/lint-1 v0.0.0-20191013205115-297bf364a8e0
24+
github.com/golangci/ineffassign v0.0.0-20180808204949-2ee8f2867dde
25+
github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219
2326
github.com/golangci/maligned v0.0.0-20180506175553-b1d89398deca
24-
github.com/golangci/misspell v0.0.0-20180809174111-950f5d19e770
27+
github.com/golangci/misspell v0.3.5
2528
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21
26-
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0
29+
github.com/golangci/revgrep v0.0.0-20180812185044-276a5c0a1039
2730
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
2831
github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a
29-
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3
32+
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
3033
github.com/kyoh86/exportloopref v0.1.4
3134
github.com/maratori/testpackage v1.0.1
32-
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb // v1.0
35+
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb
3336
github.com/mattn/go-colorable v0.1.6
3437
github.com/mitchellh/go-homedir v1.1.0
3538
github.com/mitchellh/go-ps v1.0.0
@@ -38,18 +41,18 @@ require (
3841
github.com/pkg/errors v0.9.1
3942
github.com/ryancurrah/gomodguard v1.1.0
4043
github.com/securego/gosec/v2 v2.3.0
41-
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada // v2.19.8
44+
github.com/shirou/gopsutil v2.20.5+incompatible
4245
github.com/sirupsen/logrus v1.6.0
4346
github.com/sonatard/noctx v0.0.1
4447
github.com/sourcegraph/go-diff v0.5.3
4548
github.com/spf13/cobra v1.0.0
4649
github.com/spf13/pflag v1.0.5
4750
github.com/spf13/viper v1.7.0
4851
github.com/stretchr/testify v1.6.1
49-
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2
52+
github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b
5053
github.com/tetafro/godot v0.4.2
51-
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e
52-
github.com/tommy-muehle/go-mnd v1.3.1-0.20200224220436-e6f9a994e8fa
54+
github.com/timakin/bodyclose v0.0.0-20200424151742-cb6215831a94
55+
github.com/tommy-muehle/go-mnd v1.3.0
5356
github.com/ultraware/funlen v0.0.2
5457
github.com/ultraware/whitespace v0.0.4
5558
github.com/uudashr/gocognit v1.0.1
@@ -59,5 +62,5 @@ require (
5962
honnef.co/go/tools v0.0.1-2020.1.4
6063
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed
6164
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
62-
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f
65+
mvdan.cc/unparam v0.0.0-20200501210554-b37ab49443f7
6366
)

go.sum

Lines changed: 30 additions & 59 deletions
Large diffs are not rendered by default.

pkg/config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ type LintersSettings struct {
239239
Dogsled DogsledSettings
240240
Gocognit GocognitSettings
241241
Godot GodotSettings
242+
Goheader GoHeaderSettings
242243
Testpackage TestpackageSettings
243244
Nestif NestifSettings
244245
NoLintLint NoLintLintSettings
@@ -247,6 +248,12 @@ type LintersSettings struct {
247248
Custom map[string]CustomLinterSettings
248249
}
249250

251+
type GoHeaderSettings struct {
252+
Values map[string]map[string]string `mapstructure:"values"`
253+
Template string `mapstructure:"template"`
254+
TemplatePath string `mapstructure:"template-path"`
255+
}
256+
250257
type GovetSettings struct {
251258
CheckShadowing bool `mapstructure:"check-shadowing"`
252259
Settings map[string]map[string]interface{}

pkg/golinters/goheader.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package golinters
2+
3+
import (
4+
"go/token"
5+
"sync"
6+
7+
goheader "github.com/denis-tingajkin/go-header"
8+
"golang.org/x/tools/go/analysis"
9+
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 goHeaderName = "goheader"
16+
17+
func NewGoHeader() *goanalysis.Linter {
18+
var mu sync.Mutex
19+
var issues []goanalysis.Issue
20+
21+
analyzer := &analysis.Analyzer{
22+
Name: goHeaderName,
23+
Doc: goanalysis.TheOnlyanalyzerDoc,
24+
}
25+
return goanalysis.NewLinter(
26+
goHeaderName,
27+
"Checks is file header matches to pattern",
28+
[]*analysis.Analyzer{analyzer},
29+
nil,
30+
).WithContextSetter(func(lintCtx *linter.Context) {
31+
cfg := lintCtx.Cfg.LintersSettings.Goheader
32+
c := &goheader.Configuration{
33+
Values: cfg.Values,
34+
Template: cfg.Template,
35+
TemplatePath: cfg.TemplatePath,
36+
}
37+
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
38+
template, err := c.GetTemplate()
39+
if err != nil {
40+
return nil, err
41+
}
42+
values, err := c.GetValues()
43+
if err != nil {
44+
return nil, err
45+
}
46+
a := goheader.New(goheader.WithTemplate(template), goheader.WithValues(values))
47+
var res []goanalysis.Issue
48+
for _, file := range pass.Files {
49+
i := a.Analyze(file)
50+
issue := result.Issue{
51+
Pos: token.Position{
52+
Line: i.Location().Line + 1,
53+
Column: i.Location().Position,
54+
Filename: pass.Fset.Position(file.Pos()).Filename,
55+
},
56+
Text: i.Message(),
57+
FromLinter: goHeaderName,
58+
}
59+
res = append(res, goanalysis.NewIssue(&issue, pass))
60+
}
61+
if len(res) == 0 {
62+
return nil, nil
63+
}
64+
65+
mu.Lock()
66+
issues = append(issues, res...)
67+
mu.Unlock()
68+
69+
return nil, nil
70+
}
71+
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
72+
return issues
73+
}).WithLoadMode(goanalysis.LoadModeSyntax)
74+
}

pkg/lint/lintersdb/manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
199199
WithPresets(linter.PresetFormatting).
200200
WithAutoFix().
201201
WithURL("https://godoc.org/golang.org/x/tools/cmd/goimports"),
202+
linter.NewConfig(golinters.NewGoHeader()).
203+
WithPresets(linter.PresetStyle).
204+
WithLoadForGoAnalysis().
205+
WithURL("https://github.com/denis-tingajkin/go-header"),
202206
linter.NewConfig(golinters.NewMaligned()).
203207
WithLoadForGoAnalysis().
204208
WithPresets(linter.PresetPerformance).

test/linters_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,9 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
183183
scanner := bufio.NewScanner(f)
184184
for scanner.Scan() {
185185
line := scanner.Text()
186-
if !strings.HasPrefix(line, "//") {
186+
if strings.HasPrefix(strings.TrimSpace(line), "package") {
187187
return rc
188188
}
189-
190189
line = strings.TrimPrefix(line, "//")
191190
if strings.HasPrefix(line, "args: ") {
192191
assert.Nil(t, rc.args)
@@ -212,8 +211,6 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
212211
rc.configPath = configPath
213212
continue
214213
}
215-
216-
assert.Fail(t, "invalid prefix of comment line %s", line)
217214
}
218215

219216
return rc

test/testdata/configs/go-header.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
linters-settings:
2+
goheader:
3+
template: MY {{title}}
4+
values:
5+
const:
6+
title: TITLE.

test/testdata/go-header.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*MY TITLE!*/ // ERROR "Expected:TITLE., Actual: TITLE!"
2+
3+
//args: -Egoheader
4+
//config_path: testdata/configs/go-header.yml
5+
package testdata

0 commit comments

Comments
 (0)