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

Improve design

This commit is contained in:
mgechev 2018-01-21 18:29:07 -08:00
parent f336ff920d
commit 40991fc70d
3 changed files with 14 additions and 14 deletions

View File

@ -13,19 +13,19 @@ import (
type File struct {
Name string
pkg *Package
Content []byte
content []byte
ast *ast.File
}
// NewFile creates a new file
func NewFile(name string, content []byte, pkg *Package) (*File, error) {
f, err := parser.ParseFile(pkg.Fset, name, content, parser.ParseComments)
f, err := parser.ParseFile(pkg.fset, name, content, parser.ParseComments)
if err != nil {
return nil, err
}
return &File{
Name: name,
Content: content,
content: content,
pkg: pkg,
ast: f,
}, nil
@ -33,7 +33,7 @@ func NewFile(name string, content []byte, pkg *Package) (*File, error) {
// ToPosition returns line and column for given position.
func (f *File) ToPosition(pos token.Pos) token.Position {
return f.pkg.Fset.Position(pos)
return f.pkg.fset.Position(pos)
}
// GetAST returns the AST of the file

View File

@ -44,8 +44,8 @@ func isGenerated(src []byte) bool {
// Lint lints a set of files with the specified rule.
func (l *Linter) Lint(filenames []string, ruleSet []Rule, rulesConfig RulesConfig) ([]Failure, error) {
pkg := &Package{
Fset: token.NewFileSet(),
Files: map[string]*File{},
fset: token.NewFileSet(),
files: map[string]*File{},
}
var pkgName string
for _, filename := range filenames {
@ -68,7 +68,7 @@ func (l *Linter) Lint(filenames []string, ruleSet []Rule, rulesConfig RulesConfi
return nil, fmt.Errorf("%s is in package %s, not %s", filename, file.GetAST().Name.Name, pkgName)
}
pkg.Files[filename] = file
pkg.files[filename] = file
}
return pkg.lint(ruleSet, rulesConfig), nil

View File

@ -10,8 +10,8 @@ import (
// Package represents a package in the project.
type Package struct {
Fset *token.FileSet
Files map[string]*File
fset *token.FileSet
files map[string]*File
TypesPkg *types.Package
TypesInfo *types.Info
@ -39,7 +39,7 @@ func (p *Package) IsMain() bool {
} else if p.main == falseValue {
return false
}
for _, f := range p.Files {
for _, f := range p.files {
if f.isMain() {
p.main = trueValue
return true
@ -54,7 +54,7 @@ func (p *Package) TypeCheck() error {
config := &types.Config{
// By setting a no-op error reporter, the type checker does as much work as possible.
Error: func(error) {},
Importer: newImporter(p.Fset),
Importer: newImporter(p.fset),
}
info := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
@ -64,11 +64,11 @@ func (p *Package) TypeCheck() error {
}
var anyFile *File
var astFiles []*ast.File
for _, f := range p.Files {
for _, f := range p.files {
anyFile = f
astFiles = append(astFiles, f.GetAST())
}
typesPkg, err := config.Check(anyFile.GetAST().Name.Name, p.Fset, astFiles, info)
typesPkg, err := config.Check(anyFile.GetAST().Name.Name, p.fset, astFiles, info)
// Remember the typechecking info, even if config.Check failed,
// since we will get partial information.
p.TypesPkg = typesPkg
@ -79,7 +79,7 @@ func (p *Package) TypeCheck() error {
func (p *Package) lint(rules []Rule, config RulesConfig) []Failure {
var failures []Failure
p.TypeCheck()
for _, file := range p.Files {
for _, file := range p.files {
failures = append(failures, file.lint(rules, config)...)
}
return failures