Skip to content

Commit

Permalink
lintcmd: print reference to -explain check to stderr
Browse files Browse the repository at this point in the history
Separate diagnostics from help output. The output of the plain
formatter is supposed to be machine readable, so don't emit text meant
strictly for humans.

For the stylish formatter we could've gone either way, because its
output is not meant to be processed by machines, but we copy the
behavior of the plain formatter for the sake of consistency.
  • Loading branch information
dominikh committed Jun 14, 2020
1 parent 66f0fd3 commit 9cc924a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lintcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,9 @@ func ProcessFlagSet(cs []*analysis.Analyzer, fs *flag.FlagSet) {
var f formatter
switch theFormatter {
case "text":
f = textFormatter{W: os.Stdout}
f = textFormatter{Diagnostics: os.Stdout, UI: os.Stderr}
case "stylish":
f = &stylishFormatter{W: os.Stdout}
f = &stylishFormatter{Diagnostics: os.Stdout, UI: os.Stderr}
case "json":
f = jsonFormatter{W: os.Stdout}
default:
Expand Down
24 changes: 13 additions & 11 deletions lintcmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,19 @@ type documentationMentioner interface {
}

type textFormatter struct {
W io.Writer
Diagnostics io.Writer
UI io.Writer
}

func (o textFormatter) Format(p problem) {
fmt.Fprintf(o.W, "%s: %s\n", relativePositionString(p.Position), p.String())
fmt.Fprintf(o.Diagnostics, "%s: %s\n", relativePositionString(p.Position), p.String())
for _, r := range p.Related {
fmt.Fprintf(o.W, "\t%s: %s\n", relativePositionString(r.Position), r.Message)
fmt.Fprintf(o.Diagnostics, "\t%s: %s\n", relativePositionString(r.Position), r.Message)
}
}

func (o textFormatter) MentionCheckDocumentation(cmd string) {
fmt.Fprintf(o.W, "\nRun '%s -explain <check>' or visit https://staticcheck.io/docs/checks for documentation on checks.\n", cmd)
fmt.Fprintf(o.UI, "\nRun '%s -explain <check>' or visit https://staticcheck.io/docs/checks for documentation on checks.\n", cmd)
}

type jsonFormatter struct {
Expand Down Expand Up @@ -118,7 +119,8 @@ func (o jsonFormatter) Format(p problem) {
}

type stylishFormatter struct {
W io.Writer
Diagnostics io.Writer
UI io.Writer

prevFile string
tw *tabwriter.Writer
Expand All @@ -133,11 +135,11 @@ func (o *stylishFormatter) Format(p problem) {
if pos.Filename != o.prevFile {
if o.prevFile != "" {
o.tw.Flush()
fmt.Fprintln(o.W)
fmt.Fprintln(o.Diagnostics)
}
fmt.Fprintln(o.W, pos.Filename)
fmt.Fprintln(o.Diagnostics, pos.Filename)
o.prevFile = pos.Filename
o.tw = tabwriter.NewWriter(o.W, 0, 4, 2, ' ', 0)
o.tw = tabwriter.NewWriter(o.Diagnostics, 0, 4, 2, ' ', 0)
}
fmt.Fprintf(o.tw, " (%d, %d)\t%s\t%s\n", pos.Line, pos.Column, p.Category, p.Message)
for _, r := range p.Related {
Expand All @@ -146,14 +148,14 @@ func (o *stylishFormatter) Format(p problem) {
}

func (o *stylishFormatter) MentionCheckDocumentation(cmd string) {
textFormatter{W: o.W}.MentionCheckDocumentation(cmd)
textFormatter{UI: o.UI}.MentionCheckDocumentation(cmd)
}

func (o *stylishFormatter) Stats(total, errors, warnings, ignored int) {
if o.tw != nil {
o.tw.Flush()
fmt.Fprintln(o.W)
fmt.Fprintln(o.Diagnostics)
}
fmt.Fprintf(o.W, " ✖ %d problems (%d errors, %d warnings, %d ignored)\n",
fmt.Fprintf(o.Diagnostics, " ✖ %d problems (%d errors, %d warnings, %d ignored)\n",
total, errors, warnings, ignored)
}

0 comments on commit 9cc924a

Please sign in to comment.