Skip to content

Commit

Permalink
Fix comment ignore with trailing comment not recognized (#3295)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversun9 authored Sep 6, 2024
1 parent b8d651f commit ce4082e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## [Unreleased]

- No changes yet.
- Fix issue with `buf lint` where comment ignores in the shape of `// buf:lint:ignore <RULE_ID> <extra comment>`
were not recognized due to the extra comment.

## [v1.40.0] - 2024-09-04

Expand Down
24 changes: 11 additions & 13 deletions private/bufpkg/bufcheck/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,27 +491,25 @@ func ignoreLocation(
}

// checkCommentLineForCheckIgnore checks that the comment line starts with the configured
// comment ignore prefix, and the rest of the string is the ruleID of the check.
// comment ignore prefix, a space and the ruleID of the check.
//
// We currently do not support multiple rules per comment ignore:
// All of the following comments are valid, ignoring SERVICE_PASCAL_CASE and this rule only:
//
// Invalid:
// // buf:lint:ignore SERVICE_SUFFIX, SERVICE_PASCAL_CASE
// // buf:lint:ignore SERVICE_PASCAL_CASE, SERVICE_SUFFIX (only SERVICE_PASCAL_CASE is ignored)
// // buf:lint:ignore SERVICE_PASCAL_CASE
// // buf:lint:ignore SERVICE_PASCAL_CASEsome other comment
// // buf:lint:ignore SERVICE_PASCAL_CASE some other comment
//
// Valid:
// // buf:lint:ignore SERVICE_SUFFIX
// // buf:lint:ignore SERVICE_PASCAL_CASE
// While the following is invalid and a nop
//
// // buf:lint:ignoreSERVICE_PASCAL_CASE
func checkCommentLineForCheckIgnore(
commentLine string,
commentIgnorePrefix string,
ruleID string,
) bool {
if after, ok := strings.CutPrefix(commentLine, commentIgnorePrefix); ok {
if strings.TrimSpace(after) == ruleID {
return true
}
}
return false
fullIgnorePrefix := commentIgnorePrefix + " " + ruleID
return strings.HasPrefix(commentLine, fullIgnorePrefix)
}

type lintOptions struct {
Expand Down
8 changes: 8 additions & 0 deletions private/bufpkg/bufcheck/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,14 @@ func TestCommentIgnoresOnlyRule(t *testing.T) {
)
}

func TestCommentIgnoresWithTrailingComment(t *testing.T) {
t.Parallel()
testLint(
t,
"comment_ignores_with_trailing_comment",
)
}

func TestRunLintCustomPlugins(t *testing.T) {
t.Parallel()
testLint(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
syntax = "proto3";

// buf:lint:ignore PACKAGE_DIRECTORY_MATCH trailing comment after the ignore comment is also OK
package a;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: v2
lint:
use:
- PACKAGE_DIRECTORY_MATCH

0 comments on commit ce4082e

Please sign in to comment.