Skip to content

Commit

Permalink
add comments all and go_package prefix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwei7 committed Aug 6, 2019
1 parent a394143 commit cfd75b8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import (
"github.com/uber/prototool/internal/text"
)

var commentsChecker = NewAddChecker(
"COMMENTS",
"varifies comments",
checkComments,
var commentsAllLinter = NewLinter(
"COMMENTS_ALL",
"varifies that every field has comment.",
checkCommentsAll,
)

func checkComments(add func(*text.Failure), dirPath string, descriptors []*proto.Proto) error {
return runVisitor(&commentsVisitor{baseAddVisitor: newBaseAddVisitor(add)}, descriptors)
func checkCommentsAll(add func(*text.Failure), dirPath string, descriptors []*FileDescriptor) error {
return runVisitor(&commentsAllVisitor{baseAddVisitor: newBaseAddVisitor(add)}, descriptors)
}

type commentsVisitor struct {
type commentsAllVisitor struct {
baseAddVisitor
}

func (v commentsVisitor) VisitMessage(element *proto.Message) {
func (v commentsAllVisitor) VisitMessage(element *proto.Message) {
if !strings.HasSuffix(element.Name, "Req") && !strings.HasSuffix(element.Name, "Reply") {
v.checkComments(element.Position, element.Comment)
}
Expand All @@ -31,53 +31,53 @@ func (v commentsVisitor) VisitMessage(element *proto.Message) {
}
}

func (v commentsVisitor) VisitService(element *proto.Service) {
func (v commentsAllVisitor) VisitService(element *proto.Service) {
v.checkComments(element.Position, element.Comment)
for _, child := range element.Elements {
child.Accept(v)
}
}

func (v commentsVisitor) VisitNormalField(element *proto.NormalField) {
func (v commentsAllVisitor) VisitNormalField(element *proto.NormalField) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
for _, child := range element.Options {
child.Accept(v)
}
}

func (v commentsVisitor) VisitEnumField(element *proto.EnumField) {
func (v commentsAllVisitor) VisitEnumField(element *proto.EnumField) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
if element.ValueOption != nil {
element.ValueOption.Accept(v)
}
}

func (v commentsVisitor) VisitEnum(element *proto.Enum) {
func (v commentsAllVisitor) VisitEnum(element *proto.Enum) {
v.checkComments(element.Position, element.Comment)
for _, child := range element.Elements {
child.Accept(v)
}
}

func (v commentsVisitor) VisitOneof(element *proto.Oneof) {
func (v commentsAllVisitor) VisitOneof(element *proto.Oneof) {
v.checkComments(element.Position, element.Comment)
for _, child := range element.Elements {
child.Accept(v)
}
}

func (v commentsVisitor) VisitOneofField(element *proto.OneOfField) {
func (v commentsAllVisitor) VisitOneofField(element *proto.OneOfField) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
for _, child := range element.Options {
child.Accept(v)
}
}

func (v commentsVisitor) VisitRPC(element *proto.RPC) {
func (v commentsAllVisitor) VisitRPC(element *proto.RPC) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
}

func (v commentsVisitor) checkComments(position scanner.Position, comments ...*proto.Comment) {
func (v commentsAllVisitor) checkComments(position scanner.Position, comments ...*proto.Comment) {
for _, comment := range comments {
if comment != nil {
return
Expand Down
54 changes: 54 additions & 0 deletions internal/lint/check_go_package_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package lint

import (
"fmt"
"os"
"strings"

"github.com/emicklei/proto"
"github.com/uber/prototool/internal/text"
)

var defaultPrefix = "git.bilibili.co/bapis/bapis-go"

var fileOptionsGoPackagePrefixLinter = NewLinter(
"FILE_OPTIONS_GO_PACKAGE_PREFIX",
fmt.Sprintf(`Verifies that the file option "go_package" has prefix "%s", prefix value can set by environment PROTO_GO_PACKAGE_PREFIX`, defaultPrefix),
checkFileOptionsGoPackagePrefix,
)

func checkFileOptionsGoPackagePrefix(add func(*text.Failure), dirPath string, descriptors []*FileDescriptor) error {
return runVisitor(&fileOptionsGoPackagePrefixVisitor{baseAddVisitor: newBaseAddVisitor(add)}, descriptors)
}

type fileOptionsGoPackagePrefixVisitor struct {
baseAddVisitor

option *proto.Option
}

func (v *fileOptionsGoPackagePrefixVisitor) OnStart(descriptor *FileDescriptor) error {
v.option = nil
return nil
}

func (v *fileOptionsGoPackagePrefixVisitor) VisitOption(element *proto.Option) {
if element.Name == "go_package" {
v.option = element
}
}

func (v *fileOptionsGoPackagePrefixVisitor) Finally() error {
if v.option == nil {
return nil
}
value := v.option.Constant.Source
prefix := defaultPrefix
if v := os.Getenv("PROTO_GO_PACKAGE_PREFIX"); v != "" {
prefix = v
}
if !strings.HasPrefix(value, prefix) {
v.AddFailuref(v.option.Position, `Option "go_package" must has prefix: %s`, prefix)
}
return nil
}
2 changes: 2 additions & 0 deletions internal/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ var (
wktDirectlyImportedLinter,
wktDurationSuffixLinter,
wktTimestampSuffixLinter,
commentsAllLinter,
fileOptionsGoPackagePrefixLinter,
}

// DefaultLinters is the slice of default Linters.
Expand Down
Loading

0 comments on commit cfd75b8

Please sign in to comment.