This commit is contained in:
golangci 2018-05-08 13:33:00 +03:00
parent c9d006d77a
commit 6f384926cf
8 changed files with 34 additions and 9 deletions

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
test:
go test -v ./...

View File

@ -37,6 +37,7 @@ func (e *Executor) initRun() {
config.OutFormatColoredLineNumber,
fmt.Sprintf("Format of output: %s", strings.Join(config.OutFormats, "|")))
runCmd.Flags().BoolVar(&rc.PrintIssuedLine, "print-issued-lines", true, "Print lines of code with issue")
runCmd.Flags().BoolVar(&rc.PrintLinterName, "print-linter-name", true, "Print linter name in issue line")
runCmd.Flags().IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code",
1, "Exit code when issues were found")
@ -53,7 +54,7 @@ func (e *Executor) initRun() {
runCmd.Flags().BoolVar(&rc.Gofmt.Simplify, "gofmt.simplify", true, "Gofmt: simplify code")
runCmd.Flags().IntVar(&rc.Gocyclo.MinComplexity, "gocyclo.min-complexity",
50, "Minimal complexity of function to report it")
30, "Minimal complexity of function to report it")
runCmd.Flags().BoolVar(&rc.Structcheck.CheckExportedFields, "structcheck.exported-fields", false, "Structcheck: report about unused exported struct fields")
runCmd.Flags().BoolVar(&rc.Varcheck.CheckExportedFields, "varcheck.exported-fields", false, "Varcheck: report about unused exported variables")
@ -240,7 +241,8 @@ func (e *Executor) executeRun(cmd *cobra.Command, args []string) {
if e.cfg.Run.OutFormat == config.OutFormatJSON {
p = printers.NewJSON()
} else {
p = printers.NewText(e.cfg.Run.PrintIssuedLine, e.cfg.Run.OutFormat == config.OutFormatColoredLineNumber)
p = printers.NewText(e.cfg.Run.PrintIssuedLine,
e.cfg.Run.OutFormat == config.OutFormatColoredLineNumber, e.cfg.Run.PrintLinterName)
}
gotAnyIssues, err := p.Print(issues)
if err != nil {

View File

@ -35,6 +35,7 @@ type Run struct { // nolint:maligned
OutFormat string
PrintIssuedLine bool
PrintLinterName bool
ExitCodeIfIssuesFound int

View File

@ -49,7 +49,14 @@ func installBinary(t *testing.T) {
func testOneSource(t *testing.T, sourcePath string) {
goErrchkBin := filepath.Join(runtime.GOROOT(), "test", "errchk")
cmd := exec.Command(goErrchkBin, binName, "run", "--enable-all", "--gocyclo.min-complexity", "20", sourcePath)
cmd := exec.Command(goErrchkBin, binName, "run",
"--enable-all",
"--dupl.threshold=20",
"--gocyclo.min-complexity=20",
"--print-issued-lines=false",
"--print-linter-name=false",
"--out-format=line-number",
sourcePath)
runGoErrchk(cmd, t)
}

View File

@ -34,7 +34,8 @@ func (p ProjectPaths) MixedPaths() []string {
func (p ProjectPaths) FilesGrouppedByDirs() [][]string {
dirToFiles := map[string][]string{}
for _, f := range p.Files {
dirToFiles[filepath.Dir(f)] = append(dirToFiles[f], f)
dir := filepath.Dir(f)
dirToFiles[dir] = append(dirToFiles[dir], f)
}
ret := [][]string{}

View File

@ -18,7 +18,7 @@ func (Golint) Name() string {
func (g Golint) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) {
var issues []result.Issue
for _, pkgFiles := range lintCtx.Paths.FilesGrouppedByDirs() {
i, err := lintFiles(lintCtx.RunCfg().Golint.MinConfidence, pkgFiles...)
i, err := g.lintFiles(lintCtx.RunCfg().Golint.MinConfidence, pkgFiles...)
if err != nil {
// TODO: skip and warn
return nil, fmt.Errorf("can't lint files %s: %s", lintCtx.Paths.Files, err)
@ -29,7 +29,7 @@ func (g Golint) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, erro
return issues, nil
}
func lintFiles(minConfidence float64, filenames ...string) ([]result.Issue, error) {
func (g Golint) lintFiles(minConfidence float64, filenames ...string) ([]result.Issue, error) {
files := make(map[string][]byte)
for _, filename := range filenames {
src, err := ioutil.ReadFile(filename)
@ -51,6 +51,7 @@ func lintFiles(minConfidence float64, filenames ...string) ([]result.Issue, erro
issues = append(issues, result.Issue{
Pos: p.Position,
Text: p.Text,
FromLinter: g.Name(),
})
// TODO: use p.Link and p.Category
}

View File

@ -14,12 +14,14 @@ import (
type Text struct {
printIssuedLine bool
useColors bool
printLinterName bool
}
func NewText(printIssuedLine bool, useColors bool) *Text {
func NewText(printIssuedLine, useColors, printLinterName bool) *Text {
return &Text{
printIssuedLine: printIssuedLine,
useColors: useColors,
printLinterName: printLinterName,
}
}
@ -47,6 +49,9 @@ func (p Text) Print(issues chan result.Issue) (bool, error) {
for i := range issues {
gotAnyIssue = true
text := p.SprintfColored(color.FgRed, "%s", i.Text)
if p.printLinterName {
text += fmt.Sprintf(" (%s)", i.FromLinter)
}
pos := p.SprintfColored(color.Bold, "%s:%d", i.FilePath(), i.Line())
fmt.Fprintf(out, "%s: %s\n", pos, text)

View File

@ -13,3 +13,9 @@ type ExportedInterfaceWithNoComment interface{}
// Bad comment // ERROR "comment on exported function ExportedFuncWithBadComment should be of the form .ExportedFuncWithBadComment \.\.\.."
func ExportedFuncWithBadComment() {}
type GolintTest struct{}
func (receiver1 GolintTest) A() {}
func (receiver2 GolintTest) B() {} // ERROR "receiver name receiver2 should be consistent with previous receiver name receiver1 for GolintTest"