1
0
mirror of https://github.com/mgechev/revive.git synced 2024-12-12 10:44:59 +02:00
revive/linter/linter.go
2017-08-27 20:53:12 -07:00

46 lines
992 B
Go

package linter
import (
"go/token"
"github.com/mgechev/golinter/file"
"github.com/mgechev/golinter/rules"
)
// ReadFile defines an abstraction for reading files.
type ReadFile func(path string) (result []byte, err error)
// Linter is used for lintign set of files.
type Linter struct {
reader ReadFile
}
// New creates a new Linter
func New(reader ReadFile) Linter {
return Linter{reader: reader}
}
// Lint lints a set of files with the specified rules.
func (l *Linter) Lint(filenames []string, ruleSet []rules.Rule) ([]rules.Failure, error) {
var fileSet token.FileSet
var failures []rules.Failure
for _, filename := range filenames {
content, err := l.reader(filename)
if err != nil {
return nil, err
}
file, err := file.New(filename, content, &fileSet)
if err != nil {
return nil, err
}
for _, rule := range ruleSet {
currentFailures := rule.Apply(file, []string{})
failures = append(failures, currentFailures...)
}
}
return failures, nil
}