Skip to content

Commit 1702936

Browse files
Merge pull request #1 from ldez/feat/improve
2 parents fde710e + cf16515 commit 1702936

File tree

6 files changed

+170
-66
lines changed

6 files changed

+170
-66
lines changed

.github/workflows/ci.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
run:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
17+
- name: Install Go
18+
uses: actions/setup-go@v3
19+
with:
20+
go-version: 1.19
21+
22+
- name: Lint
23+
uses: golangci/[email protected]
24+
with:
25+
version: latest
26+
args: --timeout 5m
27+
28+
- name: Go Format
29+
run: gofmt -s -w . && git diff --exit-code
30+
31+
- name: Go Tidy
32+
run: go mod tidy && git diff --exit-code
33+
34+
- name: Go Mod
35+
run: go mod download
36+
37+
- name: Go Build
38+
run: go build -v ./...
39+
40+
- name: Go Test
41+
run: go test -v -race ./...

go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ module github.com/sashamelentyev/interfacebloat
22

33
go 1.18
44

5-
require (
6-
github.com/sashamelentyev/usestdlibvars v1.7.0
7-
golang.org/x/tools v0.1.12
8-
)
5+
require golang.org/x/tools v0.1.12
96

107
require (
118
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/sashamelentyev/usestdlibvars v1.7.0 h1:jjDzfl4RjcUWer1EwhNyipT+f+LL0HmcuGF0jDhb7sM=
2-
github.com/sashamelentyev/usestdlibvars v1.7.0/go.mod h1:BFt7b5mSVHaaa26ZupiNRV2ODViQBxZZVhtAxAJRrjs=
31
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
42
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
53
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"golang.org/x/tools/go/analysis/singlechecker"
55

6-
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer"
6+
"github.com/sashamelentyev/interfacebloat/pkg/analyzer"
77
)
88

99
func main() {

pkg/analyzer/analyzer.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ package analyzer
33
import (
44
"flag"
55
"go/ast"
6-
"go/token"
76

87
"golang.org/x/tools/go/analysis"
98
"golang.org/x/tools/go/analysis/passes/inspect"
109
"golang.org/x/tools/go/ast/inspector"
1110
)
1211

13-
const InterfaceLenFlag = "interface-len"
12+
const InterfaceMaxMethodsFlag = "max"
1413

15-
const defaultInterfaceLen = 10
14+
const defaultMaxMethods = 10
1615

1716
// New returns new interfacebloat analyzer.
1817
func New() *analysis.Analyzer {
1918
return &analysis.Analyzer{
2019
Name: "interfacebloat",
21-
Doc: "A linter that checks length of interface.",
20+
Doc: "A linter that checks the number of methods inside an interface.",
2221
Run: run,
2322
Flags: flags(),
2423
Requires: []*analysis.Analyzer{inspect.Analyzer},
@@ -27,11 +26,16 @@ func New() *analysis.Analyzer {
2726

2827
func flags() flag.FlagSet {
2928
flags := flag.NewFlagSet("", flag.ExitOnError)
30-
flags.Int(InterfaceLenFlag, 10, "length of interface")
29+
flags.Int(InterfaceMaxMethodsFlag, 10, "maximum number of methods")
3130
return *flags
3231
}
3332

3433
func run(pass *analysis.Pass) (interface{}, error) {
34+
maxMethods, ok := pass.Analyzer.Flags.Lookup(InterfaceMaxMethodsFlag).Value.(flag.Getter).Get().(int)
35+
if !ok {
36+
maxMethods = defaultMaxMethods
37+
}
38+
3539
insp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
3640

3741
filter := []ast.Node{
@@ -43,23 +47,11 @@ func run(pass *analysis.Pass) (interface{}, error) {
4347
if !ok {
4448
return
4549
}
46-
interfaceLen := interfaceLen(pass, InterfaceLenFlag)
47-
if len(i.Methods.List) > interfaceLen {
48-
report(pass, node.Pos(), interfaceLen)
50+
51+
if len(i.Methods.List) > maxMethods {
52+
pass.Reportf(node.Pos(), `the interface has more than %d methods: %d`, maxMethods, len(i.Methods.List))
4953
}
5054
})
5155

5256
return nil, nil
5357
}
54-
55-
func interfaceLen(pass *analysis.Pass, name string) (interfaceLen int) {
56-
interfaceLen, ok := pass.Analyzer.Flags.Lookup(name).Value.(flag.Getter).Get().(int)
57-
if !ok {
58-
interfaceLen = defaultInterfaceLen
59-
}
60-
return
61-
}
62-
63-
func report(pass *analysis.Pass, pos token.Pos, interfaceLen int) {
64-
pass.Reportf(pos, `length of interface greater than %d`, interfaceLen)
65-
}

pkg/analyzer/testdata/src/a/a.go

Lines changed: 115 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,123 @@
11
package a
22

3-
type _ interface { // want "length of interface greater than 10"
4-
a()
5-
b()
6-
c()
7-
d()
8-
f()
9-
g()
10-
h()
11-
i()
12-
j()
13-
k()
14-
l()
15-
}
16-
17-
func _() {
18-
var _ interface { // want "length of interface greater than 10"
19-
a()
20-
b()
21-
c()
22-
d()
23-
f()
24-
g()
25-
h()
26-
i()
27-
j()
28-
k()
29-
l()
3+
type Example01 interface { // want "the interface has more than 10 methods: 11"
4+
a01()
5+
a02()
6+
a03()
7+
a04()
8+
a05()
9+
a06()
10+
a07()
11+
a08()
12+
a09()
13+
a10()
14+
a11()
15+
}
16+
17+
func Example02() {
18+
var _ interface { // want "the interface has more than 10 methods: 11"
19+
a01()
20+
a02()
21+
a03()
22+
a04()
23+
a05()
24+
a06()
25+
a07()
26+
a08()
27+
a09()
28+
a10()
29+
a11()
30+
}
31+
}
32+
33+
func Example03() interface { // want "the interface has more than 10 methods: 11"
34+
a01()
35+
a02()
36+
a03()
37+
a04()
38+
a05()
39+
a06()
40+
a07()
41+
a08()
42+
a09()
43+
a10()
44+
a11()
45+
} {
46+
return nil
47+
}
48+
49+
type Example04 struct {
50+
Foo interface { // want "the interface has more than 10 methods: 11"
51+
a01()
52+
a02()
53+
a03()
54+
a04()
55+
a05()
56+
a06()
57+
a07()
58+
a08()
59+
a09()
60+
a10()
61+
a11()
62+
}
63+
}
64+
65+
type Small01 interface {
66+
a01()
67+
a02()
68+
a03()
69+
a04()
70+
a05()
71+
}
72+
73+
type Small02 interface {
74+
a06()
75+
a07()
76+
a08()
77+
a09()
78+
a10()
79+
a11()
80+
}
81+
82+
type Example05 interface {
83+
Small01
84+
Small02
85+
}
86+
87+
type Example06 interface {
88+
interface { // want "the interface has more than 10 methods: 11"
89+
a01()
90+
a02()
91+
a03()
92+
a04()
93+
a05()
94+
a06()
95+
a07()
96+
a08()
97+
a09()
98+
a10()
99+
a11()
30100
}
31101
}
32102

33-
func __() interface { // want "length of interface greater than 10"
34-
a()
35-
b()
36-
c()
37-
d()
38-
f()
39-
g()
40-
h()
41-
i()
42-
j()
43-
k()
44-
l()
103+
type TypeGeneric interface {
104+
~uint8 | ~uint16 | ~uint32 | ~uint64 | uint |
105+
~int8 | ~int16 | ~int32 | ~int64 | int |
106+
~float32 | ~float64 |
107+
~string
108+
}
109+
110+
func ExampleNoProblem() interface {
111+
a01()
112+
a02()
113+
a03()
114+
a04()
115+
a05()
116+
a06()
117+
a07()
118+
a08()
119+
a09()
120+
a10()
45121
} {
46122
return nil
47123
}

0 commit comments

Comments
 (0)