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