From 8084c599e37d0933e7248e58e7f223c43d12f3b8 Mon Sep 17 00:00:00 2001 From: Matt Holiday Date: Tue, 20 Apr 2021 10:10:33 -0600 Subject: [PATCH] #104 allow a blank line after each comment group at the start of a block --- doc/configuration.md | 30 +++++++++++++++++++++ doc/rules.md | 25 +++++++++++++++++ wsl.go | 22 ++++++++++++--- wsl_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+), 4 deletions(-) diff --git a/doc/configuration.md b/doc/configuration.md index f2ec62e..02d02a3 100644 --- a/doc/configuration.md +++ b/doc/configuration.md @@ -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 diff --git a/doc/rules.md b/doc/rules.md index d80afff..865bcf3 100644 --- a/doc/rules.md +++ b/doc/rules.md @@ -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 diff --git a/wsl.go b/wsl.go index 9389f4e..d5ec72a 100644 --- a/wsl.go +++ b/wsl.go @@ -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 { @@ -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, diff --git a/wsl_test.go b/wsl_test.go index 41d62d5..3dd30e9 100644 --- a/wsl_test.go +++ b/wsl_test.go @@ -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{