Skip to content

Commit 8084c59

Browse files
mholiday-nytbombsimon
authored andcommitted
#104 allow a blank line after each comment group at the start of a block
1 parent 15cab0f commit 8084c59

File tree

4 files changed

+137
-4
lines changed

4 files changed

+137
-4
lines changed

doc/configuration.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,36 @@ assignmentTwo := "so I cannot be cuddled"
133133
assignmentThree := "this is fine"
134134
```
135135

136+
### [allow-separated-leading-comment](rules.md#block-should-not-start-with-a-whitespace)
137+
138+
This option allows whitespace after each comment group that begins a block.
139+
140+
> Default value: false
141+
142+
For example,
143+
144+
```go
145+
func example() string {
146+
// comment
147+
148+
return fmt.Sprintf("x")
149+
}
150+
```
151+
152+
and
153+
154+
```go
155+
func example() string {
156+
// comment
157+
158+
// comment
159+
return fmt.Sprintf("x")
160+
}
161+
```
162+
163+
become legal, as the whitespace _after_ (or between) each comment block
164+
doesn't count against whitespace before the first actual statement.
165+
136166
### [force-case-trailing-whitespace](rules.md#case-block-should-end-with-newline-at-this-size)
137167

138168
Can be set to force trailing newlines at the end of case blocks to improve

doc/rules.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,31 @@ func example() string {
202202
}
203203
```
204204

205+
> However, this can be configured to allow white space after one
206+
> or more initial comment groups, see
207+
[configuration documentation](configuration.md#allow-separated-leading-comment)
208+
>
209+
> If that is done, then these examples are allowed:
210+
211+
```go
212+
func example() string {
213+
// comment
214+
215+
return fmt.Sprintf("x")
216+
}
217+
```
218+
219+
> and
220+
221+
```go
222+
func example() string {
223+
// comment
224+
225+
// comment
226+
return fmt.Sprintf("x")
227+
}
228+
```
229+
205230
---
206231

207232
### Branch statements should not be cuddled if block has more than two lines

wsl.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,8 +1070,17 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
10701070
}
10711071

10721072
// We store number of seen comment groups because we allow multiple
1073-
// groups with a newline between them.
1074-
seenCommentGroups++
1073+
// groups with a newline between them; but if the first one has WS
1074+
// before it, we're not going to count it to force an error.
1075+
if p.config.AllowSeparatedLeadingComment {
1076+
cg := p.fileSet.Position(commentGroup.Pos()).Line
1077+
1078+
if seenCommentGroups > 0 || cg == blockStartLine+1 {
1079+
seenCommentGroups++
1080+
}
1081+
} else {
1082+
seenCommentGroups++
1083+
}
10751084

10761085
// Support both /* multiline */ and //single line comments
10771086
for _, c := range commentGroup.List {
@@ -1080,14 +1089,19 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
10801089
}
10811090
}
10821091

1083-
// If we have multiple groups, add support for newline between each group.
1092+
// If we allow separated comments, allow for a space after each group
10841093
if p.config.AllowSeparatedLeadingComment {
10851094
if seenCommentGroups > 1 {
10861095
allowedLinesBeforeFirstStatement += seenCommentGroups - 1
1096+
} else if seenCommentGroups == 1 {
1097+
allowedLinesBeforeFirstStatement += 1
10871098
}
10881099
}
10891100

1090-
if p.nodeStart(firstStatement) != blockStartLine+allowedLinesBeforeFirstStatement {
1101+
// And now if the first statement is passed the number of allowed lines,
1102+
// then we had extra WS, possibly before the first comment group.
1103+
if p.nodeStart(firstStatement) > blockStartLine+allowedLinesBeforeFirstStatement {
1104+
fmt.Printf("error: p.nodeStart(firstStatement)=%d, blockStartLine+allowedLinesBeforeFirstStatement=%d\n", p.nodeStart(firstStatement), blockStartLine+allowedLinesBeforeFirstStatement)
10911105
p.addError(
10921106
blockStartPos,
10931107
reasonBlockStartsWithWS,

wsl_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,8 +1732,72 @@ func TestWithConfig(t *testing.T) {
17321732
*/
17331733
fmt.Println("Hello, World")
17341734
}
1735+
1736+
func () { // Comment
1737+
// and one more
1738+
1739+
fmt.Println("Hello, World")
1740+
}
1741+
1742+
func () { // Comment
1743+
fmt.Println("Hello, World")
1744+
}
17351745
}`),
17361746
},
1747+
{
1748+
description: "allow separated leading comment (negative)",
1749+
customConfig: &Configuration{
1750+
AllowSeparatedLeadingComment: true,
1751+
},
1752+
code: []byte(`package main
1753+
1754+
func main() {
1755+
// These blocks should generate error
1756+
func () {
1757+
1758+
// Comment
1759+
1760+
// Comment
1761+
fmt.Println("Hello, World")
1762+
}
1763+
1764+
func () {
1765+
1766+
fmt.Println("Hello, World")
1767+
}
1768+
1769+
func () {
1770+
// Comment
1771+
1772+
1773+
fmt.Println("Hello, World")
1774+
}
1775+
1776+
func () {
1777+
// Comment
1778+
1779+
// Comment
1780+
1781+
1782+
fmt.Println("Hello, World")
1783+
}
1784+
1785+
func () { // Comment
1786+
1787+
/*
1788+
Multiline
1789+
*/
1790+
fmt.Println("Hello, World")
1791+
}
1792+
}`),
1793+
expectedErrorStrings: []string{
1794+
reasonBlockStartsWithWS,
1795+
reasonBlockStartsWithWS,
1796+
reasonBlockStartsWithWS,
1797+
reasonBlockStartsWithWS,
1798+
reasonBlockStartsWithWS,
1799+
},
1800+
},
17371801
{
17381802
description: "only warn about cuddling errors if it's an expression above",
17391803
customConfig: &Configuration{

0 commit comments

Comments
 (0)