Skip to content

Commit 0007283

Browse files
update go header to v1.0.0-rc.7
Signed-off-by: Denis Tingaikin <[email protected]>
1 parent fd792ae commit 0007283

File tree

11 files changed

+28
-129
lines changed

11 files changed

+28
-129
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ require (
3535
github.com/ckaznocha/intrange v0.3.1
3636
github.com/curioswitch/go-reassign v0.3.0
3737
github.com/daixiang0/gci v0.13.6
38-
github.com/denis-tingaikin/go-header v0.5.0
38+
github.com/denis-tingaikin/go-header v1.0.0-rc.7
3939
github.com/fatih/color v1.18.0
4040
github.com/firefart/nonamedreturns v1.0.6
4141
github.com/fzipp/gocyclo v0.6.0

go.sum

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/goheader/goheader.go

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

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

0 commit comments

Comments
 (0)