github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/cmd/tast-lint/internal/check/comments.go (about)

     1  // Copyright 2019 The ChromiumOS Authors
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package check
     6  
     7  import (
     8  	"fmt"
     9  	"go/ast"
    10  	"go/token"
    11  	"strings"
    12  )
    13  
    14  // Comments checks comments on unexported functions (if any) start with the function name.
    15  // Golint examines only exported functions. This check is a complementary of it.
    16  func Comments(fs *token.FileSet, f *ast.File) []*Issue {
    17  	var issues []*Issue
    18  
    19  	v := funcVisitor(func(node ast.Node) {
    20  		fn, ok := node.(*ast.FuncDecl)
    21  		// Exported functions are checked by golint.go .
    22  		if !ok || ast.IsExported(fn.Name.Name) {
    23  			return
    24  		}
    25  		doc := fn.Doc
    26  		if doc == nil {
    27  			return
    28  		}
    29  		prefix := fn.Name.Name + " "
    30  		if strings.HasPrefix(doc.Text(), prefix) {
    31  			return
    32  		}
    33  
    34  		issues = append(issues, &Issue{
    35  			Pos:  fs.Position(doc.Pos()),
    36  			Msg:  fmt.Sprintf("Comment on function %s should be of the form %q", fn.Name.Name, prefix+"..."),
    37  			Link: "https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences",
    38  		})
    39  	})
    40  
    41  	ast.Walk(v, f)
    42  	return issues
    43  }