1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00

Further refactoring

This commit is contained in:
mgechev 2017-11-19 17:58:28 -08:00
parent af459bbef6
commit 836c184aaf
3 changed files with 21 additions and 37 deletions

View File

@ -14,15 +14,15 @@ const (
) )
// LintElseRule lints given else constructs. // LintElseRule lints given else constructs.
type LintElseRule struct { type LintElseRule struct{}
rule.AbstractRule
}
// Apply applies the rule to given file. // Apply applies the rule to given file.
func (r *LintElseRule) Apply(file *file.File, arguments rule.Arguments) []rule.Failure { func (r *LintElseRule) Apply(file *file.File, arguments rule.Arguments) []rule.Failure {
r.File = file var failures []rule.Failure
ast.Walk(lintElse{r}, file.GetAST()) ast.Walk(lintElse(func(failure rule.Failure) {
return r.Failures() failures = append(failures, failure)
}), file.GetAST())
return failures
} }
// Name returns the rule name. // Name returns the rule name.
@ -30,9 +30,7 @@ func (r *LintElseRule) Name() string {
return ruleName return ruleName
} }
type lintElse struct { type lintElse func(rule.Failure)
r *LintElseRule
}
func (f lintElse) Visit(n ast.Node) ast.Visitor { func (f lintElse) Visit(n ast.Node) ast.Visitor {
node, ok := n.(*ast.IfStmt) node, ok := n.(*ast.IfStmt)
@ -55,10 +53,11 @@ func (f lintElse) Visit(n ast.Node) ast.Visitor {
} }
lastStmt := node.Body.List[len(node.Body.List)-1] lastStmt := node.Body.List[len(node.Body.List)-1]
if _, ok := lastStmt.(*ast.ReturnStmt); ok { if _, ok := lastStmt.(*ast.ReturnStmt); ok {
f.r.AddFailureAtNode(rule.Failure{ f(rule.Failure{
Failure: failure, Failure: failure,
Type: rule.FailureTypeWarning, Type: rule.FailureTypeWarning,
}, node.Else, f.r.File) Node: node.Else,
})
return f return f
} }
} }

View File

@ -46,13 +46,16 @@ func (l *Linter) Lint(filenames []string, ruleSet []rule.Rule) ([]rule.Failure,
return nil, err return nil, err
} }
for _, rule := range ruleSet { for _, currentRule := range ruleSet {
currentFailures := rule.Apply(file, []string{}) currentFailures := currentRule.Apply(file, []string{})
for idx, failure := range currentFailures { for idx, failure := range currentFailures {
if failure.RuleName == "" { if failure.RuleName == "" {
failure.RuleName = rule.Name() failure.RuleName = currentRule.Name()
currentFailures[idx] = failure
} }
if failure.Node != nil {
failure.Position = rule.ToFailurePosition(failure.Node.Pos(), failure.Node.End(), file)
}
currentFailures[idx] = failure
} }
currentFailures = l.filterFailures(currentFailures, disabledIntervals) currentFailures = l.filterFailures(currentFailures, disabledIntervals)
failures = append(failures, currentFailures...) failures = append(failures, currentFailures...)

View File

@ -29,7 +29,7 @@ type Failure struct {
RuleName string RuleName string
Type FailureType Type FailureType
Position FailurePosition Position FailurePosition
file *file.File Node ast.Node
} }
// GetFilename returns the filename. // GetFilename returns the filename.
@ -57,33 +57,15 @@ type Config struct {
type Rule interface { type Rule interface {
Name() string Name() string
Apply(*file.File, Arguments) []Failure Apply(*file.File, Arguments) []Failure
AddFailures(...Failure)
Failures() []Failure
} }
// AbstractRule defines an abstract rule. // AbstractRule defines an abstract rule.
type AbstractRule struct { type AbstractRule struct {
failures []Failure Failures []Failure
File *file.File
} }
// AddFailures adds rule failures. // ToFailurePosition returns the failure position.
func (r *AbstractRule) AddFailures(failures ...Failure) { func ToFailurePosition(start token.Pos, end token.Pos, file *file.File) FailurePosition {
r.failures = append(r.failures, failures...)
}
// AddFailureAtNode adds rule failure at specific node.
func (r *AbstractRule) AddFailureAtNode(failure Failure, t ast.Node, file *file.File) {
failure.Position = toFailurePosition(t.Pos(), t.End(), file)
r.AddFailures(failure)
}
// Failures returns the rule failures.
func (r *AbstractRule) Failures() []Failure {
return r.failures
}
func toFailurePosition(start token.Pos, end token.Pos, file *file.File) FailurePosition {
return FailurePosition{ return FailurePosition{
Start: file.ToPosition(start), Start: file.ToPosition(start),
End: file.ToPosition(end), End: file.ToPosition(end),