mirror of
https://github.com/mgechev/revive.git
synced 2024-11-24 08:32:22 +02:00
Further refactoring
This commit is contained in:
parent
af459bbef6
commit
836c184aaf
@ -14,15 +14,15 @@ const (
|
||||
)
|
||||
|
||||
// LintElseRule lints given else constructs.
|
||||
type LintElseRule struct {
|
||||
rule.AbstractRule
|
||||
}
|
||||
type LintElseRule struct{}
|
||||
|
||||
// Apply applies the rule to given file.
|
||||
func (r *LintElseRule) Apply(file *file.File, arguments rule.Arguments) []rule.Failure {
|
||||
r.File = file
|
||||
ast.Walk(lintElse{r}, file.GetAST())
|
||||
return r.Failures()
|
||||
var failures []rule.Failure
|
||||
ast.Walk(lintElse(func(failure rule.Failure) {
|
||||
failures = append(failures, failure)
|
||||
}), file.GetAST())
|
||||
return failures
|
||||
}
|
||||
|
||||
// Name returns the rule name.
|
||||
@ -30,9 +30,7 @@ func (r *LintElseRule) Name() string {
|
||||
return ruleName
|
||||
}
|
||||
|
||||
type lintElse struct {
|
||||
r *LintElseRule
|
||||
}
|
||||
type lintElse func(rule.Failure)
|
||||
|
||||
func (f lintElse) Visit(n ast.Node) ast.Visitor {
|
||||
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]
|
||||
if _, ok := lastStmt.(*ast.ReturnStmt); ok {
|
||||
f.r.AddFailureAtNode(rule.Failure{
|
||||
f(rule.Failure{
|
||||
Failure: failure,
|
||||
Type: rule.FailureTypeWarning,
|
||||
}, node.Else, f.r.File)
|
||||
Node: node.Else,
|
||||
})
|
||||
return f
|
||||
}
|
||||
}
|
||||
|
@ -46,13 +46,16 @@ func (l *Linter) Lint(filenames []string, ruleSet []rule.Rule) ([]rule.Failure,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, rule := range ruleSet {
|
||||
currentFailures := rule.Apply(file, []string{})
|
||||
for _, currentRule := range ruleSet {
|
||||
currentFailures := currentRule.Apply(file, []string{})
|
||||
for idx, failure := range currentFailures {
|
||||
if failure.RuleName == "" {
|
||||
failure.RuleName = rule.Name()
|
||||
currentFailures[idx] = failure
|
||||
failure.RuleName = currentRule.Name()
|
||||
}
|
||||
if failure.Node != nil {
|
||||
failure.Position = rule.ToFailurePosition(failure.Node.Pos(), failure.Node.End(), file)
|
||||
}
|
||||
currentFailures[idx] = failure
|
||||
}
|
||||
currentFailures = l.filterFailures(currentFailures, disabledIntervals)
|
||||
failures = append(failures, currentFailures...)
|
||||
|
26
rule/rule.go
26
rule/rule.go
@ -29,7 +29,7 @@ type Failure struct {
|
||||
RuleName string
|
||||
Type FailureType
|
||||
Position FailurePosition
|
||||
file *file.File
|
||||
Node ast.Node
|
||||
}
|
||||
|
||||
// GetFilename returns the filename.
|
||||
@ -57,33 +57,15 @@ type Config struct {
|
||||
type Rule interface {
|
||||
Name() string
|
||||
Apply(*file.File, Arguments) []Failure
|
||||
AddFailures(...Failure)
|
||||
Failures() []Failure
|
||||
}
|
||||
|
||||
// AbstractRule defines an abstract rule.
|
||||
type AbstractRule struct {
|
||||
failures []Failure
|
||||
File *file.File
|
||||
Failures []Failure
|
||||
}
|
||||
|
||||
// AddFailures adds rule failures.
|
||||
func (r *AbstractRule) AddFailures(failures ...Failure) {
|
||||
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 {
|
||||
// ToFailurePosition returns the failure position.
|
||||
func ToFailurePosition(start token.Pos, end token.Pos, file *file.File) FailurePosition {
|
||||
return FailurePosition{
|
||||
Start: file.ToPosition(start),
|
||||
End: file.ToPosition(end),
|
||||
|
Loading…
Reference in New Issue
Block a user