Skip to content

build(deps): bump github.com/denis-tingaikin/go-header from 0.6.0 to 1.0.0 #5968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require (
github.com/ckaznocha/intrange v0.3.1
github.com/curioswitch/go-reassign v0.3.0
github.com/daixiang0/gci v0.13.7
github.com/denis-tingaikin/go-header v0.5.0
github.com/denis-tingaikin/go-header v1.0.0
github.com/fatih/color v1.18.0
github.com/firefart/nonamedreturns v1.0.6
github.com/fzipp/gocyclo v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 12 additions & 103 deletions pkg/golinters/goheader/goheader.go
Original file line number Diff line number Diff line change
@@ -1,126 +1,35 @@
package goheader

import (
"go/token"
"strings"

goheader "github.com/denis-tingaikin/go-header"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/v2/pkg/config"
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
"github.com/golangci/golangci-lint/v2/pkg/golinters/internal"
)

const linterName = "goheader"

func New(settings *config.GoHeaderSettings, replacer *strings.Replacer) *goanalysis.Linter {
conf := &goheader.Configuration{}
conf := &goheader.Config{}
if settings != nil {
conf = &goheader.Configuration{
conf = &goheader.Config{
Values: settings.Values,
Template: settings.Template,
TemplatePath: replacer.Replace(settings.TemplatePath),
}
}

return goanalysis.
NewLinterFromAnalyzer(&analysis.Analyzer{
Name: linterName,
Doc: "Check if file header matches to pattern",
Run: func(pass *analysis.Pass) (any, error) {
err := runGoHeader(pass, conf)
if err != nil {
return nil, err
}

return nil, nil
},
}).
WithLoadMode(goanalysis.LoadModeSyntax)
}

func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error {
if conf.TemplatePath == "" && conf.Template == "" {
// User did not pass template, so then do not run go-header linter
return nil
}

template, err := conf.GetTemplate()
if err != nil {
return err
}

values, err := conf.GetValues()
if err != nil {
return err
var goheaderSettings goheader.Settings
if err := conf.FillSettings(&goheaderSettings); err != nil {
internal.LinterLogger.Fatalf("%s: invalid toolchain pattern: %s", linterName, err.Error())
}

a := goheader.New(goheader.WithTemplate(template), goheader.WithValues(values))

for _, file := range pass.Files {
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
if !isGoFile {
continue
}

issue := a.Analyze(&goheader.Target{File: file, Path: position.Filename})
if issue == nil {
continue
}

f := pass.Fset.File(file.Pos())

commentLine := 1
var offset int

// Inspired by https://github.com/denis-tingaikin/go-header/blob/4c75a6a2332f025705325d6c71fff4616aedf48f/analyzer.go#L85-L92
if len(file.Comments) > 0 && file.Comments[0].Pos() < file.Package {
if !strings.HasPrefix(file.Comments[0].List[0].Text, "/*") {
// When the comment is "//" there is a one character offset.
offset = 1
}
commentLine = goanalysis.GetFilePositionFor(pass.Fset, file.Comments[0].Pos()).Line
}

// Skip issues related to build directives.
// https://github.com/denis-tingaikin/go-header/issues/18
if issue.Location().Position-offset < 0 {
continue
}

diag := analysis.Diagnostic{
Pos: f.LineStart(issue.Location().Line+1) + token.Pos(issue.Location().Position-offset), // The position of the first divergence.
Message: issue.Message(),
}

if fix := issue.Fix(); fix != nil {
current := len(fix.Actual)
for _, s := range fix.Actual {
current += len(s)
}

start := f.LineStart(commentLine)

end := start + token.Pos(current)

header := strings.Join(fix.Expected, "\n") + "\n"

// Adds an extra line between the package and the header.
if end == file.Package {
header += "\n"
}

diag.SuggestedFixes = []analysis.SuggestedFix{{
TextEdits: []analysis.TextEdit{{
Pos: start,
End: end,
NewText: []byte(header),
}},
}}
}

pass.Report(diag)
}

return nil
return goanalysis.NewLinter(
linterName,
"Checks if file header matches to pattern",
[]*analysis.Analyzer{goheader.New(&goheaderSettings)},
nil,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
8 changes: 4 additions & 4 deletions pkg/golinters/goheader/testdata/fix/in/goheader_3.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
Copyright 1999 The Awesome

Use of this source code is governed by LICENSE
*/
* Copyright 1999 The Awesome
*
* Use of this source code is governed by LICENSE
*/

//golangcitest:args -Egoheader
//golangcitest:expected_exitcode 0
Expand Down
6 changes: 4 additions & 2 deletions pkg/golinters/goheader/testdata/fix/out/goheader_2.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* Copyright 2024 The Awesome Project Authors
/*
Copyright 2024 The Awesome Project Authors

Use of this source code is governed by LICENSE */
Use of this source code is governed by LICENSE
*/

//golangcitest:args -Egoheader
//golangcitest:expected_exitcode 0
Expand Down
8 changes: 4 additions & 4 deletions pkg/golinters/goheader/testdata/fix/out/goheader_3.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
Copyright 2024 The Awesome Project Authors

Use of this source code is governed by LICENSE
*/
* Copyright 2024 The Awesome Project Authors
*
* Use of this source code is governed by LICENSE
*/

//golangcitest:args -Egoheader
//golangcitest:expected_exitcode 0
Expand Down
4 changes: 2 additions & 2 deletions pkg/golinters/goheader/testdata/fix/out/goheader_4.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright 2024 The Awesome Project Authors
Copyright 2024 The Awesome Project Authors

Use of this source code is governed by LICENSE
Use of this source code is governed by LICENSE
*/

//golangcitest:args -Egoheader
Expand Down
4 changes: 2 additions & 2 deletions pkg/golinters/goheader/testdata/goheader-fix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ linters:
const:
AUTHOR: The Awesome Project Authors
template: |-
Copyright 2024 {{ AUTHOR }}
Copyright 2024 {{ .AUTHOR }}

Use of this source code is governed by LICENSE
4 changes: 2 additions & 2 deletions pkg/golinters/goheader/testdata/goheader.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "2"
linters:
settings:
goheader:
template: MY {{title}}
template: MY {{.title}}.
values:
const:
title: TITLE.
title: TITLE
3 changes: 2 additions & 1 deletion pkg/golinters/goheader/testdata/goheader_bad.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*MY TITLE!*/ // want `Expected:TITLE\., Actual: TITLE!`
/*oops!*/ // want `template doesn't match`

//golangcitest:args -Egoheader
//golangcitest:config_path testdata/goheader.yml
//golangcitest:expected_exitcode 1
package testdata
5 changes: 2 additions & 3 deletions pkg/golinters/goheader/testdata/goheader_cgo.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//go:build ignore

// TODO(ldez) the linter doesn't support cgo.

/*MY TITLE!*/ // want `Expected:TITLE\., Actual: TITLE!`
/*my template*/ // want `template doesn't match`

//golangcitest:args -Egoheader
//golangcitest:config_path testdata/goheader.yml
//golangcitest:expected_exitcode 1
package testdata

/*
Expand Down
Loading