Skip to content

Commit ac55ea7

Browse files
authored
add 2 options: ignore leading and/or trailing lines (#15)
* fix build of golang.org/x/tools v0.20.0 with 1.25.0 See golang/go#74462 Signed-off-by: Ivan Prisyazhnyy <john.koepi@gmail.com> * add 2 options: ignore leading and/or trailing lines -ignore-leading Do not check leading newlines -ignore-trailing Do not check trailing newlines Signed-off-by: Ivan Prisyazhnyy <john.koepi@gmail.com> --------- Signed-off-by: Ivan Prisyazhnyy <john.koepi@gmail.com>
1 parent 3d7a6d9 commit ac55ea7

File tree

9 files changed

+691
-27
lines changed

9 files changed

+691
-27
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,22 @@ Whitespace is a linter that checks for unnecessary newlines at the start and end
77
To install as a standalone linter, run `go install github.com/ultraware/whitespace/cmd/whitespace@latest`.
88

99
Whitespace is also included in [golangci-lint](https://github.com/golangci/golangci-lint/). Install it and enable whitespace.
10+
11+
## Usage
12+
13+
```
14+
15+
Example: ./whitespace ./...
16+
17+
Usage: whitespace [-flag] [package]
18+
19+
Configuration flags:
20+
-ignore-leading
21+
Do not check leading newlines
22+
-ignore-trailing
23+
Do not check trailing newlines
24+
-multi-func
25+
Check that multi line functions have a leading newline
26+
-multi-if
27+
Check that multi line if-statements have a leading newline
28+
```

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/ultraware/whitespace
22

3-
go 1.20
3+
go 1.24.0
44

5-
require golang.org/x/tools v0.20.0
5+
require golang.org/x/tools v0.38.0
66

77
require (
8-
golang.org/x/mod v0.17.0 // indirect
9-
golang.org/x/sync v0.7.0 // indirect
8+
golang.org/x/mod v0.29.0 // indirect
9+
golang.org/x/sync v0.17.0 // indirect
1010
)

go.sum

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
2-
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
3-
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
4-
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
5-
golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY=
6-
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
1+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3+
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
4+
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
5+
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
6+
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
7+
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
8+
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package whitespace
2+
3+
import "fmt"
4+
5+
func fn1(
6+
arg1 int,
7+
arg2 int,
8+
) {
9+
10+
fmt.Println("Hello, World")
11+
12+
if true &&
13+
false {
14+
15+
fmt.Println("Hello, World")
16+
}
17+
18+
_ = func(
19+
arg1 int,
20+
arg2 int,
21+
) {
22+
23+
fmt.Println("Hello, World")
24+
}
25+
26+
_ = func(
27+
arg1 int,
28+
arg2 int,
29+
) {
30+
_ = func(
31+
arg1 int,
32+
arg2 int,
33+
) {
34+
35+
fmt.Println("Hello, World")
36+
}
37+
}
38+
}
39+
40+
func fn2(
41+
arg1 int,
42+
arg2 int,
43+
) {
44+
45+
// A comment.
46+
fmt.Println("Hello, World")
47+
}
48+
49+
func fn3(
50+
arg1 int,
51+
arg2 int,
52+
) {
53+
54+
55+
56+
// A comment.
57+
fmt.Println("Hello, World")
58+
59+
if true {
60+
61+
62+
63+
fmt.Println("No comments")
64+
65+
66+
} // want "unnecessary trailing newline"
67+
// Also at end
68+
69+
70+
71+
} // want "unnecessary trailing newline"
72+
73+
// Regular func (FuncDecl) that's not `gofmt`:ed.
74+
func fn4() {
75+
76+
77+
78+
79+
fmt.Println("Hello, World")
80+
81+
if true {
82+
83+
84+
85+
fmt.Println("No comments")
86+
87+
88+
} // want "unnecessary trailing newline"
89+
90+
91+
92+
93+
} // want "unnecessary trailing newline"
94+
95+
// Regular func (FuncDecl) that's not `gofmt`:ed. with comments
96+
func fn5() {
97+
// A comment that should still exist after this
98+
// This one should also still exist
99+
100+
101+
102+
fmt.Println("Hello, World")
103+
104+
if true {
105+
// A comment that should still exist after this
106+
// This one should also still exist
107+
108+
109+
110+
fmt.Println("No comments")
111+
112+
113+
} // want "unnecessary trailing newline"
114+
115+
116+
117+
118+
} // want "unnecessary trailing newline"
119+
120+
// Regular func (FuncDecl) that's not `gofmt`:ed. with comment blocks
121+
func fn6() {
122+
/*
123+
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
124+
Curabitur ornare dolor at nulla ultrices cursus.
125+
Mauris pharetra metus ac condimentum sodales.
126+
Fusce viverra libero vitae tellus dictum, sed congue risus sodales.
127+
*/
128+
129+
130+
131+
fmt.Println("Hello, World")
132+
133+
if true {
134+
/*
135+
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
136+
Curabitur ornare dolor at nulla ultrices cursus.
137+
Mauris pharetra metus ac condimentum sodales.
138+
Fusce viverra libero vitae tellus dictum, sed congue risus sodales.
139+
*/
140+
141+
142+
fmt.Println("No comments")
143+
144+
145+
} // want "unnecessary trailing newline"
146+
147+
148+
149+
150+
} // want "unnecessary trailing newline"
151+
152+
func fn7() { /* Multiline spaning 1 line */
153+
154+
fmt.Println("No comments")
155+
}
156+
157+
func fn8() { /* Multiline spaning
158+
over several lines */
159+
160+
fmt.Println("No comments")
161+
}
162+
163+
func fn9() { /* Multiline spaning
164+
165+
over several lines */
166+
167+
fmt.Println("No comments")
168+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package whitespace
2+
3+
import "fmt"
4+
5+
func fn1(
6+
arg1 int,
7+
arg2 int,
8+
) {
9+
10+
fmt.Println("Hello, World")
11+
12+
if true &&
13+
false {
14+
15+
fmt.Println("Hello, World")
16+
}
17+
18+
_ = func(
19+
arg1 int,
20+
arg2 int,
21+
) {
22+
23+
fmt.Println("Hello, World")
24+
}
25+
26+
_ = func(
27+
arg1 int,
28+
arg2 int,
29+
) {
30+
_ = func(
31+
arg1 int,
32+
arg2 int,
33+
) {
34+
35+
fmt.Println("Hello, World")
36+
}
37+
}
38+
}
39+
40+
func fn2(
41+
arg1 int,
42+
arg2 int,
43+
) {
44+
45+
// A comment.
46+
fmt.Println("Hello, World")
47+
}
48+
49+
func fn3(
50+
arg1 int,
51+
arg2 int,
52+
) {
53+
54+
55+
56+
// A comment.
57+
fmt.Println("Hello, World")
58+
59+
if true {
60+
61+
62+
63+
fmt.Println("No comments")
64+
} // want "unnecessary trailing newline"
65+
// Also at end
66+
} // want "unnecessary trailing newline"
67+
68+
// Regular func (FuncDecl) that's not `gofmt`:ed.
69+
func fn4() {
70+
71+
72+
73+
74+
fmt.Println("Hello, World")
75+
76+
if true {
77+
78+
79+
80+
fmt.Println("No comments")
81+
} // want "unnecessary trailing newline"
82+
} // want "unnecessary trailing newline"
83+
84+
// Regular func (FuncDecl) that's not `gofmt`:ed. with comments
85+
func fn5() {
86+
// A comment that should still exist after this
87+
// This one should also still exist
88+
89+
fmt.Println("Hello, World")
90+
91+
if true {
92+
// A comment that should still exist after this
93+
// This one should also still exist
94+
95+
fmt.Println("No comments")
96+
} // want "unnecessary trailing newline"
97+
} // want "unnecessary trailing newline"
98+
99+
// Regular func (FuncDecl) that's not `gofmt`:ed. with comment blocks
100+
func fn6() {
101+
/*
102+
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
103+
Curabitur ornare dolor at nulla ultrices cursus.
104+
Mauris pharetra metus ac condimentum sodales.
105+
Fusce viverra libero vitae tellus dictum, sed congue risus sodales.
106+
*/
107+
108+
fmt.Println("Hello, World")
109+
110+
if true {
111+
/*
112+
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
113+
Curabitur ornare dolor at nulla ultrices cursus.
114+
Mauris pharetra metus ac condimentum sodales.
115+
Fusce viverra libero vitae tellus dictum, sed congue risus sodales.
116+
*/
117+
118+
fmt.Println("No comments")
119+
} // want "unnecessary trailing newline"
120+
} // want "unnecessary trailing newline"
121+
122+
func fn7() { /* Multiline spaning 1 line */
123+
124+
fmt.Println("No comments")
125+
}
126+
127+
func fn8() { /* Multiline spaning
128+
over several lines */
129+
130+
fmt.Println("No comments")
131+
}
132+
133+
func fn9() { /* Multiline spaning
134+
135+
over several lines */
136+
137+
fmt.Println("No comments")
138+
}

0 commit comments

Comments
 (0)