Skip to content

Commit f50a3f4

Browse files
update go-header to version v1.0.0-rc.8
Signed-off-by: Denis Tingaikin <[email protected]>
1 parent 766c461 commit f50a3f4

File tree

9 files changed

+32
-124
lines changed

9 files changed

+32
-124
lines changed

pkg/golinters/goheader/goheader.go

Lines changed: 10 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package goheader
22

33
import (
4-
"go/token"
54
"strings"
65

76
goheader "github.com/denis-tingaikin/go-header"
@@ -14,113 +13,20 @@ import (
1413
const linterName = "goheader"
1514

1615
func New(settings *config.GoHeaderSettings, replacer *strings.Replacer) *goanalysis.Linter {
17-
conf := &goheader.Configuration{}
16+
conf := &goheader.Config{}
1817
if settings != nil {
19-
conf = &goheader.Configuration{
18+
conf = &goheader.Config{
2019
Values: settings.Values,
2120
Template: settings.Template,
2221
TemplatePath: replacer.Replace(settings.TemplatePath),
2322
}
2423
}
25-
26-
return goanalysis.
27-
NewLinterFromAnalyzer(&analysis.Analyzer{
28-
Name: linterName,
29-
Doc: "Check if file header matches to pattern",
30-
Run: func(pass *analysis.Pass) (any, error) {
31-
err := runGoHeader(pass, conf)
32-
if err != nil {
33-
return nil, err
34-
}
35-
36-
return nil, nil
37-
},
38-
}).
39-
WithLoadMode(goanalysis.LoadModeSyntax)
40-
}
41-
42-
func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error {
43-
if conf.TemplatePath == "" && conf.Template == "" {
44-
// User did not pass template, so then do not run go-header linter
45-
return nil
46-
}
47-
48-
template, err := conf.GetTemplate()
49-
if err != nil {
50-
return err
51-
}
52-
53-
values, err := conf.GetValues()
54-
if err != nil {
55-
return err
56-
}
57-
58-
a := goheader.New(goheader.WithTemplate(template), goheader.WithValues(values))
59-
60-
for _, file := range pass.Files {
61-
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
62-
if !isGoFile {
63-
continue
64-
}
65-
66-
issue := a.Analyze(&goheader.Target{File: file, Path: position.Filename})
67-
if issue == nil {
68-
continue
69-
}
70-
71-
f := pass.Fset.File(file.Pos())
72-
73-
commentLine := 1
74-
var offset int
75-
76-
// Inspired by https://github.com/denis-tingaikin/go-header/blob/4c75a6a2332f025705325d6c71fff4616aedf48f/analyzer.go#L85-L92
77-
if len(file.Comments) > 0 && file.Comments[0].Pos() < file.Package {
78-
if !strings.HasPrefix(file.Comments[0].List[0].Text, "/*") {
79-
// When the comment is "//" there is a one character offset.
80-
offset = 1
81-
}
82-
commentLine = goanalysis.GetFilePositionFor(pass.Fset, file.Comments[0].Pos()).Line
83-
}
84-
85-
// Skip issues related to build directives.
86-
// https://github.com/denis-tingaikin/go-header/issues/18
87-
if issue.Location().Position-offset < 0 {
88-
continue
89-
}
90-
91-
diag := analysis.Diagnostic{
92-
Pos: f.LineStart(issue.Location().Line+1) + token.Pos(issue.Location().Position-offset), // The position of the first divergence.
93-
Message: issue.Message(),
94-
}
95-
96-
if fix := issue.Fix(); fix != nil {
97-
current := len(fix.Actual)
98-
for _, s := range fix.Actual {
99-
current += len(s)
100-
}
101-
102-
start := f.LineStart(commentLine)
103-
104-
end := start + token.Pos(current)
105-
106-
header := strings.Join(fix.Expected, "\n") + "\n"
107-
108-
// Adds an extra line between the package and the header.
109-
if end == file.Package {
110-
header += "\n"
111-
}
112-
113-
diag.SuggestedFixes = []analysis.SuggestedFix{{
114-
TextEdits: []analysis.TextEdit{{
115-
Pos: start,
116-
End: end,
117-
NewText: []byte(header),
118-
}},
119-
}}
120-
}
121-
122-
pass.Report(diag)
123-
}
124-
125-
return nil
24+
var goheaderSettings goheader.Settings
25+
_ = conf.FillSettings(&goheaderSettings)
26+
return goanalysis.NewLinter(
27+
linterName,
28+
"Checks if file header matches to pattern",
29+
[]*analysis.Analyzer{goheader.New(&goheaderSettings)},
30+
nil,
31+
).WithLoadMode(goanalysis.LoadModeSyntax)
12632
}

pkg/golinters/goheader/testdata/fix/in/goheader_3.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
Copyright 1999 The Awesome
3-
4-
Use of this source code is governed by LICENSE
5-
*/
2+
* Copyright 1999 The Awesome
3+
*
4+
* Use of this source code is governed by LICENSE
5+
*/
66

77
//golangcitest:args -Egoheader
88
//golangcitest:expected_exitcode 0

pkg/golinters/goheader/testdata/fix/out/goheader_2.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
/* Copyright 2024 The Awesome Project Authors
1+
/*
2+
Copyright 2024 The Awesome Project Authors
23
3-
Use of this source code is governed by LICENSE */
4+
Use of this source code is governed by LICENSE
5+
*/
46

57
//golangcitest:args -Egoheader
68
//golangcitest:expected_exitcode 0

pkg/golinters/goheader/testdata/fix/out/goheader_3.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
Copyright 2024 The Awesome Project Authors
3-
4-
Use of this source code is governed by LICENSE
5-
*/
2+
* Copyright 2024 The Awesome Project Authors
3+
*
4+
* Use of this source code is governed by LICENSE
5+
*/
66

77
//golangcitest:args -Egoheader
88
//golangcitest:expected_exitcode 0

pkg/golinters/goheader/testdata/fix/out/goheader_4.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
Copyright 2024 The Awesome Project Authors
2+
Copyright 2024 The Awesome Project Authors
33
4-
Use of this source code is governed by LICENSE
4+
Use of this source code is governed by LICENSE
55
*/
66

77
//golangcitest:args -Egoheader

pkg/golinters/goheader/testdata/goheader-fix.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ linters:
77
const:
88
AUTHOR: The Awesome Project Authors
99
template: |-
10-
Copyright 2024 {{ AUTHOR }}
11-
10+
Copyright 2024 {{ .AUTHOR }}
11+
1212
Use of this source code is governed by LICENSE

pkg/golinters/goheader/testdata/goheader.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: "2"
33
linters:
44
settings:
55
goheader:
6-
template: MY {{title}}
6+
template: MY {{.title}}.
77
values:
88
const:
9-
title: TITLE.
9+
title: TITLE
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/*MY TITLE!*/ // want `Expected:TITLE\., Actual: TITLE!`
1+
/*oops!*/ // want `template doesn't match`
22

33
//golangcitest:args -Egoheader
44
//golangcitest:config_path testdata/goheader.yml
5+
//golangcitest:expected_exitcode 1
56
package testdata

pkg/golinters/goheader/testdata/goheader_cgo.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//go:build ignore
22

3-
// TODO(ldez) the linter doesn't support cgo.
4-
5-
/*MY TITLE!*/ // want `Expected:TITLE\., Actual: TITLE!`
3+
/*my template*/ // want `template doesn't match`
64

75
//golangcitest:args -Egoheader
86
//golangcitest:config_path testdata/goheader.yml
7+
//golangcitest:expected_exitcode 1
98
package testdata
109

1110
/*

0 commit comments

Comments
 (0)