Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maintidx linter #2435

Merged
merged 3 commits into from Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.example.yml
Expand Up @@ -590,6 +590,10 @@ linters-settings:
# tab width in spaces. Default to 1.
tab-width: 1

maintidx:
# show functions with maintainability index < N only.
under: 20
ldez marked this conversation as resolved.
Show resolved Hide resolved

makezero:
# Allow only slices initialized with a length of zero. Default is false.
always: false
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -91,6 +91,7 @@ require (
github.com/ultraware/whitespace v0.0.4
github.com/uudashr/gocognit v1.0.5
github.com/valyala/quicktemplate v1.7.0
github.com/yagipy/maintidx v1.0.0
github.com/yeya24/promlinter v0.1.0
gitlab.com/bosi/decorder v0.2.1
golang.org/x/tools v0.1.9-0.20211228192929-ee1ca4ffc4da
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -128,6 +128,7 @@ type LintersSettings struct {
ImportAs ImportAsSettings
Ireturn IreturnSettings
Lll LllSettings
MaintIdx MaintIdxSettings
Makezero MakezeroSettings
Maligned MalignedSettings
Misspell MisspellSettings
Expand Down Expand Up @@ -389,6 +390,10 @@ type LllSettings struct {
TabWidth int `mapstructure:"tab-width"`
}

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

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

import (
"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(cfg *config.MaintIdxSettings) *goanalysis.Linter {
analyzer := maintidx.Analyzer

cfgMap := map[string]map[string]interface{}{}
cfgMap[analyzer.Name] = map[string]interface{}{
"under": 20,
}
ldez marked this conversation as resolved.
Show resolved Hide resolved

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

return goanalysis.NewLinter(
analyzer.Name,
analyzer.Doc,
[]*analysis.Analyzer{
analyzer,
},
cfgMap,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
8 changes: 8 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -114,6 +114,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 nlreturnCfg *config.NlreturnSettings
var predeclaredCfg *config.PredeclaredSettings
Expand Down Expand Up @@ -143,6 +144,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
nlreturnCfg = &m.cfg.LintersSettings.Nlreturn
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
Expand Down Expand Up @@ -439,6 +441,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithSince("v1.8.0").
WithPresets(linter.PresetStyle),

linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)).
WithSince("v1.1.0").
WithPresets(linter.PresetComplexity).
WithLoadForGoAnalysis().
WithURL("https://github.com/yagipy/maintidx"),
ldez marked this conversation as resolved.
Show resolved Hide resolved

linter.NewConfig(golinters.NewMakezero()).
WithSince("v1.34.0").
WithPresets(linter.PresetStyle, linter.PresetBugs).
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/configs/maintidx_under_100.yml
@@ -0,0 +1,3 @@
linters-settings:
maintidx:
under: 100
196 changes: 196 additions & 0 deletions test/testdata/maintidx.go
@@ -0,0 +1,196 @@
//args: -Emaintidx
package testdata

func over20() {
}

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:
}
}
}
}
}