Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Add linter to check for keywords in the package name #281

Merged
merged 3 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,16 @@ to the element comment. The following lint rules can be suppressed:
| Lint Rule | Containing Lint Groups | Suppressing Annotation |
|-----------------------------|--------------------------|--------------------------|
| `MESSAGE_FIELDS_NOT_FLOATS` | `uber2` | `floats` |
| `PACKAGE_NO_KEYWORDS` | `uber2` | `keywords` |

As an example:

```
// This contains the "public". keyword.
//
// @suppresswarnings keywords
package foo.public.bar;

// Hello is a field where we understand the concerns with using doubles but require them.
//
// @suppresswarnings floats
Expand Down Expand Up @@ -464,6 +470,8 @@ major version, with some exceptions:
reflect things such as max line lengths.
- The breaking change detector may have additional checks added between minor versions, and therefore a change that might not have been
breaking previously might become a breaking change.
- The `PACKAGE_NO_KEYWORDS` linter on the `uber2` lint group may have additional keywords added. This can be suppressed by adding
`@suppresswarnings keywords` to the package comment.

## Development

Expand Down
21 changes: 17 additions & 4 deletions internal/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,10 @@ func TestLint(t *testing.T) {
t,
false,
`11:3:FIELDS_NOT_RESERVED
12:3:FIELDS_NOT_RESERVED
14:5:FIELDS_NOT_RESERVED
15:5:FIELDS_NOT_RESERVED
20:5:FIELDS_NOT_RESERVED`,
12:3:FIELDS_NOT_RESERVED
14:5:FIELDS_NOT_RESERVED
15:5:FIELDS_NOT_RESERVED
20:5:FIELDS_NOT_RESERVED`,
"testdata/lint/noreserved/foo.proto",
)

Expand Down Expand Up @@ -435,6 +435,19 @@ func TestLint(t *testing.T) {
"testdata/lint/floats/foo/v1/foo.proto",
)

assertDoLintFile(
t,
false,
`3:1:PACKAGE_NO_KEYWORDS`,
"testdata/lint/nokeywords/foo/public/public.proto",
)
assertDoLintFile(
t,
false,
``,
"testdata/lint/nokeywords/foo/public/public_suppressed.proto",
)

assertDoLintFile(
t,
false,
Expand Down
8 changes: 8 additions & 0 deletions internal/cmd/testdata/lint/nokeywords/foo/public/public.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package foo.public;

option go_package = "publicpb";
option java_multiple_files = true;
option java_outer_classname = "PublicProto";
option java_package = "com.foo.public";
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

// @suppresswarnings keywords
package foo.public;

option go_package = "publicpb";
option java_multiple_files = true;
option java_outer_classname = "PublicSuppressedProto";
option java_package = "com.foo.public";
4 changes: 4 additions & 0 deletions internal/cmd/testdata/lint/nokeywords/prototool.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lint:
rules:
add:
- PACKAGE_NO_KEYWORDS
77 changes: 77 additions & 0 deletions internal/lint/check_package_no_keywords.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package lint

import (
"fmt"
"strings"

"github.com/emicklei/proto"
"github.com/uber/prototool/internal/text"
)

const packageNoKeywordsSuppressableAnnotation = "keywords"

var (
packageNoKeywordsKeywords = []string{
"internal", // Golang
"public", // Java, C++, others
"private", // Java, C++, others
"protected", // Java, C++, others
"std", // C++ (causes a problem with the std package)
}

packageNoKeywordsLinter = NewLinter(
"PACKAGE_NO_KEYWORDS",
fmt.Sprintf(`Suppressable with "@suppresswarnings %s". Verifies that no packages contain one of the keywords "%s" as part of the name when split on '.'.`, packageNoKeywordsSuppressableAnnotation, strings.Join(packageNoKeywordsKeywords, ",")),
checkPackageNoKeywords,
)

packageNoKeywordsKeywordsMap = make(map[string]struct{}, len(packageNoKeywordsKeywords))
)

func init() {
for _, keyword := range packageNoKeywordsKeywords {
packageNoKeywordsKeywordsMap[keyword] = struct{}{}
}

}

func checkPackageNoKeywords(add func(*text.Failure), dirPath string, descriptors []*proto.Proto) error {
return runVisitor(packageNoKeywordsVisitor{baseAddVisitor: newBaseAddVisitor(add)}, descriptors)
}

type packageNoKeywordsVisitor struct {
baseAddVisitor
}

func (v packageNoKeywordsVisitor) VisitPackage(pkg *proto.Package) {
for _, subPackage := range strings.Split(pkg.Name, ".") {
potentialKeyword := strings.ToLower(subPackage)
if _, ok := packageNoKeywordsKeywordsMap[potentialKeyword]; ok {
if isSuppressed(pkg.Comment, packageNoKeywordsSuppressableAnnotation) {
// can just return as this will be true for other keywords as well
return
}
v.AddFailuref(pkg.Position, `Package %q contains the keyword %q, this could cause problems in generated code. This can be suppressed by adding "@suppresswarnings %s" to the package comment.`, pkg.Name, potentialKeyword, packageNoKeywordsSuppressableAnnotation)
}
}
}
2 changes: 2 additions & 0 deletions internal/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var (
packageIsDeclaredLinter,
packageLowerSnakeCaseLinter,
packageMajorBetaVersionedLinter,
packageNoKeywordsLinter,
packagesSameInDirLinter,
rpcsHaveCommentsLinter,
rpcNamesCamelCaseLinter,
Expand Down Expand Up @@ -181,6 +182,7 @@ var (
packageIsDeclaredLinter,
packageLowerSnakeCaseLinter,
packageMajorBetaVersionedLinter,
packageNoKeywordsLinter,
packagesSameInDirLinter,
rpcNamesCamelCaseLinter,
rpcNamesCapitalizedLinter,
Expand Down