Skip to content

Commit

Permalink
#104 allow a blank line after each comment group at the start of a block
Browse files Browse the repository at this point in the history
  • Loading branch information
mholiday-nyt authored and bombsimon committed Apr 20, 2021
1 parent 15cab0f commit 8084c59
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 4 deletions.
30 changes: 30 additions & 0 deletions doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,36 @@ assignmentTwo := "so I cannot be cuddled"
assignmentThree := "this is fine"
```

### [allow-separated-leading-comment](rules.md#block-should-not-start-with-a-whitespace)

This option allows whitespace after each comment group that begins a block.

> Default value: false
For example,

```go
func example() string {
// comment

return fmt.Sprintf("x")
}
```

and

```go
func example() string {
// comment

// comment
return fmt.Sprintf("x")
}
```

become legal, as the whitespace _after_ (or between) each comment block
doesn't count against whitespace before the first actual statement.

### [force-case-trailing-whitespace](rules.md#case-block-should-end-with-newline-at-this-size)

Can be set to force trailing newlines at the end of case blocks to improve
Expand Down
25 changes: 25 additions & 0 deletions doc/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ func example() string {
}
```

> However, this can be configured to allow white space after one
> or more initial comment groups, see
[configuration documentation](configuration.md#allow-separated-leading-comment)
>
> If that is done, then these examples are allowed:
```go
func example() string {
// comment

return fmt.Sprintf("x")
}
```

> and
```go
func example() string {
// comment

// comment
return fmt.Sprintf("x")
}
```

---

### Branch statements should not be cuddled if block has more than two lines
Expand Down
22 changes: 18 additions & 4 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,8 +1070,17 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
}

// We store number of seen comment groups because we allow multiple
// groups with a newline between them.
seenCommentGroups++
// groups with a newline between them; but if the first one has WS
// before it, we're not going to count it to force an error.
if p.config.AllowSeparatedLeadingComment {
cg := p.fileSet.Position(commentGroup.Pos()).Line

if seenCommentGroups > 0 || cg == blockStartLine+1 {
seenCommentGroups++
}
} else {
seenCommentGroups++
}

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

// If we have multiple groups, add support for newline between each group.
// If we allow separated comments, allow for a space after each group
if p.config.AllowSeparatedLeadingComment {
if seenCommentGroups > 1 {
allowedLinesBeforeFirstStatement += seenCommentGroups - 1
} else if seenCommentGroups == 1 {
allowedLinesBeforeFirstStatement += 1
}
}

if p.nodeStart(firstStatement) != blockStartLine+allowedLinesBeforeFirstStatement {
// And now if the first statement is passed the number of allowed lines,
// then we had extra WS, possibly before the first comment group.
if p.nodeStart(firstStatement) > blockStartLine+allowedLinesBeforeFirstStatement {
fmt.Printf("error: p.nodeStart(firstStatement)=%d, blockStartLine+allowedLinesBeforeFirstStatement=%d\n", p.nodeStart(firstStatement), blockStartLine+allowedLinesBeforeFirstStatement)
p.addError(
blockStartPos,
reasonBlockStartsWithWS,
Expand Down
64 changes: 64 additions & 0 deletions wsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1732,8 +1732,72 @@ func TestWithConfig(t *testing.T) {
*/
fmt.Println("Hello, World")
}
func () { // Comment
// and one more
fmt.Println("Hello, World")
}
func () { // Comment
fmt.Println("Hello, World")
}
}`),
},
{
description: "allow separated leading comment (negative)",
customConfig: &Configuration{
AllowSeparatedLeadingComment: true,
},
code: []byte(`package main
func main() {
// These blocks should generate error
func () {
// Comment
// Comment
fmt.Println("Hello, World")
}
func () {
fmt.Println("Hello, World")
}
func () {
// Comment
fmt.Println("Hello, World")
}
func () {
// Comment
// Comment
fmt.Println("Hello, World")
}
func () { // Comment
/*
Multiline
*/
fmt.Println("Hello, World")
}
}`),
expectedErrorStrings: []string{
reasonBlockStartsWithWS,
reasonBlockStartsWithWS,
reasonBlockStartsWithWS,
reasonBlockStartsWithWS,
reasonBlockStartsWithWS,
},
},
{
description: "only warn about cuddling errors if it's an expression above",
customConfig: &Configuration{
Expand Down

0 comments on commit 8084c59

Please sign in to comment.