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