-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from yoheimuta/enum-fieldNames-prefix
feat: Add the new rule: ENUM_FIELD_NAMES_PREFIX
- v0.53.0
- v0.52.0
- v0.51.0
- v0.50.5
- v0.50.4
- v0.50.3
- v0.50.2
- v0.50.1
- v0.50.0
- v0.49.8
- v0.49.7
- v0.49.6
- v0.49.5
- v0.49.4
- v0.49.3
- v0.49.2
- v0.49.1
- v0.49.0
- v0.48.0
- v0.47.6
- v0.47.5
- v0.47.4
- v0.47.3
- v0.47.2
- v0.47.1
- v0.47.0
- v0.46.3
- v0.46.2
- v0.46.1
- v0.46.0
- v0.45.2
- v0.45.1
- v0.45.0
- v0.44.0
- v0.43.2
- v0.43.1
- v0.43.0
- v0.42.2
- v0.42.1
- v0.42.0
- v0.41.0
- v0.40.0
- v0.39.0
- v0.38.3
- v0.38.2
- v0.38.1
- v0.38.0
- v0.37.1
- v0.37.0
- v0.36.0
- v0.35.2
- v0.35.1
- v0.35.0
- v0.34.1
- v0.34.0
- v0.33.0
- v0.32.0
- v0.31.0
Showing
6 changed files
with
270 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package rules | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/yoheimuta/protolint/linter/strs" | ||
|
||
"github.com/yoheimuta/go-protoparser/v4/parser" | ||
"github.com/yoheimuta/protolint/linter/report" | ||
"github.com/yoheimuta/protolint/linter/visitor" | ||
) | ||
|
||
// EnumFieldNamesPrefixRule verifies that enum field names are prefixed with its ENUM_NAME_UPPER_SNAKE_CASE. | ||
// See https://developers.google.com/protocol-buffers/docs/style#enums. | ||
type EnumFieldNamesPrefixRule struct { | ||
} | ||
|
||
// NewEnumFieldNamesPrefixRule creates a new EnumFieldNamesPrefixRule. | ||
func NewEnumFieldNamesPrefixRule() EnumFieldNamesPrefixRule { | ||
return EnumFieldNamesPrefixRule{} | ||
} | ||
|
||
// ID returns the ID of this rule. | ||
func (r EnumFieldNamesPrefixRule) ID() string { | ||
return "ENUM_FIELD_NAMES_PREFIX" | ||
} | ||
|
||
// Purpose returns the purpose of this rule. | ||
func (r EnumFieldNamesPrefixRule) Purpose() string { | ||
return `Verifies that enum field names are prefixed with its ENUM_NAME_UPPER_SNAKE_CASE.` | ||
} | ||
|
||
// IsOfficial decides whether or not this rule belongs to the official guide. | ||
func (r EnumFieldNamesPrefixRule) IsOfficial() bool { | ||
return true | ||
} | ||
|
||
// Apply applies the rule to the proto. | ||
func (r EnumFieldNamesPrefixRule) Apply(proto *parser.Proto) ([]report.Failure, error) { | ||
v := &enumFieldNamesPrefixVisitor{ | ||
BaseAddVisitor: visitor.NewBaseAddVisitor(r.ID()), | ||
} | ||
return visitor.RunVisitor(v, proto, r.ID()) | ||
} | ||
|
||
type enumFieldNamesPrefixVisitor struct { | ||
*visitor.BaseAddVisitor | ||
enumName string | ||
} | ||
|
||
// VisitEnum checks the enum. | ||
func (v *enumFieldNamesPrefixVisitor) VisitEnum(enum *parser.Enum) bool { | ||
v.enumName = enum.EnumName | ||
return true | ||
} | ||
|
||
// VisitEnumField checks the enum field. | ||
func (v *enumFieldNamesPrefixVisitor) VisitEnumField(field *parser.EnumField) bool { | ||
expectedPrefix, err := strs.ToUpperSnakeCaseFromCamelCase(v.enumName) | ||
if err != nil { | ||
expectedPrefix = strings.ToUpper(v.enumName) | ||
} | ||
if !strings.HasPrefix(field.Ident, expectedPrefix) { | ||
v.AddFailuref(field.Meta.Pos, "EnumField name %q should have the prefix %q", field.Ident, expectedPrefix) | ||
} | ||
return false | ||
} |
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,125 @@ | ||
package rules_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/yoheimuta/protolint/internal/addon/rules" | ||
|
||
"github.com/yoheimuta/go-protoparser/v4/parser" | ||
"github.com/yoheimuta/go-protoparser/v4/parser/meta" | ||
"github.com/yoheimuta/protolint/linter/report" | ||
) | ||
|
||
func TestEnumFieldNamesPrefixRule_Apply(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputProto *parser.Proto | ||
wantFailures []report.Failure | ||
}{ | ||
{ | ||
name: "no failures for proto without enum fields", | ||
inputProto: &parser.Proto{ | ||
ProtoBody: []parser.Visitee{ | ||
&parser.Enum{ | ||
EnumName: "FooBar", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "no failures for proto with valid enum field names", | ||
inputProto: &parser.Proto{ | ||
ProtoBody: []parser.Visitee{ | ||
&parser.Service{}, | ||
&parser.Enum{ | ||
EnumName: "FooBar", | ||
EnumBody: []parser.Visitee{ | ||
&parser.EnumField{ | ||
Ident: "FOO_BAR_UNSPECIFIED", | ||
Number: "0", | ||
}, | ||
&parser.EnumField{ | ||
Ident: "FOO_BAR_FIRST_VALUE", | ||
Number: "1", | ||
}, | ||
&parser.EnumField{ | ||
Ident: "FOO_BAR_SECOND_VALUE", | ||
Number: "2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "no failures for proto with valid enum field names even when its enum name is snake case", | ||
inputProto: &parser.Proto{ | ||
ProtoBody: []parser.Visitee{ | ||
&parser.Service{}, | ||
&parser.Enum{ | ||
EnumName: "foo_bar", | ||
EnumBody: []parser.Visitee{ | ||
&parser.EnumField{ | ||
Ident: "FOO_BAR_UNSPECIFIED", | ||
Number: "0", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "failures for proto with invalid enum field names", | ||
inputProto: &parser.Proto{ | ||
ProtoBody: []parser.Visitee{ | ||
&parser.Enum{ | ||
EnumName: "FooBar", | ||
EnumBody: []parser.Visitee{ | ||
&parser.EnumField{ | ||
Ident: "BAR_UNSPECIFIED", | ||
Number: "0", | ||
Meta: meta.Meta{ | ||
Pos: meta.Position{ | ||
Filename: "example.proto", | ||
Offset: 100, | ||
Line: 5, | ||
Column: 10, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantFailures: []report.Failure{ | ||
report.Failuref( | ||
meta.Position{ | ||
Filename: "example.proto", | ||
Offset: 100, | ||
Line: 5, | ||
Column: 10, | ||
}, | ||
"ENUM_FIELD_NAMES_PREFIX", | ||
`EnumField name "BAR_UNSPECIFIED" should have the prefix "FOO_BAR"`, | ||
), | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
rule := rules.NewEnumFieldNamesPrefixRule() | ||
|
||
got, err := rule.Apply(test.inputProto) | ||
if err != nil { | ||
t.Errorf("got err %v, but want nil", err) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, test.wantFailures) { | ||
t.Errorf("got %v, but want %v", got, test.wantFailures) | ||
} | ||
}) | ||
} | ||
} |
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