mirror of
https://github.com/mgechev/revive.git
synced 2025-01-22 03:38:47 +02:00
API improvements
This commit is contained in:
parent
466e9339e4
commit
5ec3daa762
11
config.toml
11
config.toml
@ -1,7 +1,10 @@
|
||||
severity = "warning"
|
||||
confidence = 0.8
|
||||
|
||||
[rules.else]
|
||||
[rules.names]
|
||||
[rules.argument-limit]
|
||||
arguments = ['2']
|
||||
[rule.else]
|
||||
[rule.names]
|
||||
[rule.argument-limit]
|
||||
arguments = [2]
|
||||
[rule.cyclomatic]
|
||||
arguments = [3]
|
||||
severity = "error"
|
@ -47,7 +47,7 @@ type DisabledInterval struct {
|
||||
}
|
||||
|
||||
// Arguments is type used for the arguments of a rule.
|
||||
type Arguments = []string
|
||||
type Arguments = []interface{}
|
||||
|
||||
// RuleConfig is type used for the rule configuration.
|
||||
type RuleConfig struct {
|
||||
@ -62,7 +62,7 @@ type RulesConfig = map[string]RuleConfig
|
||||
type Config struct {
|
||||
Confidence float64
|
||||
Severity Severity
|
||||
Rules RulesConfig
|
||||
Rules RulesConfig `toml:"rule"`
|
||||
}
|
||||
|
||||
// Rule defines an abstract rule interaface
|
||||
|
3
main.go
3
main.go
@ -32,6 +32,7 @@ var allRules = []lint.Rule{
|
||||
&rule.TimeNamesRule{},
|
||||
&rule.ContextKeyTypeRule{},
|
||||
&rule.ContextArgumentsRule{},
|
||||
&rule.CyclomaticRule{},
|
||||
}
|
||||
|
||||
func getLintingRules(config *lint.Config) []lint.Rule {
|
||||
@ -82,7 +83,7 @@ func main() {
|
||||
package p
|
||||
|
||||
func Test() {
|
||||
if true {
|
||||
if true || bar && baz {
|
||||
return 42;
|
||||
} else {
|
||||
return 23;
|
||||
|
@ -3,7 +3,6 @@ package rule
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"strconv"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
@ -16,15 +15,16 @@ func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []
|
||||
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"`)
|
||||
|
||||
total, ok := arguments[0].(int64) // Alt. non panicking version
|
||||
if !ok {
|
||||
panic(`invalid value passed as argument number to the "argument-list" rule`)
|
||||
}
|
||||
|
||||
var failures []lint.Failure
|
||||
|
||||
walker := lintArgsNum{
|
||||
total: total,
|
||||
total: int(total),
|
||||
onFailure: func(failure lint.Failure) {
|
||||
failures = append(failures, failure)
|
||||
},
|
||||
@ -41,14 +41,14 @@ func (r *ArgumentsLimitRule) Name() string {
|
||||
}
|
||||
|
||||
type lintArgsNum struct {
|
||||
total int64
|
||||
total int
|
||||
onFailure func(lint.Failure)
|
||||
}
|
||||
|
||||
func (w lintArgsNum) Visit(n ast.Node) ast.Visitor {
|
||||
node, ok := n.(*ast.FuncDecl)
|
||||
if ok {
|
||||
num := int64(len(node.Type.Params.List))
|
||||
num := len(node.Type.Params.List)
|
||||
if num > w.total {
|
||||
w.onFailure(lint.Failure{
|
||||
Confidence: 1,
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"strconv"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
@ -18,15 +17,15 @@ type CyclomaticRule struct{}
|
||||
func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
||||
var failures []lint.Failure
|
||||
|
||||
complexity, err := strconv.Atoi(arguments[0])
|
||||
if err != nil {
|
||||
complexity, ok := arguments[0].(int64) // Alt. non panicking version
|
||||
if !ok {
|
||||
panic("invalid argument for cyclomatic complexity")
|
||||
}
|
||||
|
||||
fileAst := file.AST
|
||||
walker := lintCyclomatic{
|
||||
file: file,
|
||||
complexity: complexity,
|
||||
complexity: int(complexity),
|
||||
onFailure: func(failure lint.Failure) {
|
||||
failures = append(failures, failure)
|
||||
},
|
||||
@ -39,7 +38,7 @@ func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint
|
||||
|
||||
// Name returns the rule name.
|
||||
func (r *CyclomaticRule) Name() string {
|
||||
return "errorf"
|
||||
return "cyclomatic"
|
||||
}
|
||||
|
||||
type lintCyclomatic struct {
|
||||
|
Loading…
x
Reference in New Issue
Block a user