1
0
mirror of https://github.com/mgechev/revive.git synced 2025-04-23 11:58:51 +02:00

Use goroutines

This commit is contained in:
mgechev 2018-01-23 17:14:23 -08:00
parent 1dc6e7cabd
commit 5c6de19f89
6 changed files with 27 additions and 15 deletions

@ -33,16 +33,17 @@ func formatFailure(failure linter.Failure) []string {
} }
// Format formats the failures gotten from the linter. // Format formats the failures gotten from the linter.
func (f *CLIFormatter) Format(failures []linter.Failure) (string, error) { func (f *CLIFormatter) Format(failures <-chan linter.Failure) (string, error) {
var result [][]string var result [][]string
var totalErrors = 0 var totalErrors = 0
for _, f := range failures { var total = 0
for f := range failures {
result = append(result, formatFailure(f)) result = append(result, formatFailure(f))
total++
if f.Type == linter.FailureTypeError { if f.Type == linter.FailureTypeError {
totalErrors++ totalErrors++
} }
} }
total := len(failures)
ps := "problems" ps := "problems"
if total == 1 { if total == 1 {
ps = "problem" ps = "problem"

@ -13,7 +13,7 @@ type JSONFormatter struct {
} }
// Format formats the failures gotten from the linter. // Format formats the failures gotten from the linter.
func (f *JSONFormatter) Format(failures []linter.Failure) (string, error) { func (f *JSONFormatter) Format(failures <-chan linter.Failure) (string, error) {
result, error := json.Marshal(failures) result, error := json.Marshal(failures)
if error != nil { if error != nil {
return "", error return "", error

@ -84,8 +84,7 @@ func (f *File) isMain() bool {
return false return false
} }
func (f *File) lint(rules []Rule, rulesConfig RulesConfig) []Failure { func (f *File) lint(rules []Rule, rulesConfig RulesConfig, failures chan Failure) {
var failures []Failure
disabledIntervals := f.disabledIntervals(rules) disabledIntervals := f.disabledIntervals(rules)
for _, currentRule := range rules { for _, currentRule := range rules {
config := rulesConfig[currentRule.Name()] config := rulesConfig[currentRule.Name()]
@ -100,9 +99,10 @@ func (f *File) lint(rules []Rule, rulesConfig RulesConfig) []Failure {
currentFailures[idx] = failure currentFailures[idx] = failure
} }
currentFailures = f.filterFailures(currentFailures, disabledIntervals) currentFailures = f.filterFailures(currentFailures, disabledIntervals)
failures = append(failures, currentFailures...) for _, failure := range currentFailures {
failures <- failure
}
} }
return failures
} }
type enableDisableConfig struct { type enableDisableConfig struct {

@ -9,5 +9,5 @@ type FormatterMetadata struct {
// Formatter defines an interface for failure formatters // Formatter defines an interface for failure formatters
type Formatter interface { type Formatter interface {
Format([]Failure) string Format(<-chan Failure) string
} }

@ -42,7 +42,8 @@ func isGenerated(src []byte) bool {
} }
// Lint lints a set of files with the specified rule. // Lint lints a set of files with the specified rule.
func (l *Linter) Lint(filenames []string, ruleSet []Rule, rulesConfig RulesConfig) ([]Failure, error) { func (l *Linter) Lint(filenames []string, ruleSet []Rule, rulesConfig RulesConfig) (<-chan Failure, error) {
failures := make(chan Failure)
pkg := &Package{ pkg := &Package{
fset: token.NewFileSet(), fset: token.NewFileSet(),
files: map[string]*File{}, files: map[string]*File{},
@ -71,5 +72,9 @@ func (l *Linter) Lint(filenames []string, ruleSet []Rule, rulesConfig RulesConfi
pkg.files[filename] = file pkg.files[filename] = file
} }
return pkg.lint(ruleSet, rulesConfig), nil go (func() {
pkg.lint(ruleSet, rulesConfig, failures)
})()
return failures, nil
} }

@ -4,6 +4,7 @@ import (
"go/ast" "go/ast"
"go/token" "go/token"
"go/types" "go/types"
"sync"
"golang.org/x/tools/go/gcexportdata" "golang.org/x/tools/go/gcexportdata"
) )
@ -84,11 +85,16 @@ func (p *Package) TypeOf(expr ast.Expr) types.Type {
return p.TypesInfo.TypeOf(expr) return p.TypesInfo.TypeOf(expr)
} }
func (p *Package) lint(rules []Rule, config RulesConfig) []Failure { func (p *Package) lint(rules []Rule, config RulesConfig, failures chan Failure) {
var failures []Failure
p.TypeCheck() p.TypeCheck()
var wg sync.WaitGroup
for _, file := range p.files { for _, file := range p.files {
failures = append(failures, file.lint(rules, config)...) wg.Add(1)
go (func(file *File) {
file.lint(rules, config, failures)
defer wg.Done()
})(file)
} }
return failures wg.Wait()
close(failures)
} }