1 Star 0 Fork 0

lonely/gometalinter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
Alec Thomas 提交于 2017-09-09 13:03 . Update all deps (#344).
// Copyright (c) 2017, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package main // import "mvdan.cc/lint/cmd/metalint"
import (
"flag"
"fmt"
"os"
"strings"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
"mvdan.cc/lint"
"github.com/kisielk/gotool"
interfacer "mvdan.cc/interfacer/check"
unparam "mvdan.cc/unparam/check"
)
var tests = flag.Bool("tests", false, "include tests")
func main() {
flag.Parse()
if err := runLinters(flag.Args()...); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
var linters = [...]struct {
name string
checker lint.Checker
}{
{"unparam", &unparam.Checker{}},
{"interfacer", &interfacer.Checker{}},
}
type metaChecker struct {
wd string
lprog *loader.Program
prog *ssa.Program
}
func runLinters(args ...string) error {
paths := gotool.ImportPaths(args)
wd, err := os.Getwd()
if err != nil {
return err
}
c := &metaChecker{wd: wd}
var conf loader.Config
if _, err := conf.FromArgs(paths, *tests); err != nil {
return err
}
if c.lprog, err = conf.Load(); err != nil {
return err
}
for _, l := range linters {
l.checker.Program(c.lprog)
if ssaChecker, ok := l.checker.(lint.WithSSA); ok {
if c.prog == nil {
c.prog = ssautil.CreateProgram(c.lprog, 0)
c.prog.Build()
}
ssaChecker.ProgramSSA(c.prog)
}
issues, err := l.checker.Check()
if err != nil {
return err
}
c.printIssues(l.name, issues)
}
return nil
}
func (c *metaChecker) printIssues(name string, issues []lint.Issue) {
for _, issue := range issues {
fpos := c.lprog.Fset.Position(issue.Pos()).String()
if strings.HasPrefix(fpos, c.wd) {
fpos = fpos[len(c.wd)+1:]
}
fmt.Printf("%s: %s (%s)\n", fpos, issue.Message(), name)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lonely0422/gometalinter.git
git@gitee.com:lonely0422/gometalinter.git
lonely0422
gometalinter
gometalinter
v2.0.9

搜索帮助