-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5499d30
commit fb5bea7
Showing
6 changed files
with
295 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package linters | ||
|
||
import ( | ||
"fmt" | ||
"github.com/qiniu/x/log" | ||
"github.com/qiniu/x/xlog" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func IsEmpty(args ...string) bool { | ||
for _, arg := range args { | ||
if arg != "" { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
type FormatLinterLineFunc func(line string) (*LinterOutput, error) | ||
|
||
func FormatLinterOutput(log *xlog.Logger, output []byte, FormatLinter FormatLinterLineFunc) (map[string][]LinterOutput, error) { | ||
lines := strings.Split(string(output), "\n") | ||
|
||
var result = make(map[string][]LinterOutput) | ||
for _, line := range lines { | ||
if line == "" { | ||
continue | ||
} | ||
output, err := FormatLinter(line) | ||
if err != nil { | ||
log.Warnf("unexpected staticcheck output: %v", line) | ||
// 不直接退出 | ||
continue | ||
} | ||
|
||
if output == nil { | ||
continue | ||
} | ||
|
||
if isGopAutoGeneratedFile(output.File) { | ||
log.Infof("skip auto generated file by go+ : %s", output.File) | ||
continue | ||
} | ||
|
||
if outs, ok := result[output.File]; !ok { | ||
result[output.File] = []LinterOutput{*output} | ||
} else { | ||
// remove duplicate | ||
var existed bool | ||
for _, v := range outs { | ||
if v.File == output.File && v.Line == output.Line && v.Column == output.Column && v.Message == output.Message { | ||
existed = true | ||
break | ||
} | ||
} | ||
|
||
if !existed { | ||
result[output.File] = append(result[output.File], *output) | ||
} | ||
} | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
// common format LinterLine | ||
func FormatLinterLine(line string) (*LinterOutput, error) { | ||
pattern := `^(.*):(\d+):(\d+): (.*)$` | ||
regex, err := regexp.Compile(pattern) | ||
if err != nil { | ||
log.Errorf("compile regex failed: %v", err) | ||
return nil, err | ||
} | ||
matches := regex.FindStringSubmatch(line) | ||
if len(matches) != 5 { | ||
return nil, fmt.Errorf("unexpected format, original: %s", line) | ||
} | ||
|
||
lineNumber, err := strconv.ParseInt(matches[2], 10, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
columnNumber, err := strconv.ParseInt(matches[3], 10, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &LinterOutput{ | ||
File: matches[1], | ||
Line: int(lineNumber), | ||
Column: int(columnNumber), | ||
Message: matches[4], | ||
}, nil | ||
} | ||
|
||
var gop_auto_generated_file_pattern = `^.*_autogen.*.go$` | ||
var gopGeneratedFileRegexp = regexp.MustCompile(gop_auto_generated_file_pattern) | ||
|
||
func isGopAutoGeneratedFile(file string) bool { | ||
// TODO: need a more accurate way to determine whether it is a go+ auto generated file | ||
return gopGeneratedFileRegexp.MatchString(filepath.Base(file)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.