-
-
Notifications
You must be signed in to change notification settings - Fork 723
Implement //nolint parsing similar to golangci-lint #3562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7317ed1
Implement //nolint parsing similar to golangci-lint
obs-gh-patrickscott 772261b
Use a map instead of custom Linters type
obs-gh-patrickscott da7819e
Add separate tests for various filters
obs-gh-patrickscott 8dfd83e
Use ast.CommentMap to associate comments to nodes
obs-gh-patrickscott a66f2f3
Address PR feedback
obs-gh-patrickscott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright 2023 The Bazel Authors. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package main | ||
|
|
||
| import "strings" | ||
|
|
||
| // Parse nolint directives and return the applicable linters. If all linters | ||
| // apply, returns (nil, true). | ||
| func parseNolint(text string) (map[string]bool, bool) { | ||
| text = strings.TrimLeft(text, "/ ") | ||
| if !strings.HasPrefix(text, "nolint") { | ||
| return nil, false | ||
| } | ||
| parts := strings.Split(text, ":") | ||
| if len(parts) == 1 { | ||
| return nil, true | ||
| } | ||
| linters := strings.Split(parts[1], ",") | ||
| result := map[string]bool{} | ||
| for _, linter := range linters { | ||
| if strings.EqualFold(linter, "all") { | ||
| return nil, true | ||
| } | ||
| result[linter] = true | ||
| } | ||
| return result, true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright 2023 The Bazel Authors. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseNolint(t *testing.T) { | ||
| tests := []struct { | ||
| Name string | ||
| Comment string | ||
| Valid bool | ||
| Linters []string | ||
| }{ | ||
| { | ||
| Name: "Invalid", | ||
| Comment: "not a comment", | ||
| }, | ||
| { | ||
| Name: "No match", | ||
| Comment: "// comment", | ||
| }, | ||
| { | ||
| Name: "All linters", | ||
| Comment: "//nolint", | ||
| Valid: true, | ||
| }, | ||
| { | ||
| Name: "All linters (explicit)", | ||
| Comment: "//nolint:all", | ||
| Valid: true, | ||
| }, | ||
| { | ||
| Name: "Single linter", | ||
| Comment: "// nolint:foo", | ||
| Valid: true, | ||
| Linters: []string{"foo"}, | ||
| }, | ||
| { | ||
| Name: "Multiple linters", | ||
| Comment: "// nolint:a,b,c", | ||
| Valid: true, | ||
| Linters: []string{"a", "b", "c"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.Name, func(t *testing.T) { | ||
| result, ok := parseNolint(tc.Comment) | ||
| if tc.Valid != ok { | ||
| t.Fatalf("parseNolint expect %t got %t", tc.Valid, ok) | ||
| } | ||
| var linters map[string]bool | ||
| if len(tc.Linters) != 0 { | ||
| linters = make(map[string]bool) | ||
| for _, l := range tc.Linters { | ||
| linters[l] = true | ||
| } | ||
| } | ||
| if !reflect.DeepEqual(result, linters) { | ||
| t.Fatalf("parseNolint expect %v got %v", linters, result) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test") | ||
|
|
||
| go_bazel_test( | ||
| name = "nolint_test", | ||
| srcs = ["nolint_test.go"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| Nolint check | ||
| ========= | ||
|
|
||
| .. _go_library: /docs/go/core/rules.md#_go_library | ||
|
|
||
| Tests to ensure that errors found by nogo and annotated with //nolint are | ||
| ignored. | ||
|
|
||
| .. contents:: | ||
|
|
||
| nolint_test | ||
| -------- | ||
| Verified that errors emitted by ``nogo`` are ignored when `//nolint` appears as | ||
| a comment. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.