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
16 changes: 15 additions & 1 deletion shorten/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,24 @@ func (s *Shortener) formatStmt(stmt dst.Stmt, force bool) {
case *dst.SwitchStmt:
s.formatStmt(st.Body, false)

case *dst.TypeSwitchStmt:
s.formatStmt(st.Body, false)

case *dst.BadStmt, *dst.EmptyStmt, *dst.LabeledStmt,
*dst.SendStmt, *dst.IncDecStmt, *dst.BranchStmt:
// These statements are explicitly defined to improve switch cases exhaustiveness.
// They may be handled in the future.
if shouldShorten {
s.logger.Debug(
"got a statement type that is not shortened",
slog.Any("stmt_type", stmtType),
)
}

default:
if shouldShorten {
s.logger.Debug(
"got a statement type that can't be shortened",
"got an unknown statement type",
slog.Any("stmt_type", stmtType),
)
}
Expand Down
25 changes: 25 additions & 0 deletions shorten/testdata/switch_type/switch_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package testdata

import (
"fmt"

"github.com/dave/dst"
)

func _(node dst.Node) error {
switch n := node.(type) {
case dst.Decl:
n.Decorations()

return fmt.Errorf("This is a really long line that can be broken up twice %s %s", fmt.Sprintf("This is a really long sub-line that should be broken up more because %s %s", "xxxx", "yyyy"), fmt.Sprintf("A short one %d", 3))

case dst.Expr:
return fmt.Errorf("This is a really long line that can be broken up twice %s %s", fmt.Sprintf("This is a really long sub-line that should be broken up more because %s %s", "xxxx", "yyyy"), fmt.Sprintf("A short one %d", 3))

case dst.Stmt:
return fmt.Errorf("This is a really long line that can be broken up twice %s %s", fmt.Sprintf("This is a really long sub-line that should be broken up more because %s %s", "xxxx", "yyyy"), fmt.Sprintf("A short one %d", 3))

default:
return fmt.Errorf("This is a really long line that can be broken up twice %s %s", fmt.Sprintf("This is a really long sub-line that should be broken up more because %s %s", "xxxx", "yyyy"), fmt.Sprintf("A short one %d", 3))
}
}
57 changes: 57 additions & 0 deletions shorten/testdata/switch_type/switch_type.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package testdata

import (
"fmt"

"github.com/dave/dst"
)

func _(node dst.Node) error {
switch n := node.(type) {
case dst.Decl:
n.Decorations()

return fmt.Errorf(
"This is a really long line that can be broken up twice %s %s",
fmt.Sprintf(
"This is a really long sub-line that should be broken up more because %s %s",
"xxxx",
"yyyy",
),
fmt.Sprintf("A short one %d", 3),
)

case dst.Expr:
return fmt.Errorf(
"This is a really long line that can be broken up twice %s %s",
fmt.Sprintf(
"This is a really long sub-line that should be broken up more because %s %s",
"xxxx",
"yyyy",
),
fmt.Sprintf("A short one %d", 3),
)

case dst.Stmt:
return fmt.Errorf(
"This is a really long line that can be broken up twice %s %s",
fmt.Sprintf(
"This is a really long sub-line that should be broken up more because %s %s",
"xxxx",
"yyyy",
),
fmt.Sprintf("A short one %d", 3),
)

default:
return fmt.Errorf(
"This is a really long line that can be broken up twice %s %s",
fmt.Sprintf(
"This is a really long sub-line that should be broken up more because %s %s",
"xxxx",
"yyyy",
),
fmt.Sprintf("A short one %d", 3),
)
}
}
Loading