github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/fileHasCommentRule.go (about)

     1  package rules
     2  
     3  import (
     4  	"github.com/yoheimuta/go-protoparser/v4/parser"
     5  	"github.com/yoheimuta/protolint/linter/report"
     6  	"github.com/yoheimuta/protolint/linter/rule"
     7  	"github.com/yoheimuta/protolint/linter/visitor"
     8  )
     9  
    10  // FileHasCommentRule verifies that a file starts with a doc comment.
    11  type FileHasCommentRule struct {
    12  	RuleWithSeverity
    13  }
    14  
    15  // NewFileHasCommentRule creates a new FileHasCommentRule.
    16  func NewFileHasCommentRule(severity rule.Severity) FileHasCommentRule {
    17  	return FileHasCommentRule{
    18  		RuleWithSeverity: RuleWithSeverity{severity: severity},
    19  	}
    20  }
    21  
    22  // ID returns the ID of this rule.
    23  func (r FileHasCommentRule) ID() string {
    24  	return "FILE_HAS_COMMENT"
    25  }
    26  
    27  // Purpose returns the purpose of this rule.
    28  func (r FileHasCommentRule) Purpose() string {
    29  	return "Verifies that a file starts with a doc comment."
    30  }
    31  
    32  // IsOfficial decides whether or not this rule belongs to the official guide.
    33  func (r FileHasCommentRule) IsOfficial() bool {
    34  	return false
    35  }
    36  
    37  // Apply applies the rule to the proto.
    38  func (r FileHasCommentRule) Apply(proto *parser.Proto) ([]report.Failure, error) {
    39  	v := &fileHasCommentVisitor{
    40  		BaseAddVisitor: visitor.NewBaseAddVisitor(r.ID(), string(r.Severity())),
    41  	}
    42  	return visitor.RunVisitor(v, proto, r.ID())
    43  }
    44  
    45  type fileHasCommentVisitor struct {
    46  	*visitor.BaseAddVisitor
    47  }
    48  
    49  // VisitSyntax checks the syntax.
    50  func (v *fileHasCommentVisitor) VisitSyntax(s *parser.Syntax) bool {
    51  	if !hasComment(s.Comments) {
    52  		v.AddFailuref(s.Meta.Pos, `File should start with a doc comment`)
    53  	}
    54  	return false
    55  }