Skip to content

Commit

Permalink
Fix issue linting buf:lint:ignore directives (bufbuild#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAbides committed May 26, 2021
1 parent 714ffd4 commit 5f16fe6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
7 changes: 5 additions & 2 deletions internal/buf/bufcheck/buflint/buflint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ import (
)

// Hint on how to get these:
// 1. cd into the specific diriectory
// 2. buf lint --error-format=json | jq '[.path, ".", .start_line, .start_column, .end_line, .end_column, .type] | @csv' --raw-output
// 1. cd into the specific directory
// 2. buf lint --error-format=json | jq '[.path, .start_line, .start_column, .end_line, .end_column, .type] | @csv' --raw-output
// or
// buf lint --error-format=json | jq -r '"bufanalysistesting.NewFileAnnotation(t, \"\(.path)\", \(.start_line|tostring), \(.start_column|tostring), \(.end_line|tostring), \(.end_column|tostring), \"\(.type)\"),"'

func TestRunComments(t *testing.T) {
testLint(
Expand Down Expand Up @@ -140,6 +142,7 @@ func TestRunComments(t *testing.T) {
bufanalysistesting.NewFileAnnotation(t, "a.proto", 260, 3, 260, 72, "COMMENT_RPC"),
bufanalysistesting.NewFileAnnotation(t, "a.proto", 263, 1, 265, 2, "COMMENT_MESSAGE"),
bufanalysistesting.NewFileAnnotation(t, "a.proto", 264, 3, 264, 30, "COMMENT_FIELD"),
bufanalysistesting.NewFileAnnotation(t, "a.proto", 274, 3, 274, 72, "COMMENT_RPC"),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func checkCommentNamedDescriptor(
// this will magically skip map entry fields as well as a side-effect, although originally unintended
return nil
}
if strings.TrimSpace(location.LeadingComments()) == "" {
if !validLeadingComment(location.LeadingComments()) {
add(namedDescriptor, location, nil, "%s %q should have a non-empty comment for documentation.", typeName, namedDescriptor.Name())
}
return nil
Expand Down
14 changes: 14 additions & 0 deletions internal/buf/bufcheck/buflint/internal/buflintcheck/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package buflintcheck

import (
"strings"

"github.com/bufbuild/buf/internal/buf/bufanalysis"
"github.com/bufbuild/buf/internal/buf/bufcheck/internal"
"github.com/bufbuild/buf/internal/pkg/protosource"
Expand All @@ -40,6 +42,18 @@ func fieldToUpperSnakeCase(s string) string {
return stringutil.ToUpperSnakeCase(s)
}

// validLeadingComment returns true if comment has at least one line that isn't empty and doesn't start with
// "buf:lint:ignore"
func validLeadingComment(comment string) bool {
for _, line := range strings.Split(comment, "\n") {
line = strings.TrimSpace(line)
if line != "" && !strings.HasPrefix(line, "buf:lint:ignore") {
return true
}
}
return false
}

func newFilesCheckFunc(
f func(addFunc, []protosource.File) error,
) func(string, internal.IgnoreFunc, []protosource.File) ([]bufanalysis.FileAnnotation, error) {
Expand Down
10 changes: 10 additions & 0 deletions internal/buf/bufcheck/buflint/testdata/comments/a.proto
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,13 @@ service ServiceFoo5 {
message Baz {
map<int64, string> one = 1;
}

// comment
service ServiceFoo6 {
// comment
rpc MethodFoo(google.protobuf.Empty) returns (google.protobuf.Empty) {}
//buf:lint:ignore RPC_REQUEST_RESPONSE_UNIQUE
// buf:lint:ignore RPC_REQUEST_STANDARD_NAME
//
rpc MethodBar(google.protobuf.Empty) returns (google.protobuf.Empty);
}

0 comments on commit 5f16fe6

Please sign in to comment.