Skip to content

Commit e3a621d

Browse files
committed
fixup! Support maintidx
1 parent 61901c2 commit e3a621d

File tree

6 files changed

+237
-10
lines changed

6 files changed

+237
-10
lines changed

.golangci.example.yml

+4
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,10 @@ linters-settings:
783783
force-short-decl-cuddling: false
784784
strict-append: true
785785

786+
maintidx:
787+
# show functions with maintainability index < N only.
788+
under: 20
789+
786790
# The custom section can be used to define linter plugins to be loaded at runtime.
787791
# See README doc for more info.
788792
custom:

pkg/config/linters_settings.go

+8
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ var defaultLintersSettings = LintersSettings{
8080
Forbidigo: ForbidigoSettings{
8181
ExcludeGodocExamples: true,
8282
},
83+
MaintIdx: MaintIdxSettings{
84+
Under: 20,
85+
},
8386
}
8487

8588
type LintersSettings struct {
@@ -117,6 +120,7 @@ type LintersSettings struct {
117120
Ireturn IreturnSettings
118121
ImportAs ImportAsSettings
119122
Lll LllSettings
123+
MaintIdx MaintIdxSettings
120124
Makezero MakezeroSettings
121125
Maligned MalignedSettings
122126
Misspell MisspellSettings
@@ -371,6 +375,10 @@ type LllSettings struct {
371375
TabWidth int `mapstructure:"tab-width"`
372376
}
373377

378+
type MaintIdxSettings struct {
379+
Under int `mapstructure:"under"`
380+
}
381+
374382
type MakezeroSettings struct {
375383
Always bool
376384
}

pkg/golinters/maintidx.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
package golinters
22

33
import (
4-
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
54
"github.com/yagipy/maintidx"
65
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
79
)
810

9-
func NewMaintIdx() *goanalysis.Linter {
10-
analyzers := []*analysis.Analyzer{
11-
maintidx.Analyzer,
11+
func NewMaintIdx(cfg *config.MaintIdxSettings) *goanalysis.Linter {
12+
analyzer := maintidx.Analyzer
13+
14+
cfgMap := map[string]map[string]interface{}{}
15+
cfgMap[analyzer.Name] = map[string]interface{}{
16+
"under": 20,
17+
}
18+
19+
if cfg != nil {
20+
cfgMap[analyzer.Name] = map[string]interface{}{
21+
"under": cfg.Under,
22+
}
1223
}
1324

1425
return goanalysis.NewLinter(
15-
"maintidx",
16-
"maintidx measures the maintainability index of each function.",
17-
analyzers,
18-
nil,
26+
analyzer.Name,
27+
analyzer.Doc,
28+
[]*analysis.Analyzer{
29+
analyzer,
30+
},
31+
cfgMap,
1932
).WithLoadMode(goanalysis.LoadModeSyntax)
2033
}

pkg/lint/lintersdb/manager.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
113113
var ifshortCfg *config.IfshortSettings
114114
var importAsCfg *config.ImportAsSettings
115115
var ireturnCfg *config.IreturnSettings
116+
var maintIdxCfg *config.MaintIdxSettings
116117
var nilNilCfg *config.NilNilSettings
117118
var predeclaredCfg *config.PredeclaredSettings
118119
var reviveCfg *config.ReviveSettings
@@ -141,6 +142,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
141142
ifshortCfg = &m.cfg.LintersSettings.Ifshort
142143
importAsCfg = &m.cfg.LintersSettings.ImportAs
143144
ireturnCfg = &m.cfg.LintersSettings.Ireturn
145+
maintIdxCfg = &m.cfg.LintersSettings.MaintIdx
144146
nilNilCfg = &m.cfg.LintersSettings.NilNil
145147
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
146148
reviveCfg = &m.cfg.LintersSettings.Revive
@@ -555,8 +557,8 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
555557
WithPresets(linter.PresetBugs).
556558
WithLoadForGoAnalysis().
557559
WithURL("https://github.com/breml/errchkjson"),
558-
linter.NewConfig(golinters.NewMaintIdx()).
559-
WithSince("v1.1.0").
560+
linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)).
561+
WithSince("v1.44.0").
560562
WithPresets(linter.PresetComplexity).
561563
WithLoadForGoAnalysis().
562564
WithURL("https://github.com/yagipy/maintidx"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
maintidx:
3+
under: 100

test/testdata/maintidx_under_100.go

+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// args: -Emaintidx
2+
// config_path: testdata/configs/maintidx_under_100.yml
3+
package testdata
4+
5+
func over20() { // ERROR "Function name: over20, Cyclomatic Complexity: 76, Halstead Volume: 1636.00, Maintainability Index: 17"
6+
}
7+
8+
func under20() { // ERROR "Function name: under20, Cyclomatic Complexity: 76, Halstead Volume: 1636.00, Maintainability Index: 17"
9+
for true {
10+
if false {
11+
if false {
12+
n := 0
13+
switch n {
14+
case 0:
15+
case 1:
16+
default:
17+
}
18+
} else if false {
19+
n := 0
20+
switch n {
21+
case 0:
22+
case 1:
23+
default:
24+
}
25+
} else if false {
26+
n := 0
27+
switch n {
28+
case 0:
29+
case 1:
30+
default:
31+
}
32+
} else if false {
33+
n := 0
34+
switch n {
35+
case 0:
36+
case 1:
37+
default:
38+
}
39+
} else {
40+
n := 0
41+
switch n {
42+
case 0:
43+
case 1:
44+
default:
45+
}
46+
}
47+
} else if false {
48+
if false {
49+
n := 0
50+
switch n {
51+
case 0:
52+
case 1:
53+
default:
54+
}
55+
} else if false {
56+
n := 0
57+
switch n {
58+
case 0:
59+
case 1:
60+
default:
61+
}
62+
} else if false {
63+
n := 0
64+
switch n {
65+
case 0:
66+
case 1:
67+
default:
68+
}
69+
} else if false {
70+
n := 0
71+
switch n {
72+
case 0:
73+
case 1:
74+
default:
75+
}
76+
} else {
77+
n := 0
78+
switch n {
79+
case 0:
80+
case 1:
81+
default:
82+
}
83+
}
84+
} else if false {
85+
if false {
86+
n := 0
87+
switch n {
88+
case 0:
89+
case 1:
90+
default:
91+
}
92+
} else if false {
93+
n := 0
94+
switch n {
95+
case 0:
96+
case 1:
97+
default:
98+
}
99+
} else if false {
100+
n := 0
101+
switch n {
102+
case 0:
103+
case 1:
104+
default:
105+
}
106+
} else if false {
107+
n := 0
108+
switch n {
109+
case 0:
110+
case 1:
111+
default:
112+
}
113+
} else {
114+
n := 0
115+
switch n {
116+
case 0:
117+
case 1:
118+
default:
119+
}
120+
}
121+
} else if false {
122+
if false {
123+
n := 0
124+
switch n {
125+
case 0:
126+
case 1:
127+
default:
128+
}
129+
} else if false {
130+
n := 0
131+
switch n {
132+
case 0:
133+
case 1:
134+
default:
135+
}
136+
} else if false {
137+
n := 0
138+
switch n {
139+
case 0:
140+
case 1:
141+
default:
142+
}
143+
} else if false {
144+
n := 0
145+
switch n {
146+
case 0:
147+
case 1:
148+
default:
149+
}
150+
} else {
151+
n := 0
152+
switch n {
153+
case 0:
154+
case 1:
155+
default:
156+
}
157+
}
158+
} else {
159+
if false {
160+
n := 0
161+
switch n {
162+
case 0:
163+
case 1:
164+
default:
165+
}
166+
} else if false {
167+
n := 0
168+
switch n {
169+
case 0:
170+
case 1:
171+
default:
172+
}
173+
} else if false {
174+
n := 0
175+
switch n {
176+
case 0:
177+
case 1:
178+
default:
179+
}
180+
} else if false {
181+
n := 0
182+
switch n {
183+
case 0:
184+
case 1:
185+
default:
186+
}
187+
} else {
188+
n := 0
189+
switch n {
190+
case 0:
191+
case 1:
192+
default:
193+
}
194+
}
195+
}
196+
}
197+
}

0 commit comments

Comments
 (0)