Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion shorten/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (s *Shortener) formatExpr(expr dst.Expr, force, isChain bool) {
}

case *dst.CompositeLit:
if shouldShorten {
if shouldShorten || annotation.HasRecursive(e) {
for i, element := range e.Elts {
if i == 0 {
element.Decorations().Before = dst.NewLine
Expand Down
3 changes: 3 additions & 0 deletions shorten/internal/annotation/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func HasRecursive[T dst.Node](node T) bool {

case *dst.FieldList:
return slices.ContainsFunc(n.List, HasRecursive)

case *dst.CompositeLit:
return slices.ContainsFunc(n.Elts, HasRecursive)
}

return false
Expand Down
30 changes: 30 additions & 0 deletions shorten/testdata/struct_compositelit/struct_compositelit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package struct_compositelit

type Foo struct {
field1 string
field2 string
field3 []string
field4 string
field5 string
}

const floccinaucinihilipilification = "floccinaucinihilipilification"
const supercalifragilisticexpialidocious = "Supercalifragilisticexpialidocious"

var _ = &Foo{
field1: "Pneumonoultramicroscopicsilicovolcanoconiosis", field2: helloWorld(supercalifragilisticexpialidocious), field3: []string{"list1"}, field4: floccinaucinihilipilification, field5: "foo",
}

var _ = &Foo{
field1: "a", field2: helloWorld("a"), field3: []string{"list1"}, field4: "c", field5: "foo",
}

func helloWorld(_ string) string {
return "hello world"
}

func _() {
_ := &Foo{
field1: "Pneumonoultramicroscopicsilicovolcanoconiosis", field2: helloWorld(supercalifragilisticexpialidocious), field3: []string{"list1"}, field4: floccinaucinihilipilification, field5: "foo",
}
}
38 changes: 38 additions & 0 deletions shorten/testdata/struct_compositelit/struct_compositelit.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package struct_compositelit

type Foo struct {
field1 string
field2 string
field3 []string
field4 string
field5 string
}

const floccinaucinihilipilification = "floccinaucinihilipilification"
const supercalifragilisticexpialidocious = "Supercalifragilisticexpialidocious"

var _ = &Foo{
field1: "Pneumonoultramicroscopicsilicovolcanoconiosis",
field2: helloWorld(supercalifragilisticexpialidocious),
field3: []string{"list1"},
field4: floccinaucinihilipilification,
field5: "foo",
}

var _ = &Foo{
field1: "a", field2: helloWorld("a"), field3: []string{"list1"}, field4: "c", field5: "foo",
}

func helloWorld(_ string) string {
return "hello world"
}

func _() {
_ := &Foo{
field1: "Pneumonoultramicroscopicsilicovolcanoconiosis",
field2: helloWorld(supercalifragilisticexpialidocious),
field3: []string{"list1"},
field4: floccinaucinihilipilification,
field5: "foo",
}
}
Loading