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

82 lines
1.8 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"
"fmt"
2017-08-27 16:57:16 -07:00
"go/token"
)
// 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.")
)
// isGenerated reports whether the source file is generated code
// according the rules from https://golang.org/s/generatedcode.
2018-01-24 15:44:03 -08:00
// This is inherited from the original go lint.
2018-01-21 18:04:41 -08:00
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-08-29 10:47:29 -07:00
// Lint lints a set of files with the specified rule.
2018-01-27 16:37:30 -08:00
func (l *Linter) Lint(filenames []string, ruleSet []Rule, config Config) (<-chan Failure, error) {
rulesConfig := config.Rules
2018-01-23 17:14:23 -08:00
failures := make(chan Failure)
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{},
2018-01-21 18:04:41 -08:00
}
var pkgName string
2017-08-27 16:57:16 -07:00
for _, filename := range filenames {
content, err := l.reader(filename)
if err != nil {
return nil, err
}
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 {
return nil, err
}
2018-01-21 18:04:41 -08:00
if pkgName == "" {
2018-01-21 18:48:51 -08:00
pkgName = file.AST.Name.Name
} else if file.AST.Name.Name != pkgName {
return nil, fmt.Errorf("%s is in package %s, not %s", filename, file.AST.Name.Name, pkgName)
2018-01-21 18:04:41 -08:00
}
2018-01-21 18:29:07 -08:00
pkg.files[filename] = file
}
2018-01-23 17:14:23 -08:00
go (func() {
pkg.lint(ruleSet, rulesConfig, failures)
})()
return failures, nil
2017-09-01 18:36:47 -07:00
}