Repository: bombsimon/wsl (go)

Issue:
Allow trailing whitespace in blocks
I see some options like --no-test -allow-declaration, but I'm uncertain what they do.
I was looking for a way to disable some of the warnings about the:
block should not start/end with a whitespace
but I'm not sure if there is any such option

Pull request description:
Add configuration and support to end blocks with comments
Resolves #43 

Flag is `--allow-traling-comment` and will allow comments in all forms as long as there's no empty line between the lats statement and the left brace.

**Ok**

```go
if true {
    fmt.Println("x")
    // Comment
}

if true {
    fmt.Println("x")
    // Comment
    //
    // Comment
}

if true {
    fmt.Println("x")
    /* Comment */
}

if true {
    fmt.Println("x")
    /*
        Multiline
    */
}
```

**Not ok**

```go
if true {
    fmt.Println("x")

    // Comment
}

if true {
    fmt.Println("x")
    // Comment

}
```

Patch:
diff --git a/cmd/wsl/main.go b/cmd/wsl/main.go
index 0630ca9..1463704 100644
--- a/cmd/wsl/main.go
+++ b/cmd/wsl/main.go
@@ -34,7 +34,8 @@ func main() {
 	flag.BoolVar(&config.AllowAssignAndCallCuddle, "allow-assign-and-call", true, "Allow assignments and calls to be cuddled (if using same variable/type)")
 	flag.BoolVar(&config.AllowMultiLineAssignCuddle, "allow-multi-line-assign", true, "Allow cuddling with multi line assignments")
 	flag.BoolVar(&config.AllowCaseTrailingWhitespace, "allow-case-trailing-whitespace", false, "Allow case statements to end with an empty line")
-	flag.BoolVar(&config.AllowCuddleDeclaration, "allow-declarations", false, "Allow declarations to be cuddled")
+	flag.BoolVar(&config.AllowCuddleDeclaration, "allow-cuddle-declarations", false, "Allow declarations to be cuddled")
+	flag.BoolVar(&config.AllowTrailingComment, "allow-trailing-comment", false, "Allow blocks to end with a comment")
 
 	flag.Parse()
 
diff --git a/wsl.go b/wsl.go
index fe62b62..8bb9647 100644
--- a/wsl.go
+++ b/wsl.go
@@ -66,6 +66,9 @@ type Configuration struct {
 	// }
 	AllowCaseTrailingWhitespace bool
 
+	// AllowTrailingComment will allow blocks to end with comments.
+	AllowTrailingComment bool
+
 	// AllowCuddleDeclaration will allow multiple var/declaration statements to
 	// be cuddled. This defaults to false but setting it to true will enable the
 	// following example:
@@ -97,6 +100,7 @@ func DefaultConfig() Configuration {
 		AllowAssignAndCallCuddle:    true,
 		AllowMultiLineAssignCuddle:  true,
 		AllowCaseTrailingWhitespace: false,
+		AllowTrailingComment:        false,
 		AllowCuddleWithCalls:        []string{"Lock", "RLock"},
 		AllowCuddleWithRHS:          []string{"Unlock", "RUnlock"},
 	}
@@ -859,8 +863,10 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
 			return
 		}
 
-		// If we allow case to end white whitespace just return.
-		if p.config.AllowCaseTrailingWhitespace {
+		// If we allow case to end white whitespace or comment just return.
+		// TODO: Includes a false-positive where a newline before/after a
+		// comment in the case is valid but that's OK for now.
+		if p.config.AllowCaseTrailingWhitespace || p.config.AllowTrailingComment {
 			return
 		}
 
@@ -877,6 +883,28 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
 		blockEndLine = p.fileSet.Position(blockEndPos).Line
 	}
 
+	if p.config.AllowTrailingComment {
+		if lastComment, ok := commentMap[lastStatement]; ok {
+			var (
+				lastCommentGroup = lastComment[len(lastComment)-1]
+				lastCommentLine  = lastCommentGroup.List[len(lastCommentGroup.List)-1]
+				countNewlines    = 0
+			)
+
+			if strings.Contains(lastCommentLine.Text, "\n") {
+				for _, x := range lastCommentLine.Text {
+					if x == '\n' {
+						countNewlines++
+					}
+				}
+			}
+
+			if p.nodeStart(lastCommentLine)+countNewlines == blockEndLine-1 {
+				return
+			}
+		}
+	}
+
 	if p.nodeEnd(lastStatement) != blockEndLine-1 && !isExampleFunc(ident) {
 		p.addError(
 			blockEndPos,


Test patch:
diff --git a/wsl_test.go b/wsl_test.go
index 9dbbb2c..5507d74 100644
--- a/wsl_test.go
+++ b/wsl_test.go
@@ -1232,6 +1232,65 @@ func TestWithConfig(t *testing.T) {
 				AllowCuddleDeclaration: true,
 			},
 		},
+		{
+			description: "support to end blocks with a comment",
+			code: []byte(`package main
+
+			func main() {
+				for i := range make([]int, 10) {
+					fmt.Println("x")
+					// This is OK
+				}
+
+				for i := range make([]int, 10) {
+					fmt.Println("x")
+					// Pad
+					// With empty lines
+					//
+					// This is OK
+				}
+
+				for i := range make([]int, 10) {
+					fmt.Println("x")
+
+					// This is NOT OK
+				}
+
+				for i := range make([]int, 10) {
+					fmt.Println("x")
+					// This is NOT OK
+
+				}
+
+				for i := range make([]int, 10) {
+					fmt.Println("x")
+					/* Block comment one line OK */
+				}
+
+				for i := range make([]int, 10) {
+					fmt.Println("x")
+					/*
+						Block comment one multiple lines OK
+					*/
+				}
+
+				switch {
+				case true:
+					fmt.Println("x")
+					// This is OK
+				case false:
+					fmt.Println("y")
+					// This is OK
+				}
+			}`),
+			customConfig: &Configuration{
+				AllowTrailingComment: true,
+			},
+			expectedErrorStrings: []string{
+				"block should not end with a whitespace (or comment)",
+				"block should not end with a whitespace (or comment)",
+			},
+		},
 	}
 
 	for _, tc := range cases {