forked from bazel-contrib/rules_go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement //nolint parsing similar to golangci-lint (bazel-contrib#3562)
* Implement //nolint parsing similar to golangci-lint Look for nolint comments and collect the ranges of where they apply. If a node immediately follows a range with the same column, expand the range to include the node. Add tests that verify errors are filtered out. * Use a map instead of custom Linters type Inline the new report function and add some comments about nolint ranges. * Add separate tests for various filters Include a failing test that shows the column issue. Will work on a fix. * Use ast.CommentMap to associate comments to nodes This is better than the logic used by golangci-lint in that it does not incorrectly attribute an inline comment with the next line if it contains an ast.Node with a matching column (see inline_column test). * Address PR feedback Use table driven tests and `CombinedOutput`. --------- Co-authored-by: Patrick Scott <[email protected]>
- Loading branch information
1 parent
ee10888
commit 98f6d33
Showing
7 changed files
with
426 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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 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.