Skip to content

Commit

Permalink
refactor: Reduce package-comments output scope (#791)
Browse files Browse the repository at this point in the history
This reduces the amount of lines reported by package-comments by only
reporting the `package XXXXXXX' line, instead of the whole file.
  • Loading branch information
heyvito authored Feb 17, 2023
1 parent 8c2cd33 commit b2bc00b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions rule/package-comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ func (l *lintPackageComments) checkPackageComment() []lint.Failure {
var packageFile *ast.File // which name is $package.go
var firstFile *ast.File
var firstFileName string
var fileSource string
for name, file := range l.file.Pkg.Files() {
if file.AST.Doc != nil {
return nil
}
if name == "doc.go" {
docFile = file.AST
fileSource = "doc.go"
}
if name == file.AST.Name.String()+".go" {
packageFile = file.AST
Expand All @@ -76,14 +78,21 @@ func (l *lintPackageComments) checkPackageComment() []lint.Failure {
// prefer warning on doc.go, $package.go over first file
if docFile == nil {
docFile = packageFile
fileSource = l.fileAst.Name.String() + ".go"
}
if docFile == nil {
docFile = firstFile
fileSource = firstFileName
}

if docFile != nil {
pkgFile := l.file.Pkg.Files()[fileSource]
return []lint.Failure{{
Category: "comments",
Node: docFile,
Category: "comments",
Position: lint.FailurePosition{
Start: pkgFile.ToPosition(docFile.Pos()),
End: pkgFile.ToPosition(docFile.Name.End()),
},
Confidence: 1,
Failure: "should have a package comment",
}}
Expand Down

0 comments on commit b2bc00b

Please sign in to comment.