github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/linter/visitor/extendedAutoDisableVisitor.go (about) 1 package visitor 2 3 import ( 4 "github.com/yoheimuta/go-protoparser/v4/parser" 5 "github.com/yoheimuta/protolint/linter/autodisable" 6 "github.com/yoheimuta/protolint/linter/report" 7 ) 8 9 type extendedAutoDisableVisitor struct { 10 inner HasExtendedVisitor 11 automator autodisable.PlacementStrategy 12 } 13 14 func newExtendedAutoDisableVisitor( 15 inner HasExtendedVisitor, 16 ruleID string, 17 protoFilename string, 18 placementType autodisable.PlacementType, 19 ) (*extendedAutoDisableVisitor, error) { 20 automator, err := autodisable.NewPlacementStrategy(placementType, protoFilename, ruleID) 21 if err != nil { 22 return nil, err 23 } 24 25 return &extendedAutoDisableVisitor{ 26 inner: inner, 27 automator: automator, 28 }, nil 29 } 30 31 func (v *extendedAutoDisableVisitor) OnStart(p *parser.Proto) error { return v.inner.OnStart(p) } 32 func (v *extendedAutoDisableVisitor) Finally() error { 33 err := v.automator.Finalize() 34 if err != nil { 35 return err 36 } 37 return v.inner.Finally() 38 } 39 func (v *extendedAutoDisableVisitor) Failures() []report.Failure { return v.inner.Failures() } 40 41 func (v *extendedAutoDisableVisitor) VisitEmptyStatement(e *parser.EmptyStatement) (next bool) { 42 return v.inner.VisitEmptyStatement(e) 43 } 44 45 func (v *extendedAutoDisableVisitor) VisitComment(c *parser.Comment) { 46 v.inner.VisitComment(c) 47 } 48 49 func (v *extendedAutoDisableVisitor) VisitEnum(e *parser.Enum) (next bool) { 50 return v.doIfFailure(func() bool { 51 return v.inner.VisitEnum(e) 52 }, func(offset int) { 53 v.automator.Disable(offset, e.Comments, e.InlineCommentBehindLeftCurly) 54 }) 55 } 56 57 func (v *extendedAutoDisableVisitor) VisitEnumField(e *parser.EnumField) (next bool) { 58 return v.doIfFailure(func() bool { 59 return v.inner.VisitEnumField(e) 60 }, func(offset int) { 61 v.automator.Disable(offset, e.Comments, e.InlineComment) 62 }) 63 } 64 65 func (v *extendedAutoDisableVisitor) VisitExtend(m *parser.Extend) (next bool) { 66 return v.inner.VisitExtend(m) 67 } 68 69 func (v *extendedAutoDisableVisitor) VisitExtensions(m *parser.Extensions) (next bool) { 70 return v.inner.VisitExtensions(m) 71 } 72 73 func (v *extendedAutoDisableVisitor) VisitField(f *parser.Field) (next bool) { 74 return v.doIfFailure(func() bool { 75 return v.inner.VisitField(f) 76 }, func(offset int) { 77 v.automator.Disable(offset, f.Comments, f.InlineComment) 78 }) 79 } 80 81 func (v *extendedAutoDisableVisitor) VisitGroupField(m *parser.GroupField) (next bool) { 82 return v.doIfFailure(func() bool { 83 return v.inner.VisitGroupField(m) 84 }, func(offset int) { 85 v.automator.Disable(offset, m.Comments, m.InlineComment) 86 }) 87 } 88 89 func (v *extendedAutoDisableVisitor) VisitImport(i *parser.Import) (next bool) { 90 return v.inner.VisitImport(i) 91 } 92 93 func (v *extendedAutoDisableVisitor) VisitMapField(m *parser.MapField) (next bool) { 94 return v.doIfFailure(func() bool { 95 return v.inner.VisitMapField(m) 96 }, func(offset int) { 97 v.automator.Disable(offset, m.Comments, m.InlineComment) 98 }) 99 } 100 101 func (v *extendedAutoDisableVisitor) VisitMessage(m *parser.Message) (next bool) { 102 return v.doIfFailure(func() bool { 103 return v.inner.VisitMessage(m) 104 }, func(offset int) { 105 v.automator.Disable(offset, m.Comments, m.InlineCommentBehindLeftCurly) 106 }) 107 } 108 109 func (v *extendedAutoDisableVisitor) VisitOneof(o *parser.Oneof) (next bool) { 110 return v.inner.VisitOneof(o) 111 } 112 113 func (v *extendedAutoDisableVisitor) VisitOneofField(o *parser.OneofField) (next bool) { 114 return v.doIfFailure(func() bool { 115 return v.inner.VisitOneofField(o) 116 }, func(offset int) { 117 v.automator.Disable(offset, o.Comments, o.InlineComment) 118 }) 119 } 120 121 func (v *extendedAutoDisableVisitor) VisitOption(o *parser.Option) (next bool) { 122 return v.inner.VisitOption(o) 123 } 124 125 func (v *extendedAutoDisableVisitor) VisitPackage(p *parser.Package) (next bool) { 126 return v.inner.VisitPackage(p) 127 } 128 129 func (v *extendedAutoDisableVisitor) VisitReserved(r *parser.Reserved) (next bool) { 130 return v.inner.VisitReserved(r) 131 } 132 133 func (v *extendedAutoDisableVisitor) VisitRPC(r *parser.RPC) (next bool) { 134 return v.doIfFailure(func() bool { 135 return v.inner.VisitRPC(r) 136 }, func(offset int) { 137 v.automator.Disable(offset, r.Comments, r.InlineComment) 138 }) 139 } 140 141 func (v *extendedAutoDisableVisitor) VisitService(s *parser.Service) (next bool) { 142 return v.doIfFailure(func() bool { 143 return v.inner.VisitService(s) 144 }, func(offset int) { 145 v.automator.Disable(offset, s.Comments, s.InlineCommentBehindLeftCurly) 146 }) 147 } 148 149 func (v *extendedAutoDisableVisitor) VisitSyntax(s *parser.Syntax) (next bool) { 150 return v.inner.VisitSyntax(s) 151 } 152 153 func (v *extendedAutoDisableVisitor) doIfFailure( 154 visit func() bool, 155 disable func(int), 156 ) bool { 157 prev := v.inner.Failures() 158 next := visit() 159 curr := v.inner.Failures() 160 if len(prev) == len(curr) { 161 return next 162 } 163 disable(curr[len(curr)-1].Pos().Offset) 164 return next 165 }