Skip to content

Commit ebb60e6

Browse files
committed
feat: format switch tag
1 parent 36d8da2 commit ebb60e6

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

shorten/format.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func (s *Shortener) formatDecl(decl dst.Decl) {
7575
// formatStmt formats an AST statement node.
7676
// Among other examples, these include assignments, case clauses,
7777
// for statements, if statements, and select statements.
78+
//
79+
//nolint:funlen // the number of statements is expected.
7880
func (s *Shortener) formatStmt(stmt dst.Stmt, force bool) {
7981
stmtType := reflect.TypeOf(stmt)
8082

@@ -145,6 +147,12 @@ func (s *Shortener) formatStmt(stmt dst.Stmt, force bool) {
145147
case *dst.SwitchStmt:
146148
s.formatStmt(st.Body, false)
147149

150+
// Ignored: st.Init
151+
152+
if st.Tag != nil {
153+
s.formatExpr(st.Tag, shouldShorten, false)
154+
}
155+
148156
case *dst.TypeSwitchStmt:
149157
s.formatStmt(st.Body, false)
150158

shorten/testdata/switch_blocks/switch_blocks.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package fixtures
22

33
import "fmt"
44

5-
func testFunc2() {
5+
func _(a int) error {
66
c := make(chan int)
77

88
for {
@@ -17,4 +17,30 @@ func testFunc2() {
1717

1818
break
1919
}
20+
21+
return nil
22+
}
23+
24+
func _() {
25+
var pneumonoultramicroscopicsilicovolcanoconiosis string
26+
var floccinaucinihilipilification string
27+
28+
switch myfunction(pneumonoultramicroscopicsilicovolcanoconiosis, floccinaucinihilipilification) {
29+
case "a":
30+
fmt.Println("a")
31+
}
32+
}
33+
34+
func _() {
35+
var pneumonoultramicroscopicsilicovolcanoconiosis string
36+
var floccinaucinihilipilification string
37+
38+
switch a := "taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu"; myfunction(pneumonoultramicroscopicsilicovolcanoconiosis, floccinaucinihilipilification) {
39+
case "a":
40+
fmt.Println(a)
41+
}
42+
}
43+
44+
func myfunction(a, b string) string {
45+
return ""
2046
}

shorten/testdata/switch_blocks/switch_blocks.go.golden

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package fixtures
22

33
import "fmt"
44

5-
func testFunc2() {
5+
func _(a int) error {
66
c := make(chan int)
77

88
for {
@@ -25,4 +25,36 @@ func testFunc2() {
2525

2626
break
2727
}
28+
29+
return nil
30+
}
31+
32+
func _() {
33+
var pneumonoultramicroscopicsilicovolcanoconiosis string
34+
var floccinaucinihilipilification string
35+
36+
switch myfunction(
37+
pneumonoultramicroscopicsilicovolcanoconiosis,
38+
floccinaucinihilipilification,
39+
) {
40+
case "a":
41+
fmt.Println("a")
42+
}
43+
}
44+
45+
func _() {
46+
var pneumonoultramicroscopicsilicovolcanoconiosis string
47+
var floccinaucinihilipilification string
48+
49+
switch a := "taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu"; myfunction(
50+
pneumonoultramicroscopicsilicovolcanoconiosis,
51+
floccinaucinihilipilification,
52+
) {
53+
case "a":
54+
fmt.Println(a)
55+
}
56+
}
57+
58+
func myfunction(a, b string) string {
59+
return ""
2860
}

0 commit comments

Comments
 (0)