Isaev Denis 95ec0cf21e
dramatically reduce memory usage ()
Run all linters per package. It allows unloading package data when it's
processed. It dramatically reduces memory (and CPU because of GC) usage.

Relates: 
2019-09-30 16:19:41 +03:00

72 lines
1.7 KiB
Go

package golinters
import (
"sync"
gofmtAPI "github.com/golangci/gofmt/gofmt"
"github.com/pkg/errors"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)
const gofmtName = "gofmt"
func NewGofmt() *goanalysis.Linter {
var mu sync.Mutex
var resIssues []result.Issue
analyzer := &analysis.Analyzer{
Name: goanalysis.TheOnlyAnalyzerName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
gofmtName,
"Gofmt checks whether code was gofmt-ed. By default "+
"this tool runs with -s option to check for code simplification",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
fileNames = append(fileNames, pos.Filename)
}
var issues []result.Issue
for _, f := range fileNames {
diff, err := gofmtAPI.Run(f, lintCtx.Settings().Gofmt.Simplify)
if err != nil { // TODO: skip
return nil, err
}
if diff == nil {
continue
}
is, err := extractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, false)
if err != nil {
return nil, errors.Wrapf(err, "can't extract issues from gofmt diff output %q", string(diff))
}
issues = append(issues, is...)
}
if len(issues) == 0 {
return nil, nil
}
mu.Lock()
resIssues = append(resIssues, issues...)
mu.Unlock()
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []result.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}