Skip to content

Commit 0423af1

Browse files
authored
Improve fixer for DeclStmt (#175)
* Improve fixer for `DeclStmt` * Stop when hitting an empty line or comment * Add test with comments above and after * Add test for type spec * Update docs * Add test mixing statement types * Clean up decl grouping impl * Avoid overlapping fixes when checking for intersection * Clarify test comment * Add comment explaining why we skip nodes with comments
1 parent e18f93d commit 0423af1

File tree

6 files changed

+417
-47
lines changed

6 files changed

+417
-47
lines changed

CHECKS.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ together they should be declared in the same group with parenthesis into a
121121
single statement. The benefit of this is that it also aligns the declaration or
122122
assignment increasing readability.
123123

124-
> **NOTE** The fixer can't do smart adjustments and currently only add
125-
> whitespaces.
126-
> This is tracked in [#21](https://github.com/bombsimon/wsl/issues/121)
124+
> **NOTE** The fixer can't do smart adjustments if there are comments on the
125+
> same line as the declaration.
127126
128127
<table>
129128
<thead><tr><th>Bad</th><th>Good</th></tr></thead>

analyzer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (wa *wslAnalyzer) run(pass *analysis.Pass) (any, error) {
117117
textEdits = append(textEdits, analysis.TextEdit{
118118
Pos: f.fixRangeStart,
119119
End: f.fixRangeEnd,
120-
NewText: []byte("\n"),
120+
NewText: f.fix,
121121
})
122122
}
123123

cursor.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ func (c *Cursor) SetChecker(ct CheckType) {
2525
c.checkType = ct
2626
}
2727

28+
func (c *Cursor) NextNode() ast.Node {
29+
defer c.Save()()
30+
31+
var nextNode ast.Node
32+
if c.Next() {
33+
nextNode = c.Stmt()
34+
}
35+
36+
return nextNode
37+
}
38+
2839
func (c *Cursor) Next() bool {
2940
if c.currentIdx >= len(c.statements)-1 {
3041
return false
Lines changed: 119 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,139 @@
11
package testpkg
22

33
func fn1() {
4+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
45
var a = 1
5-
var b = 2 // want `missing whitespace above this line \(never cuddle decl\)`
6+
var b = 2
67

8+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
79
const c = 3
8-
const d = 4 // want `missing whitespace above this line \(never cuddle decl\)`
10+
const d = 4
911

12+
// want +3 `missing whitespace above this line \(never cuddle decl\)`
13+
// want +3 `missing whitespace above this line \(invalid statement above assign\)`
1014
e := 5
11-
var f = 6 // want `missing whitespace above this line \(never cuddle decl\)`
12-
g := 7 // want `missing whitespace above this line \(invalid statement above assign\)`
15+
var f = 6
16+
g := 7
17+
18+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
19+
var a = 1
20+
var b = a
1321
}
1422

1523
func fn2() {
16-
var a = 1
17-
var b = a // want `missing whitespace above this line \(never cuddle decl\)`
24+
var x = func() { // want +1 `unnecessary whitespace \(leading-whitespace\)`
25+
26+
return 1
27+
}()
1828
}
1929

2030
func fn3() {
21-
a := 1
22-
var b = a // want `missing whitespace above this line \(never cuddle decl\)`
31+
// want +4 `missing whitespace above this line \(never cuddle decl\)`
32+
// want +4 `missing whitespace above this line \(never cuddle decl\)`
33+
// want +4 `missing whitespace above this line \(never cuddle decl\)`
34+
var a = 1
35+
var b = 2
36+
var c = 3
37+
var d = 4
2338
}
2439

2540
func fn4() {
26-
var x = func() { // want +1 `unnecessary whitespace \(leading-whitespace\)`
41+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
42+
var (
43+
a = 1
44+
b = 2
45+
)
46+
var (
47+
c = 3
48+
d = 4
49+
)
50+
}
2751

52+
func fn5() {
53+
// want +7 `missing whitespace above this line \(never cuddle decl\)`
54+
// want +7 `missing whitespace above this line \(never cuddle decl\)`
55+
// want +9 `missing whitespace above this line \(never cuddle decl\)`
56+
var (
57+
a = 1
58+
b = 2
59+
)
60+
var x int
61+
var z = func() int {
2862
return 1
29-
}()
63+
}
64+
var (
65+
c = 3
66+
d = 4
67+
)
68+
}
69+
70+
func fn6() {
71+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
72+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
73+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
74+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
75+
var a = 1
76+
var b = 2
77+
var c = 3 // test
78+
var d = 4
79+
var e = 5
80+
}
81+
82+
func fn7() {
83+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
84+
var a = 1
85+
var b = 2
86+
87+
var c = 3
88+
89+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
90+
var d = 4
91+
var e = 5
92+
}
93+
94+
func fn8() {
95+
// want +3 `missing whitespace above this line \(never cuddle decl\)`
96+
// Comment above
97+
var g = 7
98+
var h = 8
99+
// Comment after
100+
}
101+
102+
func fn9() {
103+
type S1 struct {
104+
N int
105+
}
106+
type S2 struct { // want `missing whitespace above this line \(never cuddle decl\)`
107+
N int
108+
}
109+
}
110+
111+
func fn10() {
112+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
113+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
114+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
115+
a := 1
116+
b := 2
117+
var c int
118+
var d = "string"
119+
var e string = "string"
120+
f := 3 // want `missing whitespace above this line \(invalid statement above assign\)`
121+
}
122+
123+
func fn11() {
124+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
125+
var a int
126+
var b int
127+
if b > 0 { // want `missing whitespace above this line \(too many statements above if\)`
128+
_ = 1
129+
}
130+
}
131+
132+
func fn12() {
133+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
134+
var a int
135+
var b int // Not grouped due to this comment
136+
if b > 0 {
137+
_ = 1
138+
}
30139
}
Lines changed: 146 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,165 @@
11
package testpkg
22

33
func fn1() {
4-
var a = 1
4+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
5+
var (
6+
a = 1
7+
b = 2
8+
)
59

6-
var b = 2 // want `missing whitespace above this line \(never cuddle decl\)`
7-
8-
const c = 3
9-
10-
const d = 4 // want `missing whitespace above this line \(never cuddle decl\)`
10+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
11+
const (
12+
c = 3
13+
d = 4
14+
)
1115

16+
// want +3 `missing whitespace above this line \(never cuddle decl\)`
17+
// want +3 `missing whitespace above this line \(invalid statement above assign\)`
1218
e := 5
1319

14-
var f = 6 // want `missing whitespace above this line \(never cuddle decl\)`
20+
var f = 6
21+
22+
g := 7
1523

16-
g := 7 // want `missing whitespace above this line \(invalid statement above assign\)`
24+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
25+
var (
26+
a = 1
27+
b = a
28+
)
1729
}
1830

1931
func fn2() {
20-
var a = 1
21-
22-
var b = a // want `missing whitespace above this line \(never cuddle decl\)`
32+
var x = func() { // want +1 `unnecessary whitespace \(leading-whitespace\)`
33+
return 1
34+
}()
2335
}
2436

2537
func fn3() {
38+
// want +4 `missing whitespace above this line \(never cuddle decl\)`
39+
// want +4 `missing whitespace above this line \(never cuddle decl\)`
40+
// want +4 `missing whitespace above this line \(never cuddle decl\)`
41+
var (
42+
a = 1
43+
b = 2
44+
c = 3
45+
d = 4
46+
)
47+
}
48+
49+
func fn4() {
50+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
51+
var (
52+
a = 1
53+
b = 2
54+
c = 3
55+
d = 4
56+
)
57+
}
58+
59+
func fn5() {
60+
// want +7 `missing whitespace above this line \(never cuddle decl\)`
61+
// want +7 `missing whitespace above this line \(never cuddle decl\)`
62+
// want +9 `missing whitespace above this line \(never cuddle decl\)`
63+
var (
64+
a = 1
65+
b = 2
66+
x int
67+
z = func() int {
68+
return 1
69+
}
70+
c = 3
71+
d = 4
72+
)
73+
}
74+
75+
func fn6() {
76+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
77+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
78+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
79+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
80+
var (
81+
a = 1
82+
b = 2
83+
)
84+
85+
var c = 3 // test
86+
87+
var (
88+
d = 4
89+
e = 5
90+
)
91+
}
92+
93+
func fn7() {
94+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
95+
var (
96+
a = 1
97+
b = 2
98+
)
99+
100+
var c = 3
101+
102+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
103+
var (
104+
d = 4
105+
e = 5
106+
)
107+
}
108+
109+
func fn8() {
110+
// want +3 `missing whitespace above this line \(never cuddle decl\)`
111+
// Comment above
112+
var (
113+
g = 7
114+
h = 8
115+
)
116+
// Comment after
117+
}
118+
119+
func fn9() {
120+
type S1 struct {
121+
N int
122+
}
123+
124+
type S2 struct { // want `missing whitespace above this line \(never cuddle decl\)`
125+
N int
126+
}
127+
}
128+
129+
func fn10() {
130+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
131+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
132+
// want +5 `missing whitespace above this line \(never cuddle decl\)`
26133
a := 1
134+
b := 2
27135

28-
var b = a // want `missing whitespace above this line \(never cuddle decl\)`
136+
var (
137+
c int
138+
d = "string"
139+
e string = "string"
140+
)
141+
142+
f := 3 // want `missing whitespace above this line \(invalid statement above assign\)`
29143
}
30144

31-
func fn4() {
32-
var x = func() { // want +1 `unnecessary whitespace \(leading-whitespace\)`
33-
return 1
34-
}()
145+
func fn11() {
146+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
147+
var (
148+
a int
149+
b int
150+
)
151+
152+
if b > 0 { // want `missing whitespace above this line \(too many statements above if\)`
153+
_ = 1
154+
}
155+
}
156+
157+
func fn12() {
158+
// want +2 `missing whitespace above this line \(never cuddle decl\)`
159+
var a int
160+
161+
var b int // Not grouped due to this comment
162+
if b > 0 {
163+
_ = 1
164+
}
35165
}

0 commit comments

Comments
 (0)