1
0
mirror of https://github.com/mgechev/revive.git synced 2025-04-13 11:30:36 +02:00

fixes issue #229 (#231)

This commit is contained in:
SalvadorC 2019-09-10 19:26:47 +02:00 committed by Minko Gechev
parent fa5acbc1f0
commit 84deee4163
2 changed files with 32 additions and 3 deletions

View File

@ -13,3 +13,23 @@ type (
) )
type Foo struct{} // MATCH /exported type Foo should have comment or be unexported/ type Foo struct{} // MATCH /exported type Foo should have comment or be unexported/
// The following cases are no-regression tests for issue 229
/* Bar something */
type Bar struct{}
/* Toto something */
func Toto() {}
/* FirstLetter something */
const FirstLetter = "A"
/*Bar2 something */
type Bar2 struct{}
/*Toto2 something */
func Toto2() {}
/*SecondLetter something */
const SecondLetter = "B"

View File

@ -85,7 +85,7 @@ func (w *lintExported) lintFuncDoc(fn *ast.FuncDecl) {
}) })
return return
} }
s := fn.Doc.Text() s := normalizeText(fn.Doc.Text())
prefix := fn.Name.Name + " " prefix := fn.Name.Name + " "
if !strings.HasPrefix(s, prefix) { if !strings.HasPrefix(s, prefix) {
w.onFailure(lint.Failure{ w.onFailure(lint.Failure{
@ -141,7 +141,7 @@ func (w *lintExported) lintTypeDoc(t *ast.TypeSpec, doc *ast.CommentGroup) {
return return
} }
s := doc.Text() s := normalizeText(doc.Text())
articles := [...]string{"A", "An", "The", "This"} articles := [...]string{"A", "An", "The", "This"}
for _, a := range articles { for _, a := range articles {
if t.Name.Name == a { if t.Name.Name == a {
@ -217,7 +217,8 @@ func (w *lintExported) lintValueSpecDoc(vs *ast.ValueSpec, gd *ast.GenDecl, genD
doc = gd.Doc doc = gd.Doc
} }
prefix := name + " " prefix := name + " "
if !strings.HasPrefix(doc.Text(), prefix) { s := normalizeText(doc.Text())
if !strings.HasPrefix(s, prefix) {
w.onFailure(lint.Failure{ w.onFailure(lint.Failure{
Confidence: 1, Confidence: 1,
Node: doc, Node: doc,
@ -227,6 +228,14 @@ func (w *lintExported) lintValueSpecDoc(vs *ast.ValueSpec, gd *ast.GenDecl, genD
} }
} }
// normalizeText is a helper function that normalizes comment strings by:
// * removing one leading space
//
// This function is needed because ast.CommentGroup.Text() does not handle //-style and /*-style comments uniformly
func normalizeText(t string) string {
return strings.TrimPrefix(t, " ")
}
func (w *lintExported) Visit(n ast.Node) ast.Visitor { func (w *lintExported) Visit(n ast.Node) ast.Visitor {
switch v := n.(type) { switch v := n.(type) {
case *ast.GenDecl: case *ast.GenDecl: