Skip to content

Commit

Permalink
print with colors the Message and code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Vilgelm authored and Sergey Vilgelm committed Jul 28, 2021
1 parent 08315e7 commit 16a3dfd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 93 deletions.
85 changes: 0 additions & 85 deletions pkg/golinters/goanalysis/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,91 +144,6 @@ func (lnt *Linter) configure() error {
return nil
}

func parseError(srcErr packages.Error) (*result.Issue, error) {
pos, err := libpackages.ParseErrorPosition(srcErr.Pos)
if err != nil {
return nil, err
}

return &result.Issue{
Pos: *pos,
Text: srcErr.Msg,
FromLinter: "typecheck",
}, nil
}

func buildIssuesFromErrorsForTypecheckMode(errs []error, lintCtx *linter.Context) ([]result.Issue, error) {
var issues []result.Issue
uniqReportedIssues := map[string]bool{}
for _, err := range errs {
itErr, ok := errors.Cause(err).(*IllTypedError)
if !ok {
return nil, err
}
for _, err := range libpackages.ExtractErrors(itErr.Pkg) {
i, perr := parseError(err)
if perr != nil { // failed to parse
if uniqReportedIssues[err.Msg] {
continue
}
uniqReportedIssues[err.Msg] = true
lintCtx.Log.Errorf("typechecking error: %s", err.Msg)
} else {
i.Pkg = itErr.Pkg // to save to cache later
issues = append(issues, *i)
}
}
}
return issues, nil
}

func buildIssues(diags []Diagnostic, linterNameBuilder func(diag *Diagnostic) string) []result.Issue {
var issues []result.Issue
for i := range diags {
diag := &diags[i]
linterName := linterNameBuilder(diag)

var text string
if diag.Analyzer.Name == linterName {
text = diag.Message
} else {
text = fmt.Sprintf("%s: %s", diag.Analyzer.Name, diag.Message)
}

var suggestedFixes string
if len(diag.SuggestedFixes) > 0 {
elems := []string{}
for _, fix := range diag.SuggestedFixes {
elems = append(elems, fix.Message)
for _, text := range fix.TextEdits {
elems = append(elems, string(text.NewText))
}
}
suggestedFixes = strings.Join(elems, "\n")
}

issues = append(issues, result.Issue{
FromLinter: linterName,
Text: text,
SuggestedFixes: suggestedFixes,
Pos: diag.Position,
Pkg: diag.Pkg,
})

if len(diag.Related) > 0 {
for _, info := range diag.Related {
issues = append(issues, result.Issue{
FromLinter: linterName,
Text: fmt.Sprintf("%s(related information): %s", diag.Analyzer.Name, info.Message),
Pos: diag.Pkg.Fset.Position(info.Pos),
Pkg: diag.Pkg,
})
}
}
}
return issues
}

func (lnt *Linter) preRun(lintCtx *linter.Context) error {
if err := analysis.Validate(lnt.analyzers); err != nil {
return errors.Wrap(err, "failed to validate analyzers")
Expand Down
9 changes: 5 additions & 4 deletions pkg/golinters/goanalysis/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ func buildIssues(diags []Diagnostic, linterNameBuilder func(diag *Diagnostic) st
}

issues = append(issues, result.Issue{
FromLinter: linterName,
Text: text,
Pos: diag.Position,
Pkg: diag.Pkg,
FromLinter: linterName,
Text: text,
SuggestedFixes: diag.SuggestedFixes,
Pos: diag.Position,
Pkg: diag.Pkg,
})

if len(diag.Related) > 0 {
Expand Down
17 changes: 14 additions & 3 deletions pkg/printers/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,20 @@ func (p Text) printIssue(i *result.Issue) {
}

func (p Text) printSuggestedFixes(i *result.Issue) {
suggestedFixes := strings.TrimSpace(i.SuggestedFixes)
if suggestedFixes != "" {
fmt.Fprintln(logutils.StdOut, suggestedFixes)
var text string
if len(i.SuggestedFixes) > 0 {
for _, fix := range i.SuggestedFixes {
text += p.SprintfColored(color.FgRed, "%s\n", strings.TrimSpace(fix.Message))
elems := []string{}
for _, text := range fix.TextEdits {
elems = append(elems, strings.TrimSpace(string(text.NewText)))
}
text += strings.Join(elems, "\n") + "\n"
}
}

if text != "" {
fmt.Fprintln(logutils.StdOut, text)
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/result/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"go/token"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"
)

Expand All @@ -27,7 +28,7 @@ type InlineFix struct {
type Issue struct {
FromLinter string
Text string
SuggestedFixes string
SuggestedFixes []analysis.SuggestedFix

Severity string

Expand Down

0 comments on commit 16a3dfd

Please sign in to comment.