1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-22 03:38:47 +02:00

Fix package level type checking

This commit is contained in:
mgechev 2018-05-31 19:42:58 -07:00
parent 27994a0736
commit 64636caedd
No known key found for this signature in database
GPG Key ID: 3C44F5A2A289C6BB
6 changed files with 65 additions and 44 deletions

2
Gopkg.lock generated
View File

@ -35,7 +35,7 @@
branch = "master"
name = "github.com/mgechev/dots"
packages = ["."]
revision = "1cc1f4773d0cd3a8ab98f0e79d2690ba811f9bcd"
revision = "3d1c0cc50642eae6291b43fdd37c7d7acfff7975"
[[projects]]
branch = "master"

View File

@ -155,18 +155,18 @@ func normalizeSplit(strs []string) []string {
return res
}
func getFiles() []string {
func getPackages() [][]string {
globs := normalizeSplit(flag.Args())
if len(globs) == 0 {
globs = append(globs, ".")
}
files, err := dots.Resolve(globs, normalizeSplit(excludePaths))
packages, err := dots.ResolvePackages(globs, normalizeSplit(excludePaths))
if err != nil {
fail(err.Error())
}
return files
return packages
}
type arrayFlags []string

View File

@ -3,7 +3,10 @@ package lint
import (
"bufio"
"bytes"
"fmt"
"go/token"
"os"
"sync"
)
// ReadFile defines an abstraction for reading files.
@ -26,6 +29,60 @@ var (
genFtr = []byte(" DO NOT EDIT.")
)
// 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)
}
go func() {
wg.Wait()
close(failures)
}()
return failures, nil
}
func (l *Linter) lintPackage(filenames []string, ruleSet []Rule, config Config, failures chan Failure) error {
pkg := &Package{
fset: token.NewFileSet(),
files: map[string]*File{},
}
for _, filename := range filenames {
content, err := l.reader(filename)
if err != nil {
return err
}
if isGenerated(content) && !config.IgnoreGeneratedHeader {
continue
}
file, err := NewFile(filename, content, pkg)
if err != nil {
return err
}
pkg.files[filename] = file
}
if len(pkg.files) == 0 {
return nil
}
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.
@ -39,34 +96,3 @@ func isGenerated(src []byte) bool {
}
return false
}
// Lint lints a set of files with the specified rule.
func (l *Linter) Lint(filenames []string, ruleSet []Rule, config Config) (<-chan Failure, error) {
failures := make(chan Failure)
pkg := &Package{
fset: token.NewFileSet(),
files: map[string]*File{},
}
for _, filename := range filenames {
content, err := l.reader(filename)
if err != nil {
return nil, err
}
if isGenerated(content) && !config.IgnoreGeneratedHeader {
continue
}
file, err := NewFile(filename, content, pkg)
if err != nil {
return nil, err
}
pkg.files[filename] = file
}
go (func() {
pkg.lint(ruleSet, config, failures)
})()
return failures, nil
}

View File

@ -140,10 +140,6 @@ func receiverType(fn *ast.FuncDecl) string {
}
func (p *Package) lint(rules []Rule, config Config, failures chan Failure) {
if len(p.files) == 0 {
close(failures)
return
}
p.typeCheck()
p.scanSortable()
var wg sync.WaitGroup
@ -155,5 +151,4 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) {
})(file)
}
wg.Wait()
close(failures)
}

View File

@ -26,7 +26,7 @@ Example:
func main() {
config := getConfig()
formatter := getFormatter()
files := getFiles()
packages := getPackages()
revive := lint.New(func(file string) ([]byte, error) {
return ioutil.ReadFile(file)
@ -34,7 +34,7 @@ func main() {
lintingRules := getLintingRules(config)
failures, err := revive.Lint(files, lintingRules, *config)
failures, err := revive.Lint(packages, lintingRules, *config)
if err != nil {
fail(err.Error())
}

View File

@ -46,7 +46,7 @@ func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, src []byte, rul
return ioutil.ReadFile(baseDir + file)
})
ps, err := l.Lint([]string{fi.Name()}, rules, lint.Config{
ps, err := l.Lint([][]string{[]string{fi.Name()}}, rules, lint.Config{
Rules: config,
})
if err != nil {
@ -73,7 +73,7 @@ func assertFailures(t *testing.T, baseDir string, fi os.FileInfo, src []byte, ru
return errors.Errorf("Test file %v does not have instructions", fi.Name())
}
ps, err := l.Lint([]string{fi.Name()}, rules, lint.Config{
ps, err := l.Lint([][]string{[]string{fi.Name()}}, rules, lint.Config{
Rules: config,
})
if err != nil {