Skip to content

Commit dcd04b6

Browse files
authored
fix: format switch type statement
1 parent 1095088 commit dcd04b6

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

shorten/format.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,24 @@ func (s *Shortener) formatStmt(stmt dst.Stmt, force bool) {
145145
case *dst.SwitchStmt:
146146
s.formatStmt(st.Body, false)
147147

148+
case *dst.TypeSwitchStmt:
149+
s.formatStmt(st.Body, false)
150+
151+
case *dst.BadStmt, *dst.EmptyStmt, *dst.LabeledStmt,
152+
*dst.SendStmt, *dst.IncDecStmt, *dst.BranchStmt:
153+
// These statements are explicitly defined to improve switch cases exhaustiveness.
154+
// They may be handled in the future.
155+
if shouldShorten {
156+
s.logger.Debug(
157+
"got a statement type that is not shortened",
158+
slog.Any("stmt_type", stmtType),
159+
)
160+
}
161+
148162
default:
149163
if shouldShorten {
150164
s.logger.Debug(
151-
"got a statement type that can't be shortened",
165+
"got an unknown statement type",
152166
slog.Any("stmt_type", stmtType),
153167
)
154168
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package testdata
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/dave/dst"
7+
)
8+
9+
func _(node dst.Node) error {
10+
switch n := node.(type) {
11+
case dst.Decl:
12+
n.Decorations()
13+
14+
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))
15+
16+
case dst.Expr:
17+
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))
18+
19+
case dst.Stmt:
20+
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))
21+
22+
default:
23+
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))
24+
}
25+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package testdata
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/dave/dst"
7+
)
8+
9+
func _(node dst.Node) error {
10+
switch n := node.(type) {
11+
case dst.Decl:
12+
n.Decorations()
13+
14+
return fmt.Errorf(
15+
"This is a really long line that can be broken up twice %s %s",
16+
fmt.Sprintf(
17+
"This is a really long sub-line that should be broken up more because %s %s",
18+
"xxxx",
19+
"yyyy",
20+
),
21+
fmt.Sprintf("A short one %d", 3),
22+
)
23+
24+
case dst.Expr:
25+
return fmt.Errorf(
26+
"This is a really long line that can be broken up twice %s %s",
27+
fmt.Sprintf(
28+
"This is a really long sub-line that should be broken up more because %s %s",
29+
"xxxx",
30+
"yyyy",
31+
),
32+
fmt.Sprintf("A short one %d", 3),
33+
)
34+
35+
case dst.Stmt:
36+
return fmt.Errorf(
37+
"This is a really long line that can be broken up twice %s %s",
38+
fmt.Sprintf(
39+
"This is a really long sub-line that should be broken up more because %s %s",
40+
"xxxx",
41+
"yyyy",
42+
),
43+
fmt.Sprintf("A short one %d", 3),
44+
)
45+
46+
default:
47+
return fmt.Errorf(
48+
"This is a really long line that can be broken up twice %s %s",
49+
fmt.Sprintf(
50+
"This is a really long sub-line that should be broken up more because %s %s",
51+
"xxxx",
52+
"yyyy",
53+
),
54+
fmt.Sprintf("A short one %d", 3),
55+
)
56+
}
57+
}

0 commit comments

Comments
 (0)