Skip to content

Commit

Permalink
fixup! Support maintidx
Browse files Browse the repository at this point in the history
  • Loading branch information
yagipy committed Dec 25, 2021
1 parent 61901c2 commit 8300775
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .golangci.example.yml
Expand Up @@ -783,6 +783,10 @@ linters-settings:
force-short-decl-cuddling: false
strict-append: true

maintidx:
# show functions with maintainability index < N only.
under: 20

# The custom section can be used to define linter plugins to be loaded at runtime.
# See README doc for more info.
custom:
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -80,6 +80,9 @@ var defaultLintersSettings = LintersSettings{
Forbidigo: ForbidigoSettings{
ExcludeGodocExamples: true,
},
MaintIdx: MaintIdxSettings{
Under: 20,
},
}

type LintersSettings struct {
Expand Down Expand Up @@ -117,6 +120,7 @@ type LintersSettings struct {
Ireturn IreturnSettings
ImportAs ImportAsSettings
Lll LllSettings
MaintIdx MaintIdxSettings
Makezero MakezeroSettings
Maligned MalignedSettings
Misspell MisspellSettings
Expand Down Expand Up @@ -371,6 +375,10 @@ type LllSettings struct {
TabWidth int `mapstructure:"tab-width"`
}

type MaintIdxSettings struct {
Under int `mapstructure:"under"`
}

type MakezeroSettings struct {
Always bool
}
Expand Down
29 changes: 21 additions & 8 deletions pkg/golinters/maintidx.go
@@ -1,20 +1,33 @@
package golinters

import (
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/yagipy/maintidx"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewMaintIdx() *goanalysis.Linter {
analyzers := []*analysis.Analyzer{
maintidx.Analyzer,
func NewMaintIdx(cfg *config.MaintIdxSettings) *goanalysis.Linter {
analyzer := maintidx.Analyzer

cfgMap := map[string]map[string]interface{}{}
cfgMap[analyzer.Name] = map[string]interface{}{
"under": 20,
}

if cfg != nil {
cfgMap[analyzer.Name] = map[string]interface{}{
"under": cfg.Under,
}
}

return goanalysis.NewLinter(
"maintidx",
"maintidx measures the maintainability index of each function.",
analyzers,
nil,
analyzer.Name,
analyzer.Doc,
[]*analysis.Analyzer{
analyzer,
},
cfgMap,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
6 changes: 4 additions & 2 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -113,6 +113,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var ifshortCfg *config.IfshortSettings
var importAsCfg *config.ImportAsSettings
var ireturnCfg *config.IreturnSettings
var maintIdxCfg *config.MaintIdxSettings
var nilNilCfg *config.NilNilSettings
var predeclaredCfg *config.PredeclaredSettings
var reviveCfg *config.ReviveSettings
Expand Down Expand Up @@ -141,6 +142,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
ifshortCfg = &m.cfg.LintersSettings.Ifshort
importAsCfg = &m.cfg.LintersSettings.ImportAs
ireturnCfg = &m.cfg.LintersSettings.Ireturn
maintIdxCfg = &m.cfg.LintersSettings.MaintIdx
nilNilCfg = &m.cfg.LintersSettings.NilNil
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
reviveCfg = &m.cfg.LintersSettings.Revive
Expand Down Expand Up @@ -555,8 +557,8 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/breml/errchkjson"),
linter.NewConfig(golinters.NewMaintIdx()).
WithSince("v1.1.0").
linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)).
WithSince("v1.44.0").
WithPresets(linter.PresetComplexity).
WithLoadForGoAnalysis().
WithURL("https://github.com/yagipy/maintidx"),
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/configs/maintidx_under_100.yml
@@ -0,0 +1,3 @@
linters-settings:
maintidx:
under: 100
197 changes: 197 additions & 0 deletions test/testdata/maintidx_under_100.go
@@ -0,0 +1,197 @@
// args: -Emaintidx
// config_path: testdata/configs/maintidx_under_100.yml
package testdata

func over20() { // ERROR "Function name: over20, Cyclomatic Complexity: 1, Halstead Volume: 8.00, Maintainability Index: 86"
}

func under20() { // ERROR "Function name: under20, Cyclomatic Complexity: 76, Halstead Volume: 1636.00, Maintainability Index: 17"
for true {
if false {
if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else {
n := 0
switch n {
case 0:
case 1:
default:
}
}
} else if false {
if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else {
n := 0
switch n {
case 0:
case 1:
default:
}
}
} else if false {
if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else {
n := 0
switch n {
case 0:
case 1:
default:
}
}
} else if false {
if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else {
n := 0
switch n {
case 0:
case 1:
default:
}
}
} else {
if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else if false {
n := 0
switch n {
case 0:
case 1:
default:
}
} else {
n := 0
switch n {
case 0:
case 1:
default:
}
}
}
}
}

0 comments on commit 8300775

Please sign in to comment.