1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-24 03:47:45 +02:00
revive/lint/linter.go

100 lines
2.0 KiB
Go
Raw Normal View History

2018-01-24 15:44:03 -08:00
package lint
2017-08-27 16:57:16 -07:00
import (
2018-01-21 18:04:41 -08:00
"bufio"
"bytes"
2018-05-31 19:42:58 -07:00
"fmt"
2017-08-27 16:57:16 -07:00
"go/token"
2018-05-31 19:42:58 -07:00
"os"
"sync"
2017-08-27 16:57:16 -07:00
)
// ReadFile defines an abstraction for reading files.
type ReadFile func(path string) (result []byte, err error)
2018-01-21 18:04:41 -08:00
type disabledIntervalsMap = map[string][]DisabledInterval
2017-09-01 21:46:54 -07:00
// Linter is used for linting set of files.
2017-08-27 16:57:16 -07:00
type Linter struct {
reader ReadFile
}
// New creates a new Linter
func New(reader ReadFile) Linter {
return Linter{reader: reader}
}
2018-01-21 18:04:41 -08:00
var (
genHdr = []byte("// Code generated ")
genFtr = []byte(" DO NOT EDIT.")
)
2018-05-31 19:42:58 -07:00
// Lint lints a set of files with the specified rule.
func (l *Linter) Lint(packages [][]string, ruleSet []Rule, config Config) (<-chan Failure, error) {
failures := make(chan Failure)
var wg sync.WaitGroup
for _, pkg := range packages {
wg.Add(1)
go func(pkg []string) {
if err := l.lintPackage(pkg, ruleSet, config, failures); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer wg.Done()
}(pkg)
2018-01-21 18:04:41 -08:00
}
2018-05-31 19:42:58 -07:00
go func() {
wg.Wait()
close(failures)
}()
return failures, nil
2018-01-21 18:04:41 -08:00
}
2018-05-31 19:42:58 -07:00
func (l *Linter) lintPackage(filenames []string, ruleSet []Rule, config Config, failures chan Failure) error {
2018-01-21 18:04:41 -08:00
pkg := &Package{
2018-01-21 18:29:07 -08:00
fset: token.NewFileSet(),
files: map[string]*File{},
mu: sync.Mutex{},
2018-01-21 18:04:41 -08:00
}
2017-08-27 16:57:16 -07:00
for _, filename := range filenames {
content, err := l.reader(filename)
if err != nil {
2018-05-31 19:42:58 -07:00
return err
2017-08-27 16:57:16 -07:00
}
2018-01-27 16:37:30 -08:00
if isGenerated(content) && !config.IgnoreGeneratedHeader {
2018-01-21 18:04:41 -08:00
continue
}
2017-08-27 16:57:16 -07:00
2018-01-21 18:04:41 -08:00
file, err := NewFile(filename, content, pkg)
2017-08-27 16:57:16 -07:00
if err != nil {
2018-05-31 19:42:58 -07:00
return err
2017-08-27 16:57:16 -07:00
}
2018-01-21 18:29:07 -08:00
pkg.files[filename] = file
}
2018-05-31 19:42:58 -07:00
if len(pkg.files) == 0 {
return nil
}
2018-01-23 17:14:23 -08:00
2018-05-31 19:42:58 -07:00
pkg.lint(ruleSet, config, failures)
return nil
}
// isGenerated reports whether the source file is generated code
// according the rules from https://golang.org/s/generatedcode.
// This is inherited from the original go lint.
func isGenerated(src []byte) bool {
sc := bufio.NewScanner(bytes.NewReader(src))
for sc.Scan() {
b := sc.Bytes()
if bytes.HasPrefix(b, genHdr) && bytes.HasSuffix(b, genFtr) && len(b) >= len(genHdr)+len(genFtr) {
return true
}
}
return false
2017-09-01 18:36:47 -07:00
}