Skip to content

Commit 21d052c

Browse files
authored
Rename check from newline-after-block to after-block (#228)
1 parent 6216ee8 commit 21d052c

19 files changed

+184
-181
lines changed

CHECKS.md

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Table of content
44

55
- [Checks](#checks)
6+
- [`after-block`](#after-block)
67
- [`append`](#append)
78
- [`assign`](#assign)
89
- [`assign-exclusive`](#assign-exclusive)
@@ -18,7 +19,6 @@
1819
- [`inc-dec`](#inc-dec)
1920
- [`label`](#label)
2021
- [`leading-whitespace`](#leading-whitespace)
21-
- [`newline-after-block`](#newline-after-block)
2222
- [`range`](#range)
2323
- [`return`](#return)
2424
- [`select`](#select)
@@ -36,6 +36,68 @@
3636
This document describes all the checks done by `wsl` with examples of what's not
3737
allowed and what's allowed.
3838

39+
### `after-block`
40+
41+
Block statements (`if`, `for`, `switch`, etc.) should be followed by a blank
42+
line to visually separate them from subsequent code.
43+
44+
> **NOTE** An exception is made for `defer` statements that follow an
45+
> `if err != nil` block when the defer references a variable assigned on the
46+
> line above the if statement. This is a common pattern for resource cleanup.
47+
48+
<table>
49+
<thead><tr><th>Bad</th><th>Good</th></tr></thead>
50+
<tbody>
51+
<tr><td valign="top">
52+
53+
```go
54+
if true {
55+
fmt.Println("hello")
56+
} // 1
57+
fmt.Println("world")
58+
59+
for i := 0; i < 3; i++ {
60+
fmt.Println(i)
61+
} // 2
62+
x := 1
63+
```
64+
65+
</td><td valign="top">
66+
67+
```go
68+
if true {
69+
fmt.Println("hello")
70+
}
71+
72+
fmt.Println("world")
73+
74+
for i := 0; i < 3; i++ {
75+
fmt.Println(i)
76+
}
77+
78+
x := 1
79+
80+
// Exception: defer after error check
81+
f, err := os.Open("file.txt")
82+
if err != nil {
83+
return err
84+
}
85+
defer f.Close()
86+
```
87+
88+
</td></tr>
89+
90+
<tr><td valign="top">
91+
92+
<sup>1</sup> Missing whitespace after block
93+
94+
<sup>2</sup> Missing whitespace after block
95+
96+
</td><td valign="top">
97+
98+
</td></tr>
99+
</tbody></table>
100+
39101
### `assign`
40102

41103
Assign (`foo := bar`) or re-assignments (`foo = bar`) should only be cuddled
@@ -1284,68 +1346,6 @@ if true {
12841346

12851347
</tbody></table>
12861348

1287-
### `newline-after-block`
1288-
1289-
Block statements (`if`, `for`, `switch`, etc.) should be followed by a blank
1290-
line to visually separate them from subsequent code.
1291-
1292-
> **NOTE** An exception is made for `defer` statements that follow an
1293-
> `if err != nil` block when the defer references a variable assigned on the
1294-
> line above the if statement. This is a common pattern for resource cleanup.
1295-
1296-
<table>
1297-
<thead><tr><th>Bad</th><th>Good</th></tr></thead>
1298-
<tbody>
1299-
<tr><td valign="top">
1300-
1301-
```go
1302-
if true {
1303-
fmt.Println("hello")
1304-
}
1305-
fmt.Println("world") // 1
1306-
1307-
for i := 0; i < 3; i++ {
1308-
fmt.Println(i)
1309-
}
1310-
x := 1 // 2
1311-
```
1312-
1313-
</td><td valign="top">
1314-
1315-
```go
1316-
if true {
1317-
fmt.Println("hello")
1318-
}
1319-
1320-
fmt.Println("world")
1321-
1322-
for i := 0; i < 3; i++ {
1323-
fmt.Println(i)
1324-
}
1325-
1326-
x := 1
1327-
1328-
// Exception: defer after error check
1329-
f, err := os.Open("file.txt")
1330-
if err != nil {
1331-
return err
1332-
}
1333-
defer f.Close()
1334-
```
1335-
1336-
</td></tr>
1337-
1338-
<tr><td valign="top">
1339-
1340-
<sup>1</sup> Missing whitespace after block
1341-
1342-
<sup>2</sup> Missing whitespace after block
1343-
1344-
</td><td valign="top">
1345-
1346-
</td></tr>
1347-
</tbody></table>
1348-
13491349
### `trailing-whitespace`
13501350

13511351
<table>

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ For more details and examples, see [CHECKS](CHECKS.md).
6464

6565
#### Specific `wsl` cases
6666

67+
-**after-block** - Require empty line after block statements
6768
-**append** - Only allow re-assigning with `append` if the value being
6869
appended exist on the line above
6970
-**assign-exclusive** - Only allow cuddling either new variables or
@@ -73,7 +74,6 @@ For more details and examples, see [CHECKS](CHECKS.md).
7374
-**err** - Error checking must follow immediately after the error variable
7475
is assigned
7576
-**leading-whitespace** - Disallow leading empty lines in blocks
76-
-**newline-after-block** - Require empty line after block statements
7777
-**trailing-whitespace** - Disallow trailing empty lines in blocks
7878

7979
### Configuration
@@ -166,13 +166,14 @@ linters:
166166
- leading-whitespace
167167
- trailing-whitespace
168168
disable:
169+
- after-block
169170
- assign-exclusive
170171
- assign-expr
171-
- newline-after-block
172172
```
173173
174174
## See also
175175
176+
- [`newline-after-block`][newline-after-block] - Require newline after blocks
176177
- [`nlreturn`][nlreturn] - Use empty lines before `return`
177178
- [`whitespace`][whitespace] - Don't use a blank newline at the start or end of
178179
a block.
@@ -181,5 +182,6 @@ linters:
181182
[analysis]: https://pkg.go.dev/golang.org/x/tools/go/analysis
182183
[gofumpt]: https://github.com/mvdan/gofumpt
183184
[golangci-lint]: https://golangci-lint.run
185+
[newline-after-block]: https://github.com/breml/newline-after-block
184186
[nlreturn]: https://github.com/ssgreg/nlreturn
185187
[whitespace]: https://github.com/ultraware/whitespace

analyzer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ func TestWithConfig(t *testing.T) {
116116
},
117117
},
118118
{
119-
subdir: "newline_after_block",
119+
subdir: "after_block",
120120
configFn: func(config *Configuration) {
121121
config.Checks = NoChecks()
122122
config.Checks.Add(CheckCaseTrailingNewline)
123-
config.Checks.Add(CheckNewlineAfterBlock)
123+
config.Checks.Add(CheckAfterBlock)
124124

125125
config.CaseMaxLines = 1
126126
},

config.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const (
3333
CheckSwitch
3434
CheckTypeSwitch
3535

36+
// CheckAfterBlock ensures there's a newline after each block.
37+
CheckAfterBlock
3638
// CheckAppend only allows assignments of `append` to be cuddled with other
3739
// assignments if it's a variable used in the append statement, e.g.
3840
//
@@ -70,7 +72,6 @@ const (
7072
CheckErr
7173
CheckLeadingWhitespace
7274
CheckTrailingWhitespace
73-
CheckNewlineAfterBlock
7475

7576
//nolint:godoclint // No need to document
7677
// CheckTypes only used for reporting.
@@ -97,13 +98,13 @@ func (c CheckType) String() string {
9798
"switch",
9899
"type-switch",
99100
//
101+
"after-block",
100102
"append",
101103
"assign-exclusive",
102104
"assign-expr",
103105
"err",
104106
"leading-whitespace",
105107
"trailing-whitespace",
106-
"newline-after-block",
107108
//
108109
"case-trailing-newline",
109110
}[c]
@@ -213,7 +214,7 @@ func AllChecks() CheckSet {
213214
c := DefaultChecks()
214215
c.Add(CheckAssignExclusive)
215216
c.Add(CheckAssignExpr)
216-
c.Add(CheckNewlineAfterBlock)
217+
c.Add(CheckAfterBlock)
217218

218219
return c
219220
}
@@ -265,6 +266,8 @@ func CheckFromString(s string) (CheckType, error) {
265266
case "type-switch":
266267
return CheckTypeSwitch, nil
267268

269+
case "after-block":
270+
return CheckAfterBlock, nil
268271
case "append":
269272
return CheckAppend, nil
270273
case "assign-exclusive":
@@ -277,8 +280,6 @@ func CheckFromString(s string) (CheckType, error) {
277280
return CheckLeadingWhitespace, nil
278281
case "trailing-whitespace":
279282
return CheckTrailingWhitespace, nil
280-
case "newline-after-block":
281-
return CheckNewlineAfterBlock, nil
282283
default:
283284
return CheckInvalid, fmt.Errorf("invalid check '%s'", s)
284285
}

testdata/src/with_config/newline_after_block/blockstatements.go renamed to testdata/src/with_config/after_block/blockstatements.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ func ifStatementWithoutNewline() {
66
x := 5
77
if x > 0 {
88
fmt.Println("positive")
9-
} // want `missing whitespace below this line \(newline-after-block\)`
9+
} // want `missing whitespace below this line \(after-block\)`
1010
fmt.Println("next statement")
1111
}
1212

@@ -43,7 +43,7 @@ func ifElseStatementWithoutNewline() {
4343
fmt.Println("positive")
4444
} else {
4545
fmt.Println("not positive")
46-
} // want `missing whitespace below this line \(newline-after-block\)`
46+
} // want `missing whitespace below this line \(after-block\)`
4747
fmt.Println("next statement")
4848
}
4949

@@ -77,14 +77,14 @@ func ifElseIfStatementWithoutNewline() {
7777
fmt.Println("negative")
7878
} else {
7979
fmt.Println("zero")
80-
} // want `missing whitespace below this line \(newline-after-block\)`
80+
} // want `missing whitespace below this line \(after-block\)`
8181
fmt.Println("next statement")
8282
}
8383

8484
func forLoopWithoutNewline() {
8585
for i := 0; i < 5; i++ {
8686
fmt.Println(i)
87-
} // want `missing whitespace below this line \(newline-after-block\)`
87+
} // want `missing whitespace below this line \(after-block\)`
8888
fmt.Println("after loop")
8989
}
9090

@@ -106,7 +106,7 @@ func rangeLoopWithoutNewline() {
106106
items := []int{1, 2, 3}
107107
for _, item := range items {
108108
fmt.Println(item)
109-
} // want `missing whitespace below this line \(newline-after-block\)`
109+
} // want `missing whitespace below this line \(after-block\)`
110110
fmt.Println("after loop")
111111
}
112112

@@ -128,7 +128,7 @@ func switchStatementWithoutNewline() {
128128
fmt.Println("two") // want `missing whitespace below this line \(case-trailing-newline\)`
129129
default:
130130
fmt.Println("other")
131-
} // want `missing whitespace below this line \(newline-after-block\)`
131+
} // want `missing whitespace below this line \(after-block\)`
132132
fmt.Println("after switch")
133133
}
134134

@@ -155,7 +155,7 @@ func selectStatementWithoutNewline() {
155155
fmt.Println(v) // want `missing whitespace below this line \(case-trailing-newline\)`
156156
default:
157157
fmt.Println("default")
158-
} // want `missing whitespace below this line \(newline-after-block\)`
158+
} // want `missing whitespace below this line \(after-block\)`
159159
fmt.Println("after select")
160160
}
161161

@@ -177,7 +177,7 @@ func multipleStatementsWithMixedViolations() {
177177

178178
if x > 0 {
179179
fmt.Println("positive")
180-
} // want `missing whitespace below this line \(newline-after-block\)`
180+
} // want `missing whitespace below this line \(after-block\)`
181181
for i := 0; i < x; i++ {
182182
fmt.Println(i)
183183
}
@@ -194,9 +194,9 @@ func nestedIfWithoutNewline() {
194194
if x > 0 {
195195
if y > 0 {
196196
fmt.Println("both positive")
197-
} // want `missing whitespace below this line \(newline-after-block\)`
197+
} // want `missing whitespace below this line \(after-block\)`
198198
fmt.Println("x positive")
199-
} // want `missing whitespace below this line \(newline-after-block\)`
199+
} // want `missing whitespace below this line \(after-block\)`
200200
fmt.Println("after outer if")
201201
}
202202

@@ -218,9 +218,9 @@ func nestedForWithoutNewline() {
218218
for i := 0; i < 3; i++ {
219219
for j := 0; j < 3; j++ {
220220
fmt.Println(i, j)
221-
} // want `missing whitespace below this line \(newline-after-block\)`
221+
} // want `missing whitespace below this line \(after-block\)`
222222
fmt.Println("inner loop done")
223-
} // want `missing whitespace below this line \(newline-after-block\)`
223+
} // want `missing whitespace below this line \(after-block\)`
224224
fmt.Println("after outer loop")
225225
}
226226

@@ -244,11 +244,11 @@ func nestedSwitchWithoutNewline() {
244244
switch y {
245245
case 2:
246246
fmt.Println("x=1, y=2")
247-
} // want `missing whitespace below this line \(newline-after-block\)`
247+
} // want `missing whitespace below this line \(after-block\)`
248248
fmt.Println("x=1") // want `missing whitespace below this line \(case-trailing-newline\)`
249249
case 2:
250250
fmt.Println("x=2")
251-
} // want `missing whitespace below this line \(newline-after-block\)`
251+
} // want `missing whitespace below this line \(after-block\)`
252252
fmt.Println("after outer switch")
253253
}
254254

@@ -279,7 +279,7 @@ func complexNested() {
279279
fmt.Println("zero") // want `missing whitespace below this line \(case-trailing-newline\)`
280280
case 2:
281281
fmt.Println("two")
282-
} // want `missing whitespace below this line \(newline-after-block\)`
282+
} // want `missing whitespace below this line \(after-block\)`
283283
fmt.Println("even")
284284
}
285285

@@ -298,7 +298,7 @@ func typeSwitchWithoutNewline() {
298298
fmt.Println("int:", v) // want `missing whitespace below this line \(case-trailing-newline\)`
299299
default:
300300
fmt.Println("unknown type")
301-
} // want `missing whitespace below this line \(newline-after-block\)`
301+
} // want `missing whitespace below this line \(after-block\)`
302302
fmt.Println("after type switch")
303303
}
304304

0 commit comments

Comments
 (0)