Skip to content

Commit

Permalink
fix issue mgechev#691 (mgechev#700)
Browse files Browse the repository at this point in the history
Signed-off-by: subham sarkar <subham@deepsource.io>
  • Loading branch information
chavacava authored and subham-deepsource committed Aug 5, 2022
1 parent 7bf12c5 commit c575ea1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
12 changes: 9 additions & 3 deletions rule/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (w *lintExported) lintValueSpecDoc(vs *ast.ValueSpec, gd *ast.GenDecl, genD
return
}

if vs.Doc == nil && gd.Doc == nil {
if vs.Doc == nil && vs.Comment == nil && gd.Doc == nil {
if genDeclMissingComments[gd] {
return
}
Expand All @@ -274,10 +274,16 @@ func (w *lintExported) lintValueSpecDoc(vs *ast.ValueSpec, gd *ast.GenDecl, genD
}
// The relevant text to check will be on either vs.Doc or gd.Doc.
// Use vs.Doc preferentially.
doc := vs.Doc
if doc == nil {
var doc *ast.CommentGroup
switch {
case vs.Doc != nil:
doc = vs.Doc
case vs.Comment != nil && gd.Doc == nil:
doc = vs.Comment
default:
doc = gd.Doc
}

prefix := name + " "
s := normalizeText(doc.Text())
if !strings.HasPrefix(s, prefix) {
Expand Down
12 changes: 9 additions & 3 deletions testdata/golint/const-block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
package foo

const (
InlineComment = "ShouldBeOK" // InlineComment is a valid comment

// Prefix for something.
// MATCH /comment on exported const InlineWhatever should be of the form "InlineWhatever ..."/
InlineWhatever = "blah"

Whatsit = "missing_comment" // MATCH /exported const Whatsit should have comment (or a comment on this block) or be unexported/
Whatsit = "missing_comment"
// MATCH:13 /exported const Whatsit should have comment (or a comment on this block) or be unexported/

// We should only warn once per block for missing comments,
// but always complain about malformed comments.
Expand All @@ -29,8 +32,11 @@ const (

// The comment on the previous const block shouldn't flow through to here.

const UndocAgain = 6 // MATCH /exported const UndocAgain should have comment or be unexported/
const UndocAgain = 6

// MATCH:35 /exported const UndocAgain should have comment or be unexported/

const (
SomeUndocumented = 7 // MATCH /exported const SomeUndocumented should have comment (or a comment on this block) or be unexported/
SomeUndocumented = 7
// MATCH:40 /exported const SomeUndocumented should have comment (or a comment on this block) or be unexported/
)
4 changes: 3 additions & 1 deletion testdata/golint/error-naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
var unexp = errors.New("some unexported error") // MATCH /error var unexp should have name of the form errFoo/

// Exp ...
var Exp = errors.New("some exported error") // MATCH /error var Exp should have name of the form ErrFoo/
var Exp = errors.New("some exported error")

// MATCH:14 /error var Exp should have name of the form ErrFoo/

var (
e1 = fmt.Errorf("blah %d", 4) // MATCH /error var e1 should have name of the form errFoo/
Expand Down
15 changes: 11 additions & 4 deletions testdata/golint/value-spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ type V string

func (*V) H() {} // MATCH /exported method V.H should have comment or be unexported/

var W = "foo" // MATCH /exported var W should have comment or be unexported/
var W = "foo"

const X = "bar" // MATCH /exported const X should have comment or be unexported/
// MATCH:27 /exported var W should have comment or be unexported/

var Y, Z int // MATCH /exported var Z should have its own declaration/
const X = "bar"

// MATCH:31 /exported const X should have comment or be unexported/

var Y, Z int

// MATCH:35 /exported var Z should have its own declaration/

// Location should be okay, since the other var name is an underscore.
var Location, _ = time.LoadLocation("Europe/Istanbul") // not Constantinople

// this is improperly documented
// MATCH /comment on exported const Thing should be of the form "Thing ..."/
const Thing = "wonderful"

// MATCH:42 /comment on exported const Thing should be of the form "Thing ..."/

0 comments on commit c575ea1

Please sign in to comment.