github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/linter/visitor/hasExtendedVisitor.go (about)

     1  package visitor
     2  
     3  import (
     4  	"github.com/yoheimuta/go-protoparser/v4/parser"
     5  
     6  	"github.com/yoheimuta/protolint/linter/autodisable"
     7  	"github.com/yoheimuta/protolint/linter/report"
     8  )
     9  
    10  // HasExtendedVisitor is a required interface given to RunVisitor.
    11  type HasExtendedVisitor interface {
    12  	parser.Visitor
    13  
    14  	// OnStart is called when visiting is started.
    15  	OnStart(*parser.Proto) error
    16  	// Finally is called when visiting is done.
    17  	Finally() error
    18  	// Failures returns the accumulated failures.
    19  	Failures() []report.Failure
    20  }
    21  
    22  // RunVisitor dispatches the call to the visitor.
    23  func RunVisitor(
    24  	visitor HasExtendedVisitor,
    25  	proto *parser.Proto,
    26  	ruleID string,
    27  ) ([]report.Failure, error) {
    28  	return RunVisitorAutoDisable(visitor, proto, ruleID, autodisable.Noop)
    29  }
    30  
    31  // RunVisitorAutoDisable dispatches the call to the visitor.
    32  func RunVisitorAutoDisable(
    33  	visitor HasExtendedVisitor,
    34  	proto *parser.Proto,
    35  	ruleID string,
    36  	autodisableType autodisable.PlacementType,
    37  ) ([]report.Failure, error) {
    38  	// This check is just for existing test cases.
    39  	protoFilename := ""
    40  	if proto.Meta != nil {
    41  		protoFilename = proto.Meta.Filename
    42  	}
    43  	autoDisabled, err := newExtendedAutoDisableVisitor(visitor, ruleID, protoFilename, autodisableType)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	disabled := newExtendedDisableRuleVisitor(
    48  		autoDisabled,
    49  		ruleID,
    50  	)
    51  
    52  	if err := disabled.OnStart(proto); err != nil {
    53  		return nil, err
    54  	}
    55  	proto.Accept(disabled)
    56  	if err := disabled.Finally(); err != nil {
    57  		return nil, err
    58  	}
    59  	return disabled.Failures(), nil
    60  }