Skip to content

Commit 08315e7

Browse files
Sergey VilgelmSergey Vilgelm
Sergey Vilgelm
authored and
Sergey Vilgelm
committed
Printing out SuggestedFixes
1 parent 245257b commit 08315e7

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

pkg/golinters/goanalysis/linter.go

+85
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,91 @@ func (lnt *Linter) configure() error {
144144
return nil
145145
}
146146

147+
func parseError(srcErr packages.Error) (*result.Issue, error) {
148+
pos, err := libpackages.ParseErrorPosition(srcErr.Pos)
149+
if err != nil {
150+
return nil, err
151+
}
152+
153+
return &result.Issue{
154+
Pos: *pos,
155+
Text: srcErr.Msg,
156+
FromLinter: "typecheck",
157+
}, nil
158+
}
159+
160+
func buildIssuesFromErrorsForTypecheckMode(errs []error, lintCtx *linter.Context) ([]result.Issue, error) {
161+
var issues []result.Issue
162+
uniqReportedIssues := map[string]bool{}
163+
for _, err := range errs {
164+
itErr, ok := errors.Cause(err).(*IllTypedError)
165+
if !ok {
166+
return nil, err
167+
}
168+
for _, err := range libpackages.ExtractErrors(itErr.Pkg) {
169+
i, perr := parseError(err)
170+
if perr != nil { // failed to parse
171+
if uniqReportedIssues[err.Msg] {
172+
continue
173+
}
174+
uniqReportedIssues[err.Msg] = true
175+
lintCtx.Log.Errorf("typechecking error: %s", err.Msg)
176+
} else {
177+
i.Pkg = itErr.Pkg // to save to cache later
178+
issues = append(issues, *i)
179+
}
180+
}
181+
}
182+
return issues, nil
183+
}
184+
185+
func buildIssues(diags []Diagnostic, linterNameBuilder func(diag *Diagnostic) string) []result.Issue {
186+
var issues []result.Issue
187+
for i := range diags {
188+
diag := &diags[i]
189+
linterName := linterNameBuilder(diag)
190+
191+
var text string
192+
if diag.Analyzer.Name == linterName {
193+
text = diag.Message
194+
} else {
195+
text = fmt.Sprintf("%s: %s", diag.Analyzer.Name, diag.Message)
196+
}
197+
198+
var suggestedFixes string
199+
if len(diag.SuggestedFixes) > 0 {
200+
elems := []string{}
201+
for _, fix := range diag.SuggestedFixes {
202+
elems = append(elems, fix.Message)
203+
for _, text := range fix.TextEdits {
204+
elems = append(elems, string(text.NewText))
205+
}
206+
}
207+
suggestedFixes = strings.Join(elems, "\n")
208+
}
209+
210+
issues = append(issues, result.Issue{
211+
FromLinter: linterName,
212+
Text: text,
213+
SuggestedFixes: suggestedFixes,
214+
Pos: diag.Position,
215+
Pkg: diag.Pkg,
216+
})
217+
218+
if len(diag.Related) > 0 {
219+
for _, info := range diag.Related {
220+
issues = append(issues, result.Issue{
221+
FromLinter: linterName,
222+
Text: fmt.Sprintf("%s(related information): %s", diag.Analyzer.Name, info.Message),
223+
Pos: diag.Pkg.Fset.Position(info.Pos),
224+
Pkg: diag.Pkg,
225+
})
226+
}
227+
}
228+
}
229+
return issues
230+
}
231+
147232
func (lnt *Linter) preRun(lintCtx *linter.Context) error {
148233
if err := analysis.Validate(lnt.analyzers); err != nil {
149234
return errors.Wrap(err, "failed to validate analyzers")

pkg/printers/text.go

+8
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.printSuggestedFixes(&issues[i])
5051
}
5152

5253
return nil
@@ -64,6 +65,13 @@ func (p Text) printIssue(i *result.Issue) {
6465
fmt.Fprintf(logutils.StdOut, "%s: %s\n", pos, text)
6566
}
6667

68+
func (p Text) printSuggestedFixes(i *result.Issue) {
69+
suggestedFixes := strings.TrimSpace(i.SuggestedFixes)
70+
if suggestedFixes != "" {
71+
fmt.Fprintln(logutils.StdOut, suggestedFixes)
72+
}
73+
}
74+
6775
func (p Text) printSourceCode(i *result.Issue) {
6876
for _, line := range i.SourceLines {
6977
fmt.Fprintln(logutils.StdOut, line)

pkg/result/issue.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ type InlineFix struct {
2525
}
2626

2727
type Issue struct {
28-
FromLinter string
29-
Text string
28+
FromLinter string
29+
Text string
30+
SuggestedFixes string
3031

3132
Severity string
3233

0 commit comments

Comments
 (0)