aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/sashamelentyev/interfacebloat/pkg
diff options
context:
space:
mode:
authorTaras Madan <tarasmadan@google.com>2023-02-22 22:16:50 +0100
committerTaras Madan <tarasmadan@google.com>2023-02-24 12:47:23 +0100
commit4165372ec8fd142475a4e35fd0cf4f8042132208 (patch)
tree21cd62211b4dd80bee469054c5b65db77342333c /vendor/github.com/sashamelentyev/interfacebloat/pkg
parent2b3ed821a493b8936c8bacfa6f8b4f1c90a00855 (diff)
dependencies: update
set go min requirements to 1.19 update dependencies update vendor
Diffstat (limited to 'vendor/github.com/sashamelentyev/interfacebloat/pkg')
-rw-r--r--vendor/github.com/sashamelentyev/interfacebloat/pkg/analyzer/analyzer.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/vendor/github.com/sashamelentyev/interfacebloat/pkg/analyzer/analyzer.go b/vendor/github.com/sashamelentyev/interfacebloat/pkg/analyzer/analyzer.go
new file mode 100644
index 000000000..4a6afdf8c
--- /dev/null
+++ b/vendor/github.com/sashamelentyev/interfacebloat/pkg/analyzer/analyzer.go
@@ -0,0 +1,57 @@
+package analyzer
+
+import (
+ "flag"
+ "go/ast"
+
+ "golang.org/x/tools/go/analysis"
+ "golang.org/x/tools/go/analysis/passes/inspect"
+ "golang.org/x/tools/go/ast/inspector"
+)
+
+const InterfaceMaxMethodsFlag = "max"
+
+const defaultMaxMethods = 10
+
+// New returns new interfacebloat analyzer.
+func New() *analysis.Analyzer {
+ return &analysis.Analyzer{
+ Name: "interfacebloat",
+ Doc: "A linter that checks the number of methods inside an interface.",
+ Run: run,
+ Flags: flags(),
+ Requires: []*analysis.Analyzer{inspect.Analyzer},
+ }
+}
+
+func flags() flag.FlagSet {
+ flags := flag.NewFlagSet("", flag.ExitOnError)
+ flags.Int(InterfaceMaxMethodsFlag, 10, "maximum number of methods")
+ return *flags
+}
+
+func run(pass *analysis.Pass) (interface{}, error) {
+ maxMethods, ok := pass.Analyzer.Flags.Lookup(InterfaceMaxMethodsFlag).Value.(flag.Getter).Get().(int)
+ if !ok {
+ maxMethods = defaultMaxMethods
+ }
+
+ insp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
+
+ filter := []ast.Node{
+ (*ast.InterfaceType)(nil),
+ }
+
+ insp.Preorder(filter, func(node ast.Node) {
+ i, ok := node.(*ast.InterfaceType)
+ if !ok {
+ return
+ }
+
+ if len(i.Methods.List) > maxMethods {
+ pass.Reportf(node.Pos(), `the interface has more than %d methods: %d`, maxMethods, len(i.Methods.List))
+ }
+ })
+
+ return nil, nil
+}