-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This mimics the annotation-based syntax: nolint = "" @NOLINT nolint = "a,b" @NOLINT(a,b) The annotation syntax is preferred, but the documentation block syntax is useful for those few cases where the target node doesn't support Thrift annotations (such as `const` declarations).
- Loading branch information
Showing
6 changed files
with
241 additions
and
24 deletions.
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
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,65 @@ | ||
// Copyright 2021 Pinterest | ||
// | ||
// 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 thriftcheck | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"go.uber.org/thriftrw/ast" | ||
) | ||
|
||
var nolintRegexp = regexp.MustCompile(`@nolint(?:\((.*)\))?`) | ||
|
||
func nolint(n ast.Node) ([]string, bool) { | ||
var names []string | ||
|
||
if annotations := Annotations(n); annotations != nil { | ||
for _, annotation := range annotations { | ||
if annotation.Name == "nolint" { | ||
if annotation.Value == "" { | ||
return nil, true | ||
} | ||
names = append(names, splitTrim(annotation.Value, ",")...) | ||
} | ||
} | ||
} | ||
|
||
if doc := Doc(n); doc != "" { | ||
if m := nolintRegexp.FindStringSubmatch(doc); len(m) == 2 { | ||
if m[1] == "" { | ||
return nil, true | ||
} | ||
names = append(names, splitTrim(m[1], ",")...) | ||
} | ||
} | ||
|
||
if names == nil { | ||
return nil, false | ||
} | ||
|
||
// This may contain duplicate names at this point, but given how this | ||
// return value is used, we can tolerate that without doing the extra | ||
// work here to de-duplicate. | ||
return names, true | ||
} | ||
|
||
func splitTrim(s, sep string) []string { | ||
values := strings.Split(s, sep) | ||
for i := range values { | ||
values[i] = strings.TrimSpace(values[i]) | ||
} | ||
return values | ||
} |
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,112 @@ | ||
// Copyright 2021 Pinterest | ||
// | ||
// 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 thriftcheck | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"go.uber.org/thriftrw/ast" | ||
) | ||
|
||
func TestNolint(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
node ast.Node | ||
ok bool | ||
names []string | ||
}{ | ||
{ | ||
desc: "empty", | ||
node: &ast.Struct{}, | ||
ok: false, | ||
names: nil, | ||
}, | ||
{ | ||
desc: "words", | ||
node: &ast.Struct{Doc: "Just some words"}, | ||
ok: false, | ||
names: nil, | ||
}, | ||
{ | ||
desc: `(nolint = "")`, | ||
node: &ast.Struct{ | ||
Annotations: []*ast.Annotation{{Name: "nolint", Value: ""}}, | ||
}, | ||
ok: true, | ||
names: nil, | ||
}, | ||
{ | ||
desc: `(nolint = "a")`, | ||
node: &ast.Struct{ | ||
Annotations: []*ast.Annotation{{Name: "nolint", Value: "a"}}, | ||
}, | ||
ok: true, | ||
names: []string{"a"}, | ||
}, | ||
{ | ||
desc: `(nolint = "a,b")`, | ||
node: &ast.Struct{ | ||
Annotations: []*ast.Annotation{{Name: "nolint", Value: "a,b"}}, | ||
}, | ||
ok: true, | ||
names: []string{"a", "b"}, | ||
}, | ||
{ | ||
desc: "@nolint", | ||
node: &ast.Struct{Doc: "@nolint"}, | ||
ok: true, | ||
names: nil, | ||
}, | ||
{ | ||
desc: "@nolint()", | ||
node: &ast.Struct{Doc: "@nolint()"}, | ||
ok: true, | ||
names: nil, | ||
}, | ||
{ | ||
desc: "@nolint(a)", | ||
node: &ast.Struct{Doc: "@nolint(a)"}, | ||
ok: true, | ||
names: []string{"a"}, | ||
}, | ||
{ | ||
desc: "@nolint(a,b)", | ||
node: &ast.Struct{Doc: "@nolint(a,b)"}, | ||
ok: true, | ||
names: []string{"a", "b"}, | ||
}, | ||
{ | ||
desc: `(nolint = "a,b") and @nolint(b,c)`, | ||
node: &ast.Struct{ | ||
Annotations: []*ast.Annotation{{Name: "nolint", Value: "a,b"}}, | ||
Doc: "@nolint(b,c)", | ||
}, | ||
ok: true, | ||
names: []string{"a", "b", "b", "c"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
names, ok := nolint(tt.node) | ||
if ok != tt.ok { | ||
t.Errorf("expected %v, got %v", tt.ok, ok) | ||
} else if !reflect.DeepEqual(names, tt.names) { | ||
t.Errorf("expected %v, got %v", tt.names, names) | ||
} | ||
}) | ||
} | ||
} |