1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-06 03:04:06 +02:00

refactor: Reduce package-comments output scope (#791)

This reduces the amount of lines reported by package-comments by only
reporting the `package XXXXXXX' line, instead of the whole file.
This commit is contained in:
Vito (Victor Gama) 2023-02-17 17:39:51 -03:00 committed by GitHub
parent 8c2cd33315
commit b2bc00bd75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
@ -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",
}}