golangci-lint/pkg/golinters/typecheck.go
Denis Isaev 0421bac259 Fix #237, fix #178: 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

76 lines
1.6 KiB
Go

package golinters
import (
"context"
"fmt"
"go/token"
"strconv"
"strings"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
)
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(srcErr packages.Error) (*result.Issue, error) {
// file:line(<optional>:colon)
parts := strings.Split(srcErr.Pos, ":")
if len(parts) == 1 {
return nil, errors.New("no colons")
}
file := parts[0]
line, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("can't parse line number %q: %s", parts[1], err)
}
var column int
if len(parts) == 3 { // no column
column, err = strconv.Atoi(parts[2])
if err != nil {
return nil, errors.Wrapf(err, "failed to parse column from %q", parts[2])
}
}
return &result.Issue{
Pos: token.Position{
Filename: file,
Line: line,
Column: column,
},
Text: srcErr.Msg,
FromLinter: lint.Name(),
}, nil
}
func (lint TypeCheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
var res []result.Issue
for _, pkg := range lintCtx.NotCompilingPackages {
for _, err := range pkg.Errors {
i, perr := lint.parseError(err)
if perr != nil {
res = append(res, result.Issue{
Text: err.Msg,
FromLinter: lint.Name(),
})
} else {
res = append(res, *i)
}
}
}
return res, nil
}