1 Star 0 Fork 0

Laomo./golangci-lint

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
astcache.go 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
package astcache
import (
"go/ast"
"go/parser"
"go/token"
"golang.org/x/tools/go/loader"
)
type File struct {
F *ast.File
Fset *token.FileSet
err error
}
type Cache struct {
m map[string]*File
s []*File
}
func (c Cache) GetAllValidFiles() []*File {
return c.s
}
func (c *Cache) prepareValidFiles() {
files := make([]*File, 0, len(c.m))
for _, f := range c.m {
if f.err != nil || f.F == nil {
continue
}
files = append(files, f)
}
c.s = files
}
func LoadFromProgram(prog *loader.Program) *Cache {
c := &Cache{
m: map[string]*File{},
}
for _, pkg := range prog.InitialPackages() {
for _, f := range pkg.Files {
pos := prog.Fset.Position(0)
c.m[pos.Filename] = &File{
F: f,
Fset: prog.Fset,
}
}
}
c.prepareValidFiles()
return c
}
func LoadFromFiles(files []string) *Cache {
c := &Cache{
m: map[string]*File{},
}
fset := token.NewFileSet()
for _, filePath := range files {
f, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments) // comments needed by e.g. golint
c.m[filePath] = &File{
F: f,
Fset: fset,
err: err,
}
}
c.prepareValidFiles()
return c
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LaomoBK/golangci-lint.git
git@gitee.com:LaomoBK/golangci-lint.git
LaomoBK
golangci-lint
golangci-lint
v1.3.5

搜索帮助