Skip to content

Commit 30b6eac

Browse files
authored
Merge pull request #2 from tonyboxes/objc-lint
add objc lint
2 parents e010c6f + b85c48b commit 30b6eac

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package lint
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"unicode"
9+
10+
"github.com/emicklei/proto"
11+
"github.com/uber/prototool/internal/text"
12+
)
13+
14+
var defaultObjcPrefix = "BAPI"
15+
16+
var fileOptionsObjcPackagePrefixLinter = NewLinter(
17+
"FILE_OPTIONS_OBJC_PACKAGE_PREFIX",
18+
fmt.Sprintf(`Verifies that the file option "objc_class_prefix" has prefix "%s", prefix value can set by environment PROTO_OBJC_PACKAGE_PREFIX`, defaultObjcPrefix),
19+
checkFileOptionsObjcPackagePrefix,
20+
)
21+
22+
func checkFileOptionsObjcPackagePrefix(add func(*text.Failure), dirPath string, descriptors []*FileDescriptor) error {
23+
return runVisitor(&fileOptionsObjcPackagePrefixVisitor{baseAddVisitor: newBaseAddVisitor(add)}, descriptors)
24+
}
25+
26+
type fileOptionsObjcPackagePrefixVisitor struct {
27+
baseAddVisitor
28+
29+
option *proto.Option
30+
fileName string
31+
}
32+
33+
func (v *fileOptionsObjcPackagePrefixVisitor) OnStart(descriptor *FileDescriptor) error {
34+
v.fileName = descriptor.Filename
35+
v.option = nil
36+
return nil
37+
}
38+
39+
func (v *fileOptionsObjcPackagePrefixVisitor) VisitOption(element *proto.Option) {
40+
if element.Name == "objc_class_prefix" {
41+
v.option = element
42+
}
43+
}
44+
45+
func (v *fileOptionsObjcPackagePrefixVisitor) Finally() error {
46+
if v.option == nil {
47+
return nil
48+
}
49+
value := v.option.Constant.Source
50+
prefix := defaultObjcPrefix
51+
if v := os.Getenv("PROTO_OBJC_PACKAGE_PREFIX"); v != "" {
52+
prefix = v
53+
}
54+
words := strings.Split(filepath.Dir(v.fileName), "/")
55+
for _, v := range words {
56+
prefix += objcUCFirst(v)
57+
}
58+
if value != prefix {
59+
v.AddFailuref(v.option.Position, `Expect option "objc_package" as: "%s" actual: "%s"`, prefix, value)
60+
}
61+
return nil
62+
}
63+
64+
func objcUCFirst(str string) string {
65+
for i, v := range str {
66+
return string(unicode.ToUpper(v)) + str[i+1:]
67+
}
68+
return ""
69+
}

internal/lint/lint.go

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ var (
138138
fileOptionsGoPackagePrefixLinter,
139139
importsScopeLinter,
140140
fileOptionsJavaPackagePrefixLinter,
141+
fileOptionsObjcPackagePrefixLinter,
141142
}
142143

143144
// DefaultLinters is the slice of default Linters.

0 commit comments

Comments
 (0)