Skip to content

Commit

Permalink
Add flag to support leading comments separated by newline. (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Mar 4, 2020
1 parent edb0634 commit 615dc3c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
26 changes: 24 additions & 2 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -962,13 +973,24 @@ 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"))
}
}
}

// 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,
Expand Down
53 changes: 53 additions & 0 deletions wsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 615dc3c

Please sign in to comment.