Repository: bombsimon/wsl
Pull request #74 (2020-03-02 13:40:04)

Add flag to support leading comments separated by newline. (#73)

coveralls: ## Pull Request Test Coverage Report for [Build 181](https://coveralls.io/builds/29073245)

* **9** of **9**   **(100.0%)**  changed or added relevant lines in **1** file are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage increased (+**0.1%**) to **89.561%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/29073245/badge)](https://coveralls.io/builds/29073245) |
| :-- | --: |
| Change from base [Build 177](https://coveralls.io/builds/28917980): |  0.1% |
| Covered Lines: | 592 |
| Relevant Lines: | 661 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/wsl.go b/wsl.go
index 51f8870..c4cc592 100644
--- a/wsl.go
+++ b/wsl.go
@@ -100,6 +100,16 @@ type Configuration struct {
 	// AllowTrailingComment will allow blocks to end with comments.
 	AllowTrailingComment bool
 
+	// AllowSeparatedLeadingComment will allow multiple comments in the
+	// beginning of a block separated with newline. Example:
+	//  func () {
+	//		// Comment one
+	//
+	//		// Comment two
+	// 		fmt.Println("x")
+	//  }
+	AllowSeparatedLeadingComment 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:
@@ -941,8 +951,9 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
 	}
 
 	var (
-		firstStatement = blockStatements[0]
-		lastStatement  = blockStatements[len(blockStatements)-1]
+		firstStatement    = blockStatements[0]
+		lastStatement     = blockStatements[len(blockStatements)-1]
+		seenCommentGroups = 0
 	)
 
 	// Get the comment related to the first statement, we do allow commends in
@@ -962,6 +973,10 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
 				break
 			}
 
+			// We store number of seen comment groups because we allow multiple
+			// groups with a newline between them.
+			seenCommentGroups++
+
 			// Support both /* multiline */ and //single line comments
 			for _, c := range commentGroup.List {
 				allowedLinesBeforeFirstStatement += len(strings.Split(c.Text, "\n"))
@@ -969,6 +984,13 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
 		}
 	}
 
+	// If we have multiple groups, add support for newline between each group.
+	if p.config.AllowSeparatedLeadingComment {
+		if seenCommentGroups > 1 {
+			allowedLinesBeforeFirstStatement += seenCommentGroups - 1
+		}
+	}
+
 	if p.nodeStart(firstStatement) != blockStartLine+allowedLinesBeforeFirstStatement {
 		p.addError(
 			blockStartPos,


Test patch:
diff --git a/wsl_test.go b/wsl_test.go
index 8596acd..70ec3b4 100644
--- a/wsl_test.go
+++ b/wsl_test.go
@@ -1621,6 +1621,59 @@ func TestWithConfig(t *testing.T) {
 			}`),
 			expectedErrorStrings: []string{},
 		},
+		{
+			description: "allow separated leading comment",
+			customConfig: &Configuration{
+				AllowSeparatedLeadingComment: true,
+			},
+			code: []byte(`package main
+
+			func main() {
+				// These blocks should not generate error
+				func () {
+					// Comment
+
+					// Comment
+					fmt.Println("Hello, World")
+				}
+
+				func () {
+					/*
+						Multiline
+					*/
+
+					/*
+						Multiline
+					*/
+					fmt.Println("Hello, World")
+				}
+
+				func () {
+					/*
+						Multiline
+					*/
+
+					// Comment
+					fmt.Println("Hello, World")
+				}
+
+				func () {
+					// Comment
+
+					/*
+						Multiline
+					*/
+					fmt.Println("Hello, World")
+				}
+
+				func () { // Comment
+					/*
+						Multiline
+					*/
+					fmt.Println("Hello, World")
+				}
+			}`),
+		},
 	}
 
 	for _, tc := range cases {