1
0
mirror of https://github.com/mgechev/revive.git synced 2025-03-25 21:29:16 +02:00
revive/lint/linter.go

253 lines
5.9 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"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
2018-05-31 19:42:58 -07:00
"sync"
goversion "github.com/hashicorp/go-version"
"golang.org/x/mod/modfile"
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
fileReadTokens chan struct{}
2017-08-27 16:57:16 -07:00
}
// New creates a new Linter
func New(reader ReadFile, maxOpenFiles int) Linter {
var fileReadTokens chan struct{}
if maxOpenFiles > 0 {
fileReadTokens = make(chan struct{}, maxOpenFiles)
}
return Linter{
reader: reader,
fileReadTokens: fileReadTokens,
}
}
func (l Linter) readFile(path string) (result []byte, err error) {
if l.fileReadTokens != nil {
// "take" a token by writing to the channel.
// It will block if no more space in the channel's buffer
l.fileReadTokens <- struct{}{}
defer func() {
// "free" a token by reading from the channel
<-l.fileReadTokens
}()
}
return l.reader(path)
2017-08-27 16:57:16 -07:00
}
2018-01-21 18:04:41 -08:00
var (
genHdr = []byte("// Code generated ")
genFtr = []byte(" DO NOT EDIT.")
defaultGoVersion = goversion.Must(goversion.NewVersion("1.0"))
2018-01-21 18:04:41 -08:00
)
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)
perModVersions := make(map[string]*goversion.Version)
perPkgVersions := make([]*goversion.Version, len(packages))
for n, files := range packages {
if len(files) == 0 {
continue
}
if config.GoVersion != nil {
perPkgVersions[n] = config.GoVersion
continue
}
dir, err := filepath.Abs(filepath.Dir(files[0]))
if err != nil {
return nil, err
}
alreadyKnownMod := false
for d, v := range perModVersions {
if strings.HasPrefix(dir, d) {
perPkgVersions[n] = v
alreadyKnownMod = true
break
}
}
if alreadyKnownMod {
continue
}
d, v, err := detectGoMod(dir)
if err != nil {
// No luck finding the go.mod file thus set the default Go version
v = defaultGoVersion
d = dir
}
perModVersions[d] = v
perPkgVersions[n] = v
}
2018-05-31 19:42:58 -07:00
var wg sync.WaitGroup
for n := range packages {
2018-05-31 19:42:58 -07:00
wg.Add(1)
go func(pkg []string, gover *goversion.Version) {
if err := l.lintPackage(pkg, gover, ruleSet, config, failures); err != nil {
2018-05-31 19:42:58 -07:00
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer wg.Done()
}(packages[n], perPkgVersions[n])
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
}
func (l *Linter) lintPackage(filenames []string, gover *goversion.Version, ruleSet []Rule, config Config, failures chan Failure) error {
if len(filenames) == 0 {
return nil
}
2018-01-21 18:04:41 -08:00
pkg := &Package{
fset: token.NewFileSet(),
files: map[string]*File{},
goVersion: gover,
2018-01-21 18:04:41 -08:00
}
2017-08-27 16:57:16 -07:00
for _, filename := range filenames {
content, err := l.readFile(filename)
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
}
if !config.IgnoreGeneratedHeader && isGenerated(content) {
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 {
addInvalidFileFailure(filename, err.Error(), failures)
continue
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
}
func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error) {
modFileName, err := retrieveModFile(dir)
if err != nil {
return "", nil, fmt.Errorf("%q doesn't seem to be part of a Go module", dir)
}
mod, err := os.ReadFile(modFileName)
if err != nil {
return "", nil, fmt.Errorf("failed to read %q, got %v", modFileName, err)
}
modAst, err := modfile.ParseLax(modFileName, mod, nil)
if err != nil {
return "", nil, fmt.Errorf("failed to parse %q, got %v", modFileName, err)
}
ver, err = goversion.NewVersion(modAst.Go.Version)
return path.Dir(modFileName), ver, err
}
func retrieveModFile(dir string) (string, error) {
const lookingForFile = "go.mod"
for {
if dir == "." || dir == "/" {
return "", fmt.Errorf("did not found %q file", lookingForFile)
}
lookingForFilePath := path.Join(dir, lookingForFile)
info, err := os.Stat(lookingForFilePath)
if err != nil || info.IsDir() {
// lets check the parent dir
dir = path.Dir(dir)
continue
}
return lookingForFilePath, nil
}
}
2018-05-31 19:42:58 -07:00
// 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
}
// addInvalidFileFailure adds a failure for an invalid formatted file
func addInvalidFileFailure(filename, errStr string, failures chan Failure) {
position := getPositionInvalidFile(filename, errStr)
failures <- Failure{
Confidence: 1,
Failure: fmt.Sprintf("invalid file %s: %v", filename, errStr),
Category: "validity",
Position: position,
}
}
// errPosRegexp matches with an NewFile error message
// i.e. : corrupted.go:10:4: expected '}', found 'EOF
// first group matches the line and the second group, the column
2022-04-10 11:55:13 +02:00
var errPosRegexp = regexp.MustCompile(`.*:(\d*):(\d*):.*$`)
// getPositionInvalidFile gets the position of the error in an invalid file
func getPositionInvalidFile(filename, s string) FailurePosition {
pos := errPosRegexp.FindStringSubmatch(s)
if len(pos) < 3 {
return FailurePosition{}
}
line, err := strconv.Atoi(pos[1])
if err != nil {
return FailurePosition{}
}
column, err := strconv.Atoi(pos[2])
if err != nil {
return FailurePosition{}
}
return FailurePosition{
Start: token.Position{
Filename: filename,
Line: line,
Column: column,
},
}
}