Skip to content

Commit 4c856d9

Browse files
committed
Support maintidx
1 parent 95b9b23 commit 4c856d9

File tree

9 files changed

+449
-0
lines changed

9 files changed

+449
-0
lines changed

.golangci.example.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,10 @@ linters-settings:
11171117
force-short-decl-cuddling: false
11181118
strict-append: true
11191119

1120+
maintidx:
1121+
# show functions with maintainability index < N only.
1122+
under: 20
1123+
11201124
# The custom section can be used to define linter plugins to be loaded at runtime.
11211125
# See README doc for more info.
11221126
custom:

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ require (
9191
github.com/ultraware/whitespace v0.0.4
9292
github.com/uudashr/gocognit v1.0.5
9393
github.com/valyala/quicktemplate v1.7.0
94+
github.com/yagipy/maintidx v1.0.0
9495
github.com/yeya24/promlinter v0.1.0
9596
gitlab.com/bosi/decorder v0.2.1
9697
golang.org/x/tools v0.1.9-0.20211228192929-ee1ca4ffc4da

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+5
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ type LintersSettings struct {
128128
ImportAs ImportAsSettings
129129
Ireturn IreturnSettings
130130
Lll LllSettings
131+
MaintIdx MaintIdxSettings
131132
Makezero MakezeroSettings
132133
Maligned MalignedSettings
133134
Misspell MisspellSettings
@@ -389,6 +390,10 @@ type LllSettings struct {
389390
TabWidth int `mapstructure:"tab-width"`
390391
}
391392

393+
type MaintIdxSettings struct {
394+
Under int `mapstructure:"under"`
395+
}
396+
392397
type MakezeroSettings struct {
393398
Always bool
394399
}

pkg/golinters/maintidx.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package golinters
2+
3+
import (
4+
"github.com/yagipy/maintidx"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
9+
)
10+
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+
}
23+
}
24+
25+
return goanalysis.NewLinter(
26+
analyzer.Name,
27+
analyzer.Doc,
28+
[]*analysis.Analyzer{
29+
analyzer,
30+
},
31+
cfgMap,
32+
).WithLoadMode(goanalysis.LoadModeSyntax)
33+
}

pkg/lint/lintersdb/manager.go

+8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
114114
var ifshortCfg *config.IfshortSettings
115115
var importAsCfg *config.ImportAsSettings
116116
var ireturnCfg *config.IreturnSettings
117+
var maintIdxCfg *config.MaintIdxSettings
117118
var nilNilCfg *config.NilNilSettings
118119
var nlreturnCfg *config.NlreturnSettings
119120
var predeclaredCfg *config.PredeclaredSettings
@@ -143,6 +144,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
143144
ifshortCfg = &m.cfg.LintersSettings.Ifshort
144145
importAsCfg = &m.cfg.LintersSettings.ImportAs
145146
ireturnCfg = &m.cfg.LintersSettings.Ireturn
147+
maintIdxCfg = &m.cfg.LintersSettings.MaintIdx
146148
nilNilCfg = &m.cfg.LintersSettings.NilNil
147149
nlreturnCfg = &m.cfg.LintersSettings.Nlreturn
148150
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
@@ -439,6 +441,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
439441
WithSince("v1.8.0").
440442
WithPresets(linter.PresetStyle),
441443

444+
linter.NewConfig(golinters.NewMaintIdx()).
445+
WithSince("v1.1.0").
446+
WithPresets(linter.PresetComplexity).
447+
WithLoadForGoAnalysis().
448+
WithURL("https://github.com/yagipy/maintidx"),
449+
442450
linter.NewConfig(golinters.NewMakezero()).
443451
WithSince("v1.34.0").
444452
WithPresets(linter.PresetStyle, linter.PresetBugs).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
maintidx:
3+
under: 100

test/testdata/maintidx.go

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

0 commit comments

Comments
 (0)