|
| 1 | +// Copyright (c) 2019 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package lint |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/emicklei/proto" |
| 28 | + "github.com/uber/prototool/internal/text" |
| 29 | +) |
| 30 | + |
| 31 | +const packageNoKeywordsSuppressableAnnotation = "keywords" |
| 32 | + |
| 33 | +var ( |
| 34 | + packageNoKeywordsKeywords = []string{ |
| 35 | + "internal", // Golang |
| 36 | + "public", // Java, C++, others |
| 37 | + "private", // Java, C++, others |
| 38 | + "protected", // Java, C++, others |
| 39 | + "std", // C++ (causes a problem with the std package) |
| 40 | + } |
| 41 | + |
| 42 | + packageNoKeywordsLinter = NewLinter( |
| 43 | + "PACKAGE_NO_KEYWORDS", |
| 44 | + 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, ",")), |
| 45 | + checkPackageNoKeywords, |
| 46 | + ) |
| 47 | + |
| 48 | + packageNoKeywordsKeywordsMap = make(map[string]struct{}, len(packageNoKeywordsKeywords)) |
| 49 | +) |
| 50 | + |
| 51 | +func init() { |
| 52 | + for _, keyword := range packageNoKeywordsKeywords { |
| 53 | + packageNoKeywordsKeywordsMap[keyword] = struct{}{} |
| 54 | + } |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +func checkPackageNoKeywords(add func(*text.Failure), dirPath string, descriptors []*proto.Proto) error { |
| 59 | + return runVisitor(packageNoKeywordsVisitor{baseAddVisitor: newBaseAddVisitor(add)}, descriptors) |
| 60 | +} |
| 61 | + |
| 62 | +type packageNoKeywordsVisitor struct { |
| 63 | + baseAddVisitor |
| 64 | +} |
| 65 | + |
| 66 | +func (v packageNoKeywordsVisitor) VisitPackage(pkg *proto.Package) { |
| 67 | + for _, subPackage := range strings.Split(pkg.Name, ".") { |
| 68 | + potentialKeyword := strings.ToLower(subPackage) |
| 69 | + if _, ok := packageNoKeywordsKeywordsMap[potentialKeyword]; ok { |
| 70 | + if isSuppressed(pkg.Comment, packageNoKeywordsSuppressableAnnotation) { |
| 71 | + // can just return as this will be true for other keywords as well |
| 72 | + return |
| 73 | + } |
| 74 | + 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) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments