1 Star 0 Fork 0

Laomo. / golangci-lint

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
typecheck.go 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
package golinters
import (
"context"
"go/token"
"strconv"
"strings"
"github.com/golangci/golangci-lint/pkg/result"
)
type TypeCheck struct{}
func (TypeCheck) Name() string {
return "typecheck"
}
func (TypeCheck) Desc() string {
return "Like the front-end of a Go compiler, parses and type-checks Go code"
}
func (lint TypeCheck) parseError(err error) *result.Issue {
// file:line(<optional>:colon): message
parts := strings.Split(err.Error(), ":")
if len(parts) < 3 {
return nil
}
file := parts[0]
line, err := strconv.Atoi(parts[1])
if err != nil {
return nil
}
var column int
var message string
if len(parts) == 3 { // no column
message = parts[2]
} else {
column, err = strconv.Atoi(parts[2])
if err == nil { // column was parsed
message = strings.Join(parts[3:], ":")
} else {
message = strings.Join(parts[2:], ":")
}
}
message = strings.TrimSpace(message)
if message == "" {
return nil
}
return &result.Issue{
Pos: token.Position{
Filename: file,
Line: line,
Column: column,
},
Text: message,
FromLinter: lint.Name(),
}
}
func (lint TypeCheck) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) {
var res []result.Issue
for _, pkg := range lintCtx.Program.InitialPackages() {
for _, err := range pkg.Errors {
i := lint.parseError(err)
if i != nil {
res = append(res, *i)
}
}
}
return res, nil
}
1
https://gitee.com/LaomoBK/golangci-lint.git
git@gitee.com:LaomoBK/golangci-lint.git
LaomoBK
golangci-lint
golangci-lint
v1.3.5

搜索帮助

53164aa7 5694891 3bd8fe86 5694891