Skip to content

Commit bb82273

Browse files
NiseVoidjirfag
authored andcommitted
Add funlen linter (#603)
1 parent 375a5a8 commit bb82273

File tree

13 files changed

+291
-0
lines changed

13 files changed

+291
-0
lines changed

.golangci.example.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ linters-settings:
7878
# path to a file containing a list of functions to exclude from checking
7979
# see https://github.com/kisielk/errcheck#excluding-functions for details
8080
exclude: /path/to/file.txt
81+
82+
funlen:
83+
lines: 60
84+
statements: 40
85+
8186
govet:
8287
# report about shadowed variables
8388
check-shadowing: true

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ linters:
4646
- maligned
4747
- prealloc
4848
- gochecknoglobals
49+
- funlen
4950

5051
run:
5152
skip-dirs:

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ Disabled by default linters:
208208
bodyclose: checks whether HTTP response body is closed successfully [fast: false, auto-fix: false]
209209
depguard: Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false]
210210
dupl: Tool for code clone detection [fast: true, auto-fix: false]
211+
funlen: Tool for detection of long functions [fast: true, auto-fix: false]
211212
gochecknoglobals: Checks that no globals are present in Go code [fast: true, auto-fix: false]
212213
gochecknoinits: Checks that no init functions are present in Go code [fast: true, auto-fix: false]
213214
goconst: Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
@@ -440,6 +441,7 @@ golangci-lint help linters
440441
- [gocritic](https://github.com/go-critic/go-critic) - The most opinionated Go source code linter
441442
- [gochecknoinits](https://github.com/leighmcculloch/gochecknoinits) - Checks that no init functions are present in Go code
442443
- [gochecknoglobals](https://github.com/leighmcculloch/gochecknoglobals) - Checks that no globals are present in Go code
444+
- [funlen](https://github.com/ultraware/funlen) - Tool for detection of long functions
443445
444446
## Configuration
445447
@@ -629,6 +631,11 @@ linters-settings:
629631
# path to a file containing a list of functions to exclude from checking
630632
# see https://github.com/kisielk/errcheck#excluding-functions for details
631633
exclude: /path/to/file.txt
634+
635+
funlen:
636+
lines: 60
637+
statements: 40
638+
632639
govet:
633640
# report about shadowed variables
634641
check-shadowing: true
@@ -859,6 +866,7 @@ linters:
859866
- maligned
860867
- prealloc
861868
- gochecknoglobals
869+
- funlen
862870
863871
run:
864872
skip-dirs:
@@ -999,6 +1007,7 @@ Thanks to developers and authors of used linters:
9991007
- [kyoh86](https://github.com/kyoh86)
10001008
- [go-critic](https://github.com/go-critic)
10011009
- [leighmcculloch](https://github.com/leighmcculloch)
1010+
- [ultraware](https://github.com/ultraware)
10021011
10031012
## Changelog
10041013

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ require (
4949
github.com/spf13/pflag v1.0.1
5050
github.com/spf13/viper v1.0.2
5151
github.com/stretchr/testify v1.2.2
52+
github.com/ultraware/funlen v0.0.1
5253
github.com/timakin/bodyclose v0.0.0-00010101000000-87058b9bfcec
5354
github.com/valyala/quicktemplate v1.1.1
5455
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ github.com/spf13/viper v1.0.2 h1:Ncr3ZIuJn322w2k1qmzXDnkLAdQMlJqBa9kfAH+irso=
164164
github.com/spf13/viper v1.0.2/go.mod h1:A8kyI5cUJhb8N+3pkfONlcEcZbueH6nhAm0Fq7SrnBM=
165165
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
166166
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
167+
github.com/ultraware/funlen v0.0.1 h1:UeC9tpM4wNWzUJfan8z9sFE4QCzjjzlCZmuJN+aOkH0=
168+
github.com/ultraware/funlen v0.0.1/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
167169
github.com/timakin/bodyclose v0.0.0-00010101000000-87058b9bfcec h1:Ha5Eixh5Dgi14hDFFWsxoB/jR95rHjB1biKdK9VKkbQ=
168170
github.com/timakin/bodyclose v0.0.0-00010101000000-87058b9bfcec/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk=
169171
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=

pkg/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ type LintersSettings struct {
164164
Unused struct {
165165
CheckExported bool `mapstructure:"check-exported"`
166166
}
167+
Funlen struct {
168+
Lines int
169+
Statements int
170+
}
167171

168172
Lll LllSettings
169173
Unparam UnparamSettings

pkg/golinters/funlen.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package golinters
2+
3+
import (
4+
"context"
5+
"go/token"
6+
7+
"github.com/golangci/golangci-lint/pkg/lint/linter"
8+
"github.com/golangci/golangci-lint/pkg/result"
9+
10+
"github.com/ultraware/funlen"
11+
)
12+
13+
type Funlen struct{}
14+
15+
func (Funlen) Name() string {
16+
return "funlen"
17+
}
18+
19+
func (Funlen) Desc() string {
20+
return "Tool for detection of long functions"
21+
}
22+
23+
func (f Funlen) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
24+
var issues []funlen.Message
25+
for _, file := range lintCtx.ASTCache.GetAllValidFiles() {
26+
issues = append(issues, funlen.Run(file.F, file.Fset, lintCtx.Settings().Funlen.Lines, lintCtx.Settings().Funlen.Statements)...)
27+
}
28+
29+
if len(issues) == 0 {
30+
return nil, nil
31+
}
32+
33+
res := make([]result.Issue, len(issues))
34+
for k, i := range issues {
35+
res[k] = result.Issue{
36+
Pos: token.Position{
37+
Filename: i.Pos.Filename,
38+
Line: i.Pos.Line,
39+
},
40+
Text: i.Message,
41+
FromLinter: f.Name(),
42+
}
43+
}
44+
45+
return res, nil
46+
}

pkg/lint/lintersdb/manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
237237
WithPresets(linter.PresetStyle).
238238
WithSpeed(10).
239239
WithURL("https://github.com/leighmcculloch/gochecknoglobals"),
240+
linter.NewConfig(golinters.Funlen{}).
241+
WithPresets(linter.PresetStyle).
242+
WithSpeed(10).
243+
WithURL("https://github.com/ultraware/funlen"),
240244
}
241245

242246
isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""

test/testdata/funlen.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//args: -Efunlen
2+
//config: linters-settings.funlen.lines=20
3+
//config: linters-settings.funlen.statements=10
4+
package testdata
5+
6+
func TooManyLines() { // ERROR "Function 'TooManyLines' is too long \(22 > 20\)"
7+
t := struct {
8+
A string
9+
B string
10+
C string
11+
D string
12+
E string
13+
F string
14+
G string
15+
H string
16+
I string
17+
}{
18+
`a`,
19+
`b`,
20+
`c`,
21+
`d`,
22+
`e`,
23+
`f`,
24+
`g`,
25+
`h`,
26+
`i`,
27+
}
28+
_ = t
29+
}
30+
31+
func TooManyStatements() { // ERROR "Function 'TooManyStatements' has too many statements \(11 > 10\)"
32+
a := 1
33+
b := a
34+
c := b
35+
d := c
36+
e := d
37+
f := e
38+
g := f
39+
h := g
40+
i := h
41+
j := i
42+
_ = j
43+
}

vendor/github.com/ultraware/funlen/LICENSE

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/ultraware/funlen/README.md

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/ultraware/funlen/main.go

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ github.com/stretchr/testify/assert
183183
github.com/stretchr/testify/require
184184
# github.com/timakin/bodyclose v0.0.0-00010101000000-87058b9bfcec
185185
github.com/timakin/bodyclose/passes/bodyclose
186+
# github.com/ultraware/funlen v0.0.1
187+
github.com/ultraware/funlen
186188
# github.com/valyala/bytebufferpool v1.0.0
187189
github.com/valyala/bytebufferpool
188190
# github.com/valyala/quicktemplate v1.1.1

0 commit comments

Comments
 (0)