From 7a46d91cd6007867310d389b38e8a1aa11fdd1b7 Mon Sep 17 00:00:00 2001 From: Brendan Gimby Date: Wed, 15 May 2024 21:09:53 -0600 Subject: [PATCH 1/4] Format interfaces in structs --- .goreleaser.yaml | 2 ++ annotation.go | 24 ++++++++++++++++++++++++ shortener.go | 5 +++++ tags.go | 2 ++ 4 files changed, 33 insertions(+) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 8817565..904b1d1 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -16,6 +16,8 @@ builds: - arm64 env: - GOAMD64=v3 + ldflags: + - -s -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser universal_binaries: - replace: true diff --git a/annotation.go b/annotation.go index 0790a30..fa43113 100644 --- a/annotation.go +++ b/annotation.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/dave/dst" + log "github.com/sirupsen/logrus" ) const annotationPrefix = "// __golines:shorten:" @@ -51,6 +52,12 @@ func HasAnnotationRecursive(node dst.Node) bool { } switch n := node.(type) { + case *dst.GenDecl: + for _, spec := range n.Specs { + if HasAnnotationRecursive(spec) { + return true + } + } case *dst.FuncDecl: if n.Type != nil && n.Type.Params != nil { for _, item := range n.Type.Params.List { @@ -59,8 +66,23 @@ func HasAnnotationRecursive(node dst.Node) bool { } } } + case *dst.StructType: + return HasAnnotationRecursive(n.Fields) + case *dst.FuncType: + yes := n.Params != nil && HasAnnotationRecursive(n.Params) + yes = yes || (n.TypeParams != nil && HasAnnotationRecursive(n.TypeParams)) + yes = yes || (n.Results != nil && HasAnnotationRecursive(n.Results)) + return yes + case *dst.TypeSpec: + return HasAnnotationRecursive(n.Type) case *dst.Field: return HasTailAnnotation(n) || HasAnnotationRecursive(n.Type) + case *dst.FieldList: + for _, field := range n.List { + if HasAnnotationRecursive(field) { + return true + } + } case *dst.SelectorExpr: return HasAnnotation(n.Sel) || HasAnnotation(n.X) case *dst.CallExpr: @@ -79,6 +101,8 @@ func HasAnnotationRecursive(node dst.Node) bool { return true } } + default: + log.Debugf("Couldn't analyze type for annotations: %+v", n) } return false diff --git a/shortener.go b/shortener.go index 144d78f..ba4a86c 100644 --- a/shortener.go +++ b/shortener.go @@ -556,6 +556,11 @@ func (s *Shortener) formatExpr(expr dst.Expr, force bool, isChain bool) { case *dst.SelectorExpr: s.formatExpr(e.X, shouldShorten, isChain) case *dst.StructType: + if HasAnnotationRecursive(e) && e.Fields != nil { + for _, field := range e.Fields.List { + s.formatExpr(field.Type, false, isChain) + } + } if s.config.ReformatTags { FormatStructTags(e.Fields) } diff --git a/tags.go b/tags.go index 933e995..958826e 100644 --- a/tags.go +++ b/tags.go @@ -8,6 +8,7 @@ import ( "github.com/dave/dst" "github.com/fatih/structtag" + log "github.com/sirupsen/logrus" ) var structTagRegexp = regexp.MustCompile("`([ ]*[a-zA-Z0-9_-]+:\".*\"[ ]*){2,}`") @@ -213,5 +214,6 @@ func getWidth(node dst.Node) (int, error) { return 1 + xWidth, nil } + log.Debugf("Could not get width of node %+v", node) return 0, fmt.Errorf("Could not get width of node %+v", node) } From 701f2bc39b4b56519865021323fd2981bec0dabe Mon Sep 17 00:00:00 2001 From: Brendan Gimby Date: Wed, 15 May 2024 21:10:07 -0600 Subject: [PATCH 2/4] Add test --- _fixtures/struct_interface.go | 15 +++++++++++++++ _fixtures/struct_interface__exp.go | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 _fixtures/struct_interface.go create mode 100644 _fixtures/struct_interface__exp.go diff --git a/_fixtures/struct_interface.go b/_fixtures/struct_interface.go new file mode 100644 index 0000000..e4c8342 --- /dev/null +++ b/_fixtures/struct_interface.go @@ -0,0 +1,15 @@ +package fixtures + +type MyStruct struct { + i Int + LongInterface interface { + SuperLongFunctionName(anArgument string, anotherReallyLongArgument string, superDuperLongArgument string, definitelyTheLongestArgument string) error + } +} + +type MyShortStruct struct { + s string + ShortInterface interface { + SuperShortFunctionName(shortArg string) error + } +} diff --git a/_fixtures/struct_interface__exp.go b/_fixtures/struct_interface__exp.go new file mode 100644 index 0000000..873bb68 --- /dev/null +++ b/_fixtures/struct_interface__exp.go @@ -0,0 +1,20 @@ +package fixtures + +type MyStruct struct { + i Int + LongInterface interface { + SuperLongFunctionName( + anArgument string, + anotherReallyLongArgument string, + superDuperLongArgument string, + definitelyTheLongestArgument string, + ) error + } +} + +type MyShortStruct struct { + s string + ShortInterface interface { + SuperShortFunctionName(shortArg string) error + } +} From ba8669fc5bab3e229b4ebd54eac52ea2bb6da9ef Mon Sep 17 00:00:00 2001 From: Brendan Gimby Date: Wed, 15 May 2024 21:17:05 -0600 Subject: [PATCH 3/4] rm goreleaser changes --- .goreleaser.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 904b1d1..8817565 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -16,8 +16,6 @@ builds: - arm64 env: - GOAMD64=v3 - ldflags: - - -s -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser universal_binaries: - replace: true From bb180199c4d459225ec5fdd1b66144a6ccc525fa Mon Sep 17 00:00:00 2001 From: Brendan Gimby Date: Wed, 15 May 2024 21:22:16 -0600 Subject: [PATCH 4/4] Rename yes -> hasAny --- annotation.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/annotation.go b/annotation.go index fa43113..94a81a9 100644 --- a/annotation.go +++ b/annotation.go @@ -69,10 +69,10 @@ func HasAnnotationRecursive(node dst.Node) bool { case *dst.StructType: return HasAnnotationRecursive(n.Fields) case *dst.FuncType: - yes := n.Params != nil && HasAnnotationRecursive(n.Params) - yes = yes || (n.TypeParams != nil && HasAnnotationRecursive(n.TypeParams)) - yes = yes || (n.Results != nil && HasAnnotationRecursive(n.Results)) - return yes + hasAny := n.Params != nil && HasAnnotationRecursive(n.Params) + hasAny = hasAny || (n.TypeParams != nil && HasAnnotationRecursive(n.TypeParams)) + hasAny = hasAny || (n.Results != nil && HasAnnotationRecursive(n.Results)) + return hasAny case *dst.TypeSpec: return HasAnnotationRecursive(n.Type) case *dst.Field: