From 6b89230541b0b796afef239a9361c47440ae21c4 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 18 Dec 2024 07:16:21 +0100 Subject: [PATCH 1/8] fix: NoPos is a begining of a file --- pkg/result/processors/fixer.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pkg/result/processors/fixer.go b/pkg/result/processors/fixer.go index ccab513cfea7..e1efd6ea5ed3 100644 --- a/pkg/result/processors/fixer.go +++ b/pkg/result/processors/fixer.go @@ -10,7 +10,6 @@ import ( "errors" "fmt" "go/format" - "go/token" "os" "slices" @@ -77,7 +76,8 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) { for i := range issues { issue := issues[i] - if issue.SuggestedFixes == nil || skipTextEditWithoutPosition(&issue) { + if issue.SuggestedFixes == nil || skipNoTextEdit(&issue) { + println("Fixing", len(issues), "issues") notFixableIssues = append(notFixableIssues, issue) continue } @@ -197,16 +197,14 @@ func (p Fixer) printStat() { p.sw.PrintStages() } -func skipTextEditWithoutPosition(issue *result.Issue) bool { +func skipNoTextEdit(issue *result.Issue) bool { var onlyMessage int var count int for _, sf := range issue.SuggestedFixes { - for _, edit := range sf.TextEdits { - count++ - if edit.Pos == token.NoPos && edit.End == token.NoPos { - onlyMessage++ - } + if len(sf.TextEdits) == 0 { + onlyMessage++ } + count++ } return count == onlyMessage From 65613f35f18329f233c18fa15548812321b29ed6 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 18 Dec 2024 07:17:03 +0100 Subject: [PATCH 2/8] tests: add a test with a changes not at the first line --- pkg/golinters/goheader/testdata/fix/in/goheader_4.go | 10 ++++++++++ pkg/golinters/goheader/testdata/fix/out/goheader_4.go | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 pkg/golinters/goheader/testdata/fix/in/goheader_4.go create mode 100644 pkg/golinters/goheader/testdata/fix/out/goheader_4.go diff --git a/pkg/golinters/goheader/testdata/fix/in/goheader_4.go b/pkg/golinters/goheader/testdata/fix/in/goheader_4.go new file mode 100644 index 000000000000..4513bacc661d --- /dev/null +++ b/pkg/golinters/goheader/testdata/fix/in/goheader_4.go @@ -0,0 +1,10 @@ +/* + Copyright 2024 The Awesome Project Authors + + Use of this source code is governed by LICENSE.md +*/ + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/goheader-fix.yml +package p diff --git a/pkg/golinters/goheader/testdata/fix/out/goheader_4.go b/pkg/golinters/goheader/testdata/fix/out/goheader_4.go new file mode 100644 index 000000000000..d0e6f8a8ef16 --- /dev/null +++ b/pkg/golinters/goheader/testdata/fix/out/goheader_4.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/goheader-fix.yml +package p From f0e833516cc7202519554d639be93a5cad7ece39 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 18 Dec 2024 07:20:37 +0100 Subject: [PATCH 3/8] fix: use the right position --- pkg/golinters/goheader/goheader.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/golinters/goheader/goheader.go b/pkg/golinters/goheader/goheader.go index 49280365142a..812d365bfb69 100644 --- a/pkg/golinters/goheader/goheader.go +++ b/pkg/golinters/goheader/goheader.go @@ -72,7 +72,14 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error { f := pass.Fset.File(file.Pos()) - start := f.LineStart(issue.Location().Line + 1) + commentLine := 1 + + // 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 { + commentLine = goanalysis.GetFilePositionFor(pass.Fset, file.Comments[0].Pos()).Line + } + + start := f.LineStart(commentLine) diag := analysis.Diagnostic{ Pos: start, From e45ed01b3dc33cc341092add9b64ed25a8fdb271 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 18 Dec 2024 07:21:58 +0100 Subject: [PATCH 4/8] chore: exclude build artifact (non-Go files) --- pkg/golinters/goheader/goheader.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/golinters/goheader/goheader.go b/pkg/golinters/goheader/goheader.go index 812d365bfb69..cca5ef4ac5cb 100644 --- a/pkg/golinters/goheader/goheader.go +++ b/pkg/golinters/goheader/goheader.go @@ -65,6 +65,10 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error { for _, file := range pass.Files { position := goanalysis.GetFilePosition(pass, file) + if !strings.HasSuffix(position.Filename, ".go") { + continue + } + issue := a.Analyze(&goheader.Target{File: file, Path: position.Filename}) if issue == nil { continue From ee07579e6061d95a7b75a351c8c4fef1397afc22 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 18 Dec 2024 07:22:27 +0100 Subject: [PATCH 5/8] fix: adds an extra line between the package and the header --- pkg/golinters/goheader/goheader.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/golinters/goheader/goheader.go b/pkg/golinters/goheader/goheader.go index cca5ef4ac5cb..1d21260a916c 100644 --- a/pkg/golinters/goheader/goheader.go +++ b/pkg/golinters/goheader/goheader.go @@ -91,16 +91,25 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error { } if fix := issue.Fix(); fix != nil { - end := len(fix.Actual) + current := len(fix.Actual) for _, s := range fix.Actual { - end += len(s) + current += len(s) + } + + 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: start + token.Pos(end), - NewText: []byte(strings.Join(fix.Expected, "\n") + "\n"), + End: end, + NewText: []byte(header), }}, }} } From 03b6d1a673202fa95d5eccedeaf601a92d80703c Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 18 Dec 2024 07:28:27 +0100 Subject: [PATCH 6/8] chore: use first divergence as a display position --- pkg/golinters/goheader/goheader.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/golinters/goheader/goheader.go b/pkg/golinters/goheader/goheader.go index 1d21260a916c..395e5bb20598 100644 --- a/pkg/golinters/goheader/goheader.go +++ b/pkg/golinters/goheader/goheader.go @@ -76,17 +76,8 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error { f := pass.Fset.File(file.Pos()) - commentLine := 1 - - // 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 { - commentLine = goanalysis.GetFilePositionFor(pass.Fset, file.Comments[0].Pos()).Line - } - - start := f.LineStart(commentLine) - diag := analysis.Diagnostic{ - Pos: start, + Pos: f.LineStart(issue.Location().Line+1) + token.Pos(issue.Location().Position), // The position of the first divergence. Message: issue.Message(), } @@ -96,6 +87,15 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error { current += len(s) } + commentLine := 1 + + // 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 { + commentLine = goanalysis.GetFilePositionFor(pass.Fset, file.Comments[0].Pos()).Line + } + + start := f.LineStart(commentLine) + end := start + token.Pos(current) header := strings.Join(fix.Expected, "\n") + "\n" From 91bd9a789afdc9997be28fa733592553711ee2c7 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 18 Dec 2024 08:10:10 +0100 Subject: [PATCH 7/8] fix: add offest with // comment --- pkg/golinters/goheader/goheader.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pkg/golinters/goheader/goheader.go b/pkg/golinters/goheader/goheader.go index 395e5bb20598..63a45d46fae2 100644 --- a/pkg/golinters/goheader/goheader.go +++ b/pkg/golinters/goheader/goheader.go @@ -76,8 +76,20 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error { 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 are "//" there is a one character offset. + offset = 1 + } + commentLine = goanalysis.GetFilePositionFor(pass.Fset, file.Comments[0].Pos()).Line + } + diag := analysis.Diagnostic{ - Pos: f.LineStart(issue.Location().Line+1) + token.Pos(issue.Location().Position), // The position of the first divergence. + Pos: f.LineStart(issue.Location().Line+1) + token.Pos(issue.Location().Position-offset), // The position of the first divergence. Message: issue.Message(), } @@ -87,13 +99,6 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error { current += len(s) } - commentLine := 1 - - // 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 { - commentLine = goanalysis.GetFilePositionFor(pass.Fset, file.Comments[0].Pos()).Line - } - start := f.LineStart(commentLine) end := start + token.Pos(current) From 38ef65affe236b6ca52ca9f0de2da3146520eda7 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 18 Dec 2024 13:46:59 +0100 Subject: [PATCH 8/8] review --- pkg/result/processors/fixer.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pkg/result/processors/fixer.go b/pkg/result/processors/fixer.go index e1efd6ea5ed3..67b603a09bc0 100644 --- a/pkg/result/processors/fixer.go +++ b/pkg/result/processors/fixer.go @@ -77,7 +77,6 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) { issue := issues[i] if issue.SuggestedFixes == nil || skipNoTextEdit(&issue) { - println("Fixing", len(issues), "issues") notFixableIssues = append(notFixableIssues, issue) continue } @@ -199,15 +198,13 @@ func (p Fixer) printStat() { func skipNoTextEdit(issue *result.Issue) bool { var onlyMessage int - var count int for _, sf := range issue.SuggestedFixes { if len(sf.TextEdits) == 0 { onlyMessage++ } - count++ } - return count == onlyMessage + return len(issue.SuggestedFixes) == onlyMessage } // validateEdits returns a list of edits that is sorted and