mirror of
https://github.com/mgechev/revive.git
synced 2025-11-23 22:04:49 +02:00
Implement extra rule
This commit is contained in:
63
defaultrule/argument-limit.go
Normal file
63
defaultrule/argument-limit.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package defaultrule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go/ast"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/mgechev/revive/file"
|
||||||
|
"github.com/mgechev/revive/rule"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ArgumentsLimitRule lints given else constructs.
|
||||||
|
type ArgumentsLimitRule struct{}
|
||||||
|
|
||||||
|
// Apply applies the rule to given file.
|
||||||
|
func (r *ArgumentsLimitRule) Apply(file *file.File, arguments rule.Arguments) []rule.Failure {
|
||||||
|
if len(arguments) != 1 {
|
||||||
|
panic(`invalid configuration for "argument-limit"`)
|
||||||
|
}
|
||||||
|
total, err := strconv.ParseInt(arguments[0], 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
panic(`invalid configuration for "argument-limit"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
var failures []rule.Failure
|
||||||
|
|
||||||
|
walker := lintArgsNum{
|
||||||
|
total: total,
|
||||||
|
onFailure: func(failure rule.Failure) {
|
||||||
|
failures = append(failures, failure)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ast.Walk(walker, file.GetAST())
|
||||||
|
|
||||||
|
return failures
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name returns the rule name.
|
||||||
|
func (r *ArgumentsLimitRule) Name() string {
|
||||||
|
return "argument-limit"
|
||||||
|
}
|
||||||
|
|
||||||
|
type lintArgsNum struct {
|
||||||
|
total int64
|
||||||
|
onFailure func(rule.Failure)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w lintArgsNum) Visit(n ast.Node) ast.Visitor {
|
||||||
|
node, ok := n.(*ast.FuncDecl)
|
||||||
|
if ok {
|
||||||
|
num := int64(len(node.Type.Params.List))
|
||||||
|
if num > w.total {
|
||||||
|
w.onFailure(rule.Failure{
|
||||||
|
Failure: fmt.Sprintf("maximum number of arguments per function exceeded; max %d but got %d", w.total, num),
|
||||||
|
Type: rule.FailureTypeWarning,
|
||||||
|
Node: node.Type,
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return w
|
||||||
|
}
|
||||||
@@ -8,11 +8,6 @@ import (
|
|||||||
"github.com/mgechev/revive/rule"
|
"github.com/mgechev/revive/rule"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
ruleName = "no-else-return"
|
|
||||||
failure = "if block ends with a return statement, so drop this else and outdent its block"
|
|
||||||
)
|
|
||||||
|
|
||||||
// LintElseRule lints given else constructs.
|
// LintElseRule lints given else constructs.
|
||||||
type LintElseRule struct{}
|
type LintElseRule struct{}
|
||||||
|
|
||||||
@@ -27,7 +22,7 @@ func (r *LintElseRule) Apply(file *file.File, arguments rule.Arguments) []rule.F
|
|||||||
|
|
||||||
// Name returns the rule name.
|
// Name returns the rule name.
|
||||||
func (r *LintElseRule) Name() string {
|
func (r *LintElseRule) Name() string {
|
||||||
return ruleName
|
return "no-else-return"
|
||||||
}
|
}
|
||||||
|
|
||||||
type lintElse func(rule.Failure)
|
type lintElse func(rule.Failure)
|
||||||
@@ -54,7 +49,7 @@ 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(rule.Failure{
|
f(rule.Failure{
|
||||||
Failure: failure,
|
Failure: "if block ends with a return statement, so drop this else and outdent its block",
|
||||||
Type: rule.FailureTypeWarning,
|
Type: rule.FailureTypeWarning,
|
||||||
Node: node.Else,
|
Node: node.Else,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func New(reader ReadFile) Linter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Lint lints a set of files with the specified rule.
|
// Lint lints a set of files with the specified rule.
|
||||||
func (l *Linter) Lint(filenames []string, ruleSet []rule.Rule) ([]rule.Failure, error) {
|
func (l *Linter) Lint(filenames []string, ruleSet []rule.Rule, rulesConfig rule.RulesConfig) ([]rule.Failure, error) {
|
||||||
var fileSet token.FileSet
|
var fileSet token.FileSet
|
||||||
var failures []rule.Failure
|
var failures []rule.Failure
|
||||||
var ruleNames = []string{}
|
var ruleNames = []string{}
|
||||||
@@ -47,7 +47,8 @@ func (l *Linter) Lint(filenames []string, ruleSet []rule.Rule) ([]rule.Failure,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, currentRule := range ruleSet {
|
for _, currentRule := range ruleSet {
|
||||||
currentFailures := currentRule.Apply(file, []string{})
|
config := rulesConfig[currentRule.Name()]
|
||||||
|
currentFailures := currentRule.Apply(file, config)
|
||||||
for idx, failure := range currentFailures {
|
for idx, failure := range currentFailures {
|
||||||
if failure.RuleName == "" {
|
if failure.RuleName == "" {
|
||||||
failure.RuleName = currentRule.Name()
|
failure.RuleName = currentRule.Name()
|
||||||
|
|||||||
12
main.go
12
main.go
@@ -20,15 +20,23 @@ func main() {
|
|||||||
return 23;
|
return 23;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func foobar(a int, b int, c int, d int) {
|
||||||
|
return a + b + c;
|
||||||
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
linter := linter.New(func(file string) ([]byte, error) {
|
linter := linter.New(func(file string) ([]byte, error) {
|
||||||
return []byte(src), nil
|
return []byte(src), nil
|
||||||
})
|
})
|
||||||
var result []rule.Rule
|
var result []rule.Rule
|
||||||
result = append(result, &defaultrule.LintElseRule{})
|
result = append(result, &defaultrule.LintElseRule{}, &defaultrule.ArgumentsLimitRule{})
|
||||||
|
|
||||||
failures, err := linter.Lint([]string{"foo.go", "bar.go", "baz.go"}, result)
|
var config = rule.RulesConfig{
|
||||||
|
"argument-limit": []string{"3"},
|
||||||
|
}
|
||||||
|
|
||||||
|
failures, err := linter.Lint([]string{"foo.go", "bar.go", "baz.go"}, result, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,6 +53,9 @@ type Config struct {
|
|||||||
Arguments Arguments
|
Arguments Arguments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RulesConfig defiles the config for all rules.
|
||||||
|
type RulesConfig = map[string]Arguments
|
||||||
|
|
||||||
// Rule defines an abstract rule interaface
|
// Rule defines an abstract rule interaface
|
||||||
type Rule interface {
|
type Rule interface {
|
||||||
Name() string
|
Name() string
|
||||||
|
|||||||
Reference in New Issue
Block a user