From f3dc87f090d022715731f78f628257a23e6200bf Mon Sep 17 00:00:00 2001 From: zhangyunhao Date: Sun, 8 Nov 2020 11:38:16 +0800 Subject: [PATCH] fix-invalid-doc --- stylecheck/lint.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/stylecheck/lint.go b/stylecheck/lint.go index 8ed7ad5a..653426be 100644 --- a/stylecheck/lint.go +++ b/stylecheck/lint.go @@ -42,7 +42,15 @@ func CheckPackageComment(pass *analysis.Pass) (interface{}, error) { if pass.Pkg.Name() == "main" { return nil, nil } + + type invalidDoc struct { + doc *ast.CommentGroup + msg string + } + var invalidDocs []invalidDoc hasDocs := false + hasValidDoc := false + for _, f := range pass.Files { if code.IsInTest(pass, f) { continue @@ -51,11 +59,19 @@ func CheckPackageComment(pass *analysis.Pass) (interface{}, error) { hasDocs = true prefix := "Package " + f.Name.Name + " " if !strings.HasPrefix(strings.TrimSpace(f.Doc.Text()), prefix) { - report.Report(pass, f.Doc, fmt.Sprintf(`package comment should be of the form "%s..."`, prefix)) + invalidDocs = append(invalidDocs, invalidDoc{doc: f.Doc, msg: fmt.Sprintf(`package comment should be of the form "%s..."`, prefix)}) + } else { + hasValidDoc = true } } } + if !hasValidDoc { + for _, i := range invalidDocs { + report.Report(pass, i.doc, i.msg) + } + } + if !hasDocs { for _, f := range pass.Files { if code.IsInTest(pass, f) {