github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/syntaxConsistentRule.go (about) 1 package rules 2 3 import ( 4 "github.com/yoheimuta/go-protoparser/v4/parser" 5 6 "github.com/yoheimuta/protolint/linter/report" 7 "github.com/yoheimuta/protolint/linter/rule" 8 "github.com/yoheimuta/protolint/linter/visitor" 9 ) 10 11 // SyntaxConsistentRule verifies that syntax is a specified version. 12 type SyntaxConsistentRule struct { 13 RuleWithSeverity 14 version string 15 } 16 17 // NewSyntaxConsistentRule creates a new SyntaxConsistentRule. 18 func NewSyntaxConsistentRule( 19 severity rule.Severity, 20 version string, 21 ) SyntaxConsistentRule { 22 if len(version) == 0 { 23 version = "proto3" 24 } 25 return SyntaxConsistentRule{ 26 RuleWithSeverity: RuleWithSeverity{severity: severity}, 27 version: version, 28 } 29 } 30 31 // ID returns the ID of this rule. 32 func (r SyntaxConsistentRule) ID() string { 33 return "SYNTAX_CONSISTENT" 34 } 35 36 // Purpose returns the purpose of this rule. 37 func (r SyntaxConsistentRule) Purpose() string { 38 return "Verifies that syntax is a specified version(default is proto3)." 39 } 40 41 // IsOfficial decides whether or not this rule belongs to the official guide. 42 func (r SyntaxConsistentRule) IsOfficial() bool { 43 return false 44 } 45 46 // Apply applies the rule to the proto. 47 func (r SyntaxConsistentRule) Apply(proto *parser.Proto) ([]report.Failure, error) { 48 v := &syntaxConsistentVisitor{ 49 BaseAddVisitor: visitor.NewBaseAddVisitor(r.ID(), string(r.Severity())), 50 version: r.version, 51 } 52 return visitor.RunVisitor(v, proto, r.ID()) 53 } 54 55 type syntaxConsistentVisitor struct { 56 *visitor.BaseAddVisitor 57 version string 58 } 59 60 // VisitSyntax checks the syntax. 61 func (v *syntaxConsistentVisitor) VisitSyntax(s *parser.Syntax) bool { 62 if s.ProtobufVersion != v.version { 63 v.AddFailuref(s.Meta.Pos, "Syntax should be %q but was %q.", v.version, s.ProtobufVersion) 64 } 65 return false 66 }