-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added comment-spacing rule for revive * added test for comment-spacings rule * adds comment-spacings rule to the configuration * renames the source file to match rule name * adds some tests * refactor Comment-Spacings Rule to remove whiteList and avoid Panic at empty comment * refactoring and adds rule configuration * adds rule documentation Co-authored-by: chavacava <salvadorcavadini+github@gmail.com>
- Loading branch information
Showing
8 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package rule | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/mgechev/revive/lint" | ||
) | ||
|
||
// CommentSpacings Rule check the whether there is a space between | ||
// the comment symbol( // ) and the start of the comment text | ||
type CommentSpacingsRule struct { | ||
allowList []string | ||
sync.Mutex | ||
} | ||
|
||
func (r *CommentSpacingsRule) configure(arguments lint.Arguments) { | ||
r.Lock() | ||
defer r.Unlock() | ||
|
||
if r.allowList == nil { | ||
r.allowList = []string{ | ||
"//go:", | ||
"//revive:", | ||
} | ||
|
||
for _, arg := range arguments { | ||
allow, ok := arg.(string) // Alt. non panicking version | ||
if !ok { | ||
panic(fmt.Sprintf("invalid argument %v for %s; expected string but got %T", arg, r.Name(), arg)) | ||
} | ||
r.allowList = append(r.allowList, `//`+allow+`:`) | ||
} | ||
} | ||
} | ||
|
||
func (r *CommentSpacingsRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { | ||
r.configure(args) | ||
|
||
var failures []lint.Failure | ||
|
||
for _, cg := range file.AST.Comments { | ||
for _, comment := range cg.List { | ||
commentLine := comment.Text | ||
if len(commentLine) < 3 { | ||
continue // nothing to do | ||
} | ||
|
||
isOK := commentLine[2] == ' ' | ||
if isOK { | ||
continue | ||
} | ||
|
||
if r.isAllowed(commentLine) { | ||
continue | ||
} | ||
|
||
failures = append(failures, lint.Failure{ | ||
Node: comment, | ||
Confidence: 1, | ||
Category: "style", | ||
Failure: "no space between comment delimiter and comment text", | ||
}) | ||
} | ||
} | ||
return failures | ||
} | ||
|
||
func (*CommentSpacingsRule) Name() string { | ||
return "comment-spacings" | ||
} | ||
|
||
func (r *CommentSpacingsRule) isAllowed(line string) bool { | ||
for _, allow := range r.allowList { | ||
if strings.HasPrefix(line, allow) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mgechev/revive/lint" | ||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
func TestCommentSpacings(t *testing.T) { | ||
|
||
testRule(t, "comment-spacings", &rule.CommentSpacingsRule{}, &lint.RuleConfig{ | ||
Arguments: []interface{}{"myOwnDirective"}}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package commentspacings | ||
|
||
//revive:disable:cyclomatic | ||
type some struct{} | ||
|
||
//revive:disable:cyclomatic High complexity score but easy to understand | ||
type some1 struct{} | ||
|
||
// Some nice comment | ||
// | ||
// With an empty line in the middle will make the rule panic! | ||
func itsATrap() {} | ||
|
||
// This is a well formed comment | ||
type hello struct { | ||
random `json:random` | ||
} | ||
|
||
// MATCH:21 /no space between comment delimiter and comment text/ | ||
|
||
//This comment does not respect the spacing rule! | ||
var a string | ||
|
||
//myOwnDirective: do something |