Denis Isaev 0421bac259 Fix , fix : use go/packages
Use go/packages instead of x/tools/loader: it allows to work
with go modules and speedups loading of packages with the help
of build cache.

A lot of linters became "fast": they are enabled by --fast now and
work in 1-2 seconds. Only unparam, interfacer and megacheck
are "slow" linters now.

Average project is analyzed 20-40% faster than before if all linters are
enabled! If we enable all linters except unparam, interfacer and
megacheck analysis is 10-20x faster!
2018-10-28 17:55:15 +03:00

72 lines
1.6 KiB
Go

package golinters
import (
"context"
"fmt"
"go/ast"
"go/token"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
lintAPI "github.com/golangci/lint-1"
)
type Golint struct{}
func (Golint) Name() string {
return "golint"
}
func (Golint) Desc() string {
return "Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes"
}
func (g Golint) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
var issues []result.Issue
var lintErr error
for _, pkg := range lintCtx.Packages {
files, fset, err := getASTFilesForGoPkg(lintCtx, pkg)
if err != nil {
return nil, err
}
i, err := g.lintPkg(lintCtx.Settings().Golint.MinConfidence, files, fset)
if err != nil {
lintErr = err
continue
}
issues = append(issues, i...)
}
if lintErr != nil {
lintCtx.Log.Warnf("Golint: %s", lintErr)
}
return issues, nil
}
func (g Golint) lintPkg(minConfidence float64, files []*ast.File, fset *token.FileSet) ([]result.Issue, error) {
l := new(lintAPI.Linter)
ps, err := l.LintASTFiles(files, fset)
if err != nil {
return nil, fmt.Errorf("can't lint %d files: %s", len(files), err)
}
if len(ps) == 0 {
return nil, nil
}
issues := make([]result.Issue, 0, len(ps)) // This is worst case
for _, p := range ps {
if p.Confidence >= minConfidence {
issues = append(issues, result.Issue{
Pos: p.Position,
Text: markIdentifiers(p.Text),
FromLinter: g.Name(),
})
// TODO: use p.Link and p.Category
}
}
return issues, nil
}