Skip to content

Commit 34d4b45

Browse files
committed
fix(runner): add suggested edit text from linter in display issue text
The tool did not print suggested edits by the linter when displaying the issues. If there is suggested edits by the linter, it should be displayed along with the issue. Closes #2134
1 parent 245257b commit 34d4b45

File tree

5 files changed

+84
-6
lines changed

5 files changed

+84
-6
lines changed

Diff for: pkg/golinters/goanalysis/issue.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func NewIssue(i *result.Issue, pass *analysis.Pass) Issue {
2323
type EncodingIssue struct {
2424
FromLinter string
2525
Text string
26+
SuggestedFixes []result.SuggestedFix
2627
Pos token.Position
2728
LineRange *result.Range
2829
Replacement *result.Replacement

Diff for: pkg/golinters/goanalysis/runners.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ func buildIssues(diags []Diagnostic, linterNameBuilder func(diag *Diagnostic) st
9898
}
9999

100100
issues = append(issues, result.Issue{
101-
FromLinter: linterName,
102-
Text: text,
103-
Pos: diag.Position,
104-
Pkg: diag.Pkg,
101+
FromLinter: linterName,
102+
Text: text,
103+
SuggestedFixes: result.BuildSuggestedFixes(diag.SuggestedFixes),
104+
Pos: diag.Position,
105+
Pkg: diag.Pkg,
105106
})
106107

107108
if len(diag.Related) > 0 {
@@ -150,6 +151,7 @@ func saveIssuesToCache(allPkgs []*packages.Package, pkgsFromCache map[*packages.
150151
encodedIssues = append(encodedIssues, EncodingIssue{
151152
FromLinter: i.FromLinter,
152153
Text: i.Text,
154+
SuggestedFixes: i.SuggestedFixes,
153155
Pos: i.Pos,
154156
LineRange: i.LineRange,
155157
Replacement: i.Replacement,
@@ -221,6 +223,7 @@ func loadIssuesFromCache(pkgs []*packages.Package, lintCtx *linter.Context,
221223
issues = append(issues, result.Issue{
222224
FromLinter: i.FromLinter,
223225
Text: i.Text,
226+
SuggestedFixes: i.SuggestedFixes,
224227
Pos: i.Pos,
225228
LineRange: i.LineRange,
226229
Replacement: i.Replacement,

Diff for: pkg/printers/tab.go

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"strings"
78
"text/tabwriter"
89

910
"github.com/fatih/color"
@@ -34,6 +35,7 @@ func (p *Tab) Print(ctx context.Context, issues []result.Issue) error {
3435

3536
for i := range issues {
3637
p.printIssue(&issues[i], w)
38+
p.printSuggestedEdits(&issues[i], w)
3739
}
3840

3941
if err := w.Flush(); err != nil {
@@ -56,3 +58,19 @@ func (p Tab) printIssue(i *result.Issue, w io.Writer) {
5658

5759
fmt.Fprintf(w, "%s\t%s\n", pos, text)
5860
}
61+
62+
func (p Tab) printSuggestedEdits(i *result.Issue, w io.Writer) {
63+
var text string
64+
if len(i.SuggestedFixes) > 0 {
65+
for _, fix := range i.SuggestedFixes {
66+
text += p.SprintfColored(color.FgRed, "%s\n", strings.TrimSpace(fix.Message))
67+
var suggestedEdits []string
68+
for _, textEdit := range fix.TextEdits {
69+
suggestedEdits = append(suggestedEdits, strings.TrimSpace(textEdit.NewText))
70+
}
71+
text += strings.Join(suggestedEdits, "\n") + "\n"
72+
}
73+
}
74+
75+
fmt.Fprintln(w, text)
76+
}

Diff for: pkg/printers/text.go

+17
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (p *Text) Print(ctx context.Context, issues []result.Issue) error {
4747

4848
p.printSourceCode(&issues[i])
4949
p.printUnderLinePointer(&issues[i])
50+
p.printSuggestedEdits(&issues[i])
5051
}
5152

5253
return nil
@@ -89,3 +90,19 @@ func (p Text) printUnderLinePointer(i *result.Issue) {
8990

9091
fmt.Fprintf(logutils.StdOut, "%s%s\n", string(prefixRunes), p.SprintfColored(color.FgYellow, "^"))
9192
}
93+
94+
func (p Text) printSuggestedEdits(i *result.Issue) {
95+
var text string
96+
if len(i.SuggestedFixes) > 0 {
97+
for _, fix := range i.SuggestedFixes {
98+
text += p.SprintfColored(color.FgRed, "%s\n", strings.TrimSpace(fix.Message))
99+
var suggestedEdits []string
100+
for _, textEdit := range fix.TextEdits {
101+
suggestedEdits = append(suggestedEdits, strings.TrimSpace(textEdit.NewText))
102+
}
103+
text += strings.Join(suggestedEdits, "\n") + "\n"
104+
}
105+
}
106+
107+
fmt.Fprintln(logutils.StdOut, text)
108+
}

Diff for: pkg/result/issue.go

+41-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"go/token"
77

8+
"golang.org/x/tools/go/analysis"
89
"golang.org/x/tools/go/packages"
910
)
1011

@@ -24,9 +25,47 @@ type InlineFix struct {
2425
NewString string
2526
}
2627

28+
type SuggestedFix struct {
29+
// A description for this suggested fix to be shown to a user deciding
30+
// whether to accept it.
31+
Message string
32+
TextEdits []TextEdit
33+
}
34+
35+
type TextEdit struct {
36+
Pos token.Pos
37+
End token.Pos
38+
NewText string
39+
}
40+
41+
func BuildSuggestedFixes(fixes []analysis.SuggestedFix) []SuggestedFix {
42+
if len(fixes) == 0 {
43+
return nil
44+
}
45+
46+
suggestedFixes := make([]SuggestedFix, 0, len(fixes))
47+
for _, fix := range fixes {
48+
textEdits := make([]TextEdit, 0, len(fix.TextEdits))
49+
for _, edit := range fix.TextEdits {
50+
textEdits = append(textEdits, TextEdit{
51+
Pos: edit.Pos,
52+
End: edit.End,
53+
NewText: string(edit.NewText),
54+
})
55+
}
56+
suggestedFixes = append(suggestedFixes, SuggestedFix{
57+
Message: fix.Message,
58+
TextEdits: textEdits,
59+
})
60+
}
61+
62+
return suggestedFixes
63+
}
64+
2765
type Issue struct {
28-
FromLinter string
29-
Text string
66+
FromLinter string
67+
Text string
68+
SuggestedFixes []SuggestedFix
3069

3170
Severity string
3271

0 commit comments

Comments
 (0)