From e437bcbb3e235a58003156e8933dca62b932a120 Mon Sep 17 00:00:00 2001 From: Faisal Alam Date: Thu, 29 Jul 2021 16:06:39 +0530 Subject: [PATCH] fix(junitxml-printer): display suggested edit in output --- pkg/printers/junitxml.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/printers/junitxml.go b/pkg/printers/junitxml.go index 9277cd66f2fe3..c72113c22da4c 100644 --- a/pkg/printers/junitxml.go +++ b/pkg/printers/junitxml.go @@ -3,6 +3,7 @@ package printers import ( "context" "encoding/xml" + "fmt" "strings" "github.com/golangci/golangci-lint/pkg/logutils" @@ -41,7 +42,7 @@ func NewJunitXML() *JunitXML { return &JunitXML{} } -func (JunitXML) Print(ctx context.Context, issues []result.Issue) error { +func (j JunitXML) Print(ctx context.Context, issues []result.Issue) error { suites := make(map[string]testSuiteXML) // use a map to group by file for ind := range issues { @@ -52,12 +53,14 @@ func (JunitXML) Print(ctx context.Context, issues []result.Issue) error { testSuite.Tests++ testSuite.Failures++ + content := strings.Join(i.SourceLines, "\n") + content += j.getSuggestedFix(issues[ind]) tc := testCaseXML{ Name: i.FromLinter, ClassName: i.Pos.String(), Failure: failureXML{ Message: i.Text, - Content: strings.Join(i.SourceLines, "\n"), + Content: content, }, } @@ -77,3 +80,23 @@ func (JunitXML) Print(ctx context.Context, issues []result.Issue) error { } return nil } + +func (j JunitXML) getSuggestedFix(i result.Issue) string { + var text string + if len(i.SuggestedFixes) > 0 { + for _, fix := range i.SuggestedFixes { + text += fmt.Sprintf("%s\n", strings.TrimSpace(fix.Message)) + var suggestedEdits []string + for _, textEdit := range fix.TextEdits { + suggestedEdits = append(suggestedEdits, strings.TrimSpace(textEdit.NewText)) + } + text += strings.Join(suggestedEdits, "\n") + "\n" + } + } + + if text != "" { + return fmt.Sprintf("\n\n%s", text) + } + + return "" +}