Skip to content

Commit

Permalink
Merge pull request #366 from yoheimuta/convert-kebab-case
Browse files Browse the repository at this point in the history
feat: Convert kebab case to snake case
  • Loading branch information
yoheimuta committed Mar 9, 2024
2 parents 80ba08f + 63044ba commit f338d2d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions _testdata/rules/fileNamesLowerSnakeCase/kebab-case.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
syntax = "proto3";
1 change: 0 additions & 1 deletion internal/addon/rules/fileNamesLowerSnakeCaseRule.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (v *fileNamesLowerSnakeCaseVisitor) OnStart(proto *parser.Proto) error {
base := strings.TrimSuffix(filename, ext)
if ext != ".proto" || !strs.IsLowerSnakeCase(base) {
expected := strs.ToLowerSnakeCase(base)
expected = strings.ReplaceAll(expected, ".", "_")
expected += ".proto"
v.AddFailurefWithProtoMeta(proto.Meta, "File name %q should be lower_snake_case.proto like %q.", filename, expected)

Expand Down
25 changes: 25 additions & 0 deletions internal/addon/rules/fileNamesLowerSnakeCaseRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ func TestFileNamesLowerSnakeCaseRule_Apply(t *testing.T) {
),
},
},
{
name: "a failure for proto with a kebab case file name",
inputProto: &parser.Proto{
Meta: &parser.ProtoMeta{
Filename: "proto/user-role.proto",
},
},
wantFailures: []report.Failure{
report.Failuref(
meta.Position{
Filename: "proto/user-role.proto",
Offset: 0,
Line: 1,
Column: 1,
},
"FILE_NAMES_LOWER_SNAKE_CASE",
`File name "user-role.proto" should be lower_snake_case.proto like "user_role.proto".`,
),
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -156,6 +176,11 @@ func TestFileNamesLowerSnakeCaseRule_Apply_fix(t *testing.T) {
inputFilename: "UpperCamelCase.proto",
wantFilename: "upper_camel_case.proto",
},
{
name: "fix for a kebab case proto",
inputFilename: "kebab-case.proto",
wantFilename: "kebab_case.proto",
},
}

for _, test := range tests {
Expand Down
4 changes: 4 additions & 0 deletions linter/strs/strs.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func HasAnyUpperCase(s string) bool {

// ToUpperSnakeCase converts s to UPPER_SNAKE_CASE.
func ToUpperSnakeCase(s string) string {
s = strings.ReplaceAll(s, ".", "_")
s = strings.ReplaceAll(s, "-", "_")
ws := SplitCamelCaseWord(s)
if ws == nil {
ws = []string{s}
Expand All @@ -113,6 +115,8 @@ func ToUpperSnakeCase(s string) string {

// ToLowerSnakeCase converts s to lower_snake_case.
func ToLowerSnakeCase(s string) string {
s = strings.ReplaceAll(s, ".", "_")
s = strings.ReplaceAll(s, "-", "_")
ws := SplitCamelCaseWord(s)
if ws == nil {
ws = []string{s}
Expand Down
20 changes: 20 additions & 0 deletions linter/strs/strs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,16 @@ func TestToUpperSnakeCase(t *testing.T) {
input: "ListITCDepartmentRegion",
want: "LIST_ITC_DEPARTMENT_REGION",
},
{
name: "input consists of kebab case",
input: "account-status",
want: "ACCOUNT_STATUS",
},
{
name: "input consists of .",
input: "account.status",
want: "ACCOUNT_STATUS",
},
}

for _, test := range tests {
Expand Down Expand Up @@ -316,6 +326,16 @@ func TestToLowerSnakeCase(t *testing.T) {
input: "accountStatus",
want: "account_status",
},
{
name: "input consists of kebab case",
input: "account-status",
want: "account_status",
},
{
name: "input consists of .",
input: "account.status",
want: "account_status",
},
}

for _, test := range tests {
Expand Down

0 comments on commit f338d2d

Please sign in to comment.