Denis Isaev 2b587b63d6
Load AST for fast linters in different way.
Use build.Import instead of manual parser.ParseFile and paths traversal. It allows:
1. support build tags for all linters.
2. analyze files only for current GOOS/GOARCH: less false-positives.
3. analyze xtest packages (*_test) by golint: upstream golint and
gometalinter can't do it! And don't break analysis on the first xtest
package like it was before.
4. proper handling of xtest packages for linters like goconst where
package boundary is important: less false-positives is expected.

Also:
1. reuse AST parsing for golint and goconst: minor speedup.
2. allow to specify path (not only name) regexp for --skip-files and
--skip-dirs
3. add more default exclude filters for golint about commits:
`(comment on exported (method|function)|should have( a package)?
    comment|comment should be of the form)`
4. print skipped dir in verbose (-v) mode
5. refactor per-linter tests: declare arguments in comments, run only
one linter and in combination with slow linter
2018-06-10 23:46:24 +03:00

73 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/logutils"
"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.PkgProgram.Packages() {
files, fset, err := getASTFilesForPkg(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 {
logutils.HiddenWarnf("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: p.Text,
FromLinter: g.Name(),
})
// TODO: use p.Link and p.Category
}
}
return issues, nil
}