cyclop: add missing settings (#1743)
This commit is contained in:
parent
9c477156cb
commit
32e8517a34
@ -183,11 +183,6 @@ type LintersSettings struct {
|
||||
Gocyclo struct {
|
||||
MinComplexity int `mapstructure:"min-complexity"`
|
||||
}
|
||||
Cyclop struct {
|
||||
MaxComplexity int `mapstructure:"max-complexity"`
|
||||
PackageAverage float64 `mapstructure:"package-average"`
|
||||
SkipTests bool `mapstructure:"skip-tests"`
|
||||
}
|
||||
Varcheck struct {
|
||||
CheckExportedFields bool `mapstructure:"exported-fields"`
|
||||
}
|
||||
@ -278,6 +273,7 @@ type LintersSettings struct {
|
||||
Forbidigo ForbidigoSettings
|
||||
Ifshort IfshortSettings
|
||||
Predeclared PredeclaredSettings
|
||||
Cyclop Cyclop
|
||||
|
||||
Custom map[string]CustomLinterSettings
|
||||
}
|
||||
@ -453,6 +449,12 @@ type PredeclaredSettings struct {
|
||||
Qualified bool `mapstructure:"q"`
|
||||
}
|
||||
|
||||
type Cyclop struct {
|
||||
MaxComplexity int `mapstructure:"max-complexity"`
|
||||
PackageAverage float64 `mapstructure:"package-average"`
|
||||
SkipTests bool `mapstructure:"skip-tests"`
|
||||
}
|
||||
|
||||
var defaultLintersSettings = LintersSettings{
|
||||
Lll: LllSettings{
|
||||
LineLength: 120,
|
||||
|
@ -4,18 +4,36 @@ import (
|
||||
"github.com/bkielbasa/cyclop/pkg/analyzer"
|
||||
"golang.org/x/tools/go/analysis"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/config"
|
||||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
|
||||
)
|
||||
|
||||
const cyclopName = "cyclop"
|
||||
|
||||
func NewCyclop() *goanalysis.Linter {
|
||||
func NewCyclop(settings *config.Cyclop) *goanalysis.Linter {
|
||||
a := analyzer.NewAnalyzer()
|
||||
|
||||
var cfg map[string]map[string]interface{}
|
||||
if settings != nil {
|
||||
d := map[string]interface{}{
|
||||
"skipTests": settings.SkipTests,
|
||||
}
|
||||
|
||||
if settings.MaxComplexity != 0 {
|
||||
d["maxComplexity"] = settings.MaxComplexity
|
||||
}
|
||||
|
||||
if settings.PackageAverage != 0 {
|
||||
d["packageAverage"] = settings.PackageAverage
|
||||
}
|
||||
|
||||
cfg = map[string]map[string]interface{}{a.Name: d}
|
||||
}
|
||||
|
||||
return goanalysis.NewLinter(
|
||||
cyclopName,
|
||||
"checks function and package cyclomatic complexity",
|
||||
[]*analysis.Analyzer{
|
||||
analyzer.NewAnalyzer(),
|
||||
},
|
||||
nil,
|
||||
[]*analysis.Analyzer{a},
|
||||
cfg,
|
||||
).WithLoadMode(goanalysis.LoadModeTypesInfo)
|
||||
}
|
||||
|
@ -97,6 +97,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
|
||||
var predeclaredCfg *config.PredeclaredSettings
|
||||
var ifshortCfg *config.IfshortSettings
|
||||
var reviveCfg *config.ReviveSettings
|
||||
var cyclopCfg *config.Cyclop
|
||||
if m.cfg != nil {
|
||||
govetCfg = &m.cfg.LintersSettings.Govet
|
||||
testpackageCfg = &m.cfg.LintersSettings.Testpackage
|
||||
@ -106,6 +107,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
|
||||
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
|
||||
ifshortCfg = &m.cfg.LintersSettings.Ifshort
|
||||
reviveCfg = &m.cfg.LintersSettings.Revive
|
||||
cyclopCfg = &m.cfg.LintersSettings.Cyclop
|
||||
}
|
||||
const megacheckName = "megacheck"
|
||||
lcs := []*linter.Config{
|
||||
@ -194,7 +196,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
|
||||
linter.NewConfig(golinters.NewGocyclo()).
|
||||
WithPresets(linter.PresetComplexity).
|
||||
WithURL("https://github.com/alecthomas/gocyclo"),
|
||||
linter.NewConfig(golinters.NewCyclop()).
|
||||
linter.NewConfig(golinters.NewCyclop(cyclopCfg)).
|
||||
WithLoadForGoAnalysis().
|
||||
WithPresets(linter.PresetComplexity).
|
||||
WithURL("https://github.com/bkielbasa/cyclop"),
|
||||
|
35
test/testdata/cyclop.go
vendored
35
test/testdata/cyclop.go
vendored
@ -1,30 +1,15 @@
|
||||
// args: -Ecyclop
|
||||
//args: -Ecyclop
|
||||
//config: linters-settings.cyclop.max-complexity=15
|
||||
package testdata
|
||||
|
||||
import "math"
|
||||
|
||||
func cyclopComplexFunc() { // ERROR "calculated cyclomatic complexity for function cyclopComplexFunc is 11, max is 10"
|
||||
i := math.MaxInt8
|
||||
if i > 2 {
|
||||
if i > 2 {
|
||||
}
|
||||
if i > 2 {
|
||||
}
|
||||
if i > 2 {
|
||||
}
|
||||
if i > 2 {
|
||||
}
|
||||
} else {
|
||||
if i > 2 {
|
||||
}
|
||||
if i > 2 {
|
||||
}
|
||||
if i > 2 {
|
||||
}
|
||||
if i > 2 {
|
||||
}
|
||||
func cyclopComplexFunc(s string) { // ERROR "calculated cyclomatic complexity for function cyclopComplexFunc is 22, max is 15"
|
||||
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
|
||||
return
|
||||
}
|
||||
|
||||
if i > 2 {
|
||||
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
|
||||
return
|
||||
}
|
||||
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
4
test/testdata/gocyclo.go
vendored
4
test/testdata/gocyclo.go
vendored
@ -2,8 +2,10 @@
|
||||
//config: linters-settings.gocyclo.min-complexity=20
|
||||
package testdata
|
||||
|
||||
import "net/http"
|
||||
|
||||
func GocycloBigComplexity(s string) { // ERROR "cyclomatic complexity .* of func .* is high .*"
|
||||
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
|
||||
if s == http.MethodGet || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
|
||||
return
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user