diff --git a/go.mod b/go.mod index cad2143c8cbe..c6c699982c98 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,7 @@ require ( github.com/ckaznocha/intrange v0.1.0 github.com/curioswitch/go-reassign v0.2.0 github.com/daixiang0/gci v0.12.3 - github.com/denis-tingaikin/go-header v0.4.3 + github.com/denis-tingaikin/go-header v0.5.0 github.com/esimonov/ifshort v1.0.4 github.com/fatih/color v1.16.0 github.com/firefart/nonamedreturns v1.0.4 diff --git a/go.sum b/go.sum index 9088c3540c62..7f787b4e088c 100644 --- a/go.sum +++ b/go.sum @@ -127,8 +127,8 @@ github.com/daixiang0/gci v0.12.3/go.mod h1:xtHP9N7AHdNvtRNfcx9gwTDfw7FRJx4bZUsiE github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/denis-tingaikin/go-header v0.4.3 h1:tEaZKAlqql6SKCY++utLmkPLd6K8IBM20Ha7UVm+mtU= -github.com/denis-tingaikin/go-header v0.4.3/go.mod h1:0wOCWuN71D5qIgE2nz9KrKmuYBAC2Mra5RassOIQ2/c= +github.com/denis-tingaikin/go-header v0.5.0 h1:SRdnP5ZKvcO9KKRP1KJrhFR3RrlGuD+42t4429eC9k8= +github.com/denis-tingaikin/go-header v0.5.0/go.mod h1:mMenU5bWrok6Wl2UsZjy+1okegmwQ3UgWl4V1D8gjlY= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= diff --git a/pkg/golinters/goheader.go b/pkg/golinters/goheader.go index d3cfefa90b28..5ed5d9d8ded6 100644 --- a/pkg/golinters/goheader.go +++ b/pkg/golinters/goheader.go @@ -97,6 +97,17 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) ([]goanalysi FromLinter: goHeaderName, } + if fix := i.Fix(); fix != nil { + issue.LineRange = &result.Range{ + From: issue.Line(), + To: issue.Line() + len(fix.Actual) - 1, + } + issue.Replacement = &result.Replacement{ + NeedOnlyDelete: len(fix.Expected) == 0, + NewLines: fix.Expected, + } + } + issues = append(issues, goanalysis.NewIssue(&issue, pass)) } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 707a0bd15664..3a972c7a43aa 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -486,6 +486,7 @@ func (m *Manager) GetAllSupportedLinterConfigs() []*linter.Config { linter.NewConfig(golinters.NewGoHeader(goheaderCfg)). WithSince("v1.28.0"). WithPresets(linter.PresetStyle). + WithAutoFix(). WithURL("https://github.com/denis-tingaikin/go-header"), linter.NewConfig(golinters.NewGoimports(goimportsCfg)). diff --git a/test/testdata/configs/go-header-fix.yml b/test/testdata/configs/go-header-fix.yml new file mode 100644 index 000000000000..fa1371e3b1ee --- /dev/null +++ b/test/testdata/configs/go-header-fix.yml @@ -0,0 +1,9 @@ +linters-settings: + goheader: + values: + const: + AUTHOR: The Awesome Project Authors + template: |- + Copyright 2024 {{ AUTHOR }} + + Use of this source code is governed by LICENSE diff --git a/test/testdata/fix/in/go-header_1.go b/test/testdata/fix/in/go-header_1.go new file mode 100644 index 000000000000..7fd59b97a29c --- /dev/null +++ b/test/testdata/fix/in/go-header_1.go @@ -0,0 +1,6 @@ +// Copyright 1999 The Awesome Project Authors + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testdata/fix/in/go-header_2.go b/test/testdata/fix/in/go-header_2.go new file mode 100644 index 000000000000..ed70ffd4ee24 --- /dev/null +++ b/test/testdata/fix/in/go-header_2.go @@ -0,0 +1,8 @@ +/* Copyright 1999 The Awesome Project Authors + +Use of this source code is governed */ + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testdata/fix/in/go-header_3.go b/test/testdata/fix/in/go-header_3.go new file mode 100644 index 000000000000..2961f0d5a9ab --- /dev/null +++ b/test/testdata/fix/in/go-header_3.go @@ -0,0 +1,10 @@ +/* +Copyright 1999 The Awesome + +Use of this source code is governed by LICENSE +*/ + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testdata/fix/out/go-header_1.go b/test/testdata/fix/out/go-header_1.go new file mode 100644 index 000000000000..e8f7529b627c --- /dev/null +++ b/test/testdata/fix/out/go-header_1.go @@ -0,0 +1,8 @@ +// Copyright 2024 The Awesome Project Authors +// +// Use of this source code is governed by LICENSE + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testdata/fix/out/go-header_2.go b/test/testdata/fix/out/go-header_2.go new file mode 100644 index 000000000000..80a336a47a8e --- /dev/null +++ b/test/testdata/fix/out/go-header_2.go @@ -0,0 +1,8 @@ +/* Copyright 2024 The Awesome Project Authors + +Use of this source code is governed by LICENSE */ + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testdata/fix/out/go-header_3.go b/test/testdata/fix/out/go-header_3.go new file mode 100644 index 000000000000..040cad6e8758 --- /dev/null +++ b/test/testdata/fix/out/go-header_3.go @@ -0,0 +1,10 @@ +/* +Copyright 2024 The Awesome Project Authors + +Use of this source code is governed by LICENSE +*/ + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testshared/directives.go b/test/testshared/directives.go index 01c011bb436f..34f37daf3a15 100644 --- a/test/testshared/directives.go +++ b/test/testshared/directives.go @@ -59,7 +59,13 @@ func ParseTestDirectives(tb testing.TB, sourcePath string) *RunContext { continue } - if !strings.HasPrefix(line, "//golangcitest:") { + switch { + case strings.HasPrefix(line, "//golangcitest:"): + // Ok + case !strings.Contains(line, "golangcitest"): + // Assume this is a regular comment (required for go-header tests) + continue + default: require.Failf(tb, "invalid prefix of comment line %s", line) }