Skip to content

Commit

Permalink
Add linting rules for fields and enum labels.
Browse files Browse the repository at this point in the history
This change adds following new linting rules:

- `naming.record_fields`: Checks for identifiers of record fields.
- `naming.record_of`:     Checks for record-of identifiers.
- `naming.set_fields`:    Checks for identifiers of set fields.
- `naming.set_of`:        Checks for set-of identifiers.
- `naming.union_fields`:  Checks for identifiers of union fields.
- `naming.enum_label`:    Checks for labels of enumerated types.

Closes #756
  • Loading branch information
5nord committed Nov 3, 2024
1 parent 395523c commit 96b7533
Showing 1 changed file with 83 additions and 9 deletions.
92 changes: 83 additions & 9 deletions lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ Naming Convention Checks
naming.component_consts Checks for component scoped constant identifiers.
naming.templates Checks for constant template identifiers.
naming.locals Checks for local variable identifiers.
naming.record Checks for record identifiers.
naming.set Checks for set identifiers.
naming.union Checks for union identifiers.
naming.enum Checks for enum identifiers
tags.modules Checks for module tags.
naming.record Checks for record identifiers.
naming.record_fields Checks for identifiers of record fields.
naming.record_of Checks for record-of identifiers.
naming.set Checks for set identifiers.
naming.set_fields Checks for identifiers of set fields.
naming.set_of Checks for set-of identifiers.
naming.union Checks for union identifiers.
naming.union_fields Checks for identifiers of union fields.
naming.enum Checks for enum identifiers
naming.enum_label Checks for labels of enumerated types.
tags.modules Checks for module tags.
tags.tests Checks for test-case tags.
Expand Down Expand Up @@ -163,9 +169,15 @@ For information on writing new checks, see <TBD>.
Templates map[string]string
Locals map[string]string
Record map[string]string
RecordFields map[string]string `yaml:"record_fields"`
RecordOf map[string]string `yaml:"record_of"`
Set map[string]string
SetFields map[string]string `yaml:"set_fields"`
SetOf map[string]string `yaml:"set_of"`
Union map[string]string
UnionFields map[string]string `yaml:"union_fields"`
Enum map[string]string
EnumLabels map[string]string `yaml:"enum_labels"`
}
Tags struct {
Modules map[string]string
Expand Down Expand Up @@ -340,21 +352,53 @@ func lint(cmd *cobra.Command, args []string) error {
checkBraces(n.LBrace, n.RBrace)
case *syntax.EnumSpec:
checkBraces(n.LBrace, n.RBrace)
for _, x := range n.Enums {
checkNaming(x, style.Naming.EnumLabels)
}
case *syntax.ModuleParameterGroup:
checkBraces(n.LBrace, n.RBrace)
case *syntax.StructTypeDecl:
checkBraces(n.LBrace, n.RBrace)
var nameRules, fieldRules map[string]string
switch n.Kind.Kind() {
case syntax.RECORD:
checkNaming(n, style.Naming.Record)
nameRules = style.Naming.Record
fieldRules = style.Naming.RecordFields
case syntax.SET:
checkNaming(n, style.Naming.Set)
nameRules = style.Naming.Set
fieldRules = style.Naming.SetFields
case syntax.UNION:
checkNaming(n, style.Naming.Union)
nameRules = style.Naming.Union
fieldRules = style.Naming.UnionFields
default:
log.Verbosef("unknown struct type %q. Ignoring\n", n.Kind.Kind())
return true

}
checkNaming(n, nameRules)
for _, f := range n.Fields {
checkNaming(f, fieldRules)
}
case *syntax.EnumTypeDecl:
checkBraces(n.LBrace, n.RBrace)
checkNaming(n, style.Naming.Enum)
for _, x := range n.Enums {
checkNaming(x, style.Naming.EnumLabels)
}
case *syntax.SubTypeDecl:
if n.Field == nil {
break
}

if lt, ok := n.Field.Type.(*syntax.ListSpec); ok {
switch lt.Kind.Kind() {
case syntax.RECORD:
checkNaming(n, style.Naming.RecordOf)
case syntax.SET:
checkNaming(n, style.Naming.SetOf)
}
}

case *syntax.ImportDecl:
checkBraces(n.LBrace, n.RBrace)
checkImport(n, mod, tree)
Expand Down Expand Up @@ -738,21 +782,51 @@ func buildRegexCache() error {
return err
}
}
for p := range style.Naming.RecordFields {
if err := cacheRegex(p); err != nil {
return err
}
}
for p := range style.Naming.RecordOf {
if err := cacheRegex(p); err != nil {
return err
}
}
for p := range style.Naming.Set {
if err := cacheRegex(p); err != nil {
return err
}
}
for p := range style.Naming.SetFields {
if err := cacheRegex(p); err != nil {
return err
}
}
for p := range style.Naming.SetOf {
if err := cacheRegex(p); err != nil {
return err
}
}
for p := range style.Naming.Union {
if err := cacheRegex(p); err != nil {
return err
}
}
for p := range style.Naming.UnionFields {
if err := cacheRegex(p); err != nil {
return err
}
}
for p := range style.Naming.Enum {
if err := cacheRegex(p); err != nil {
return err
}
}
for p := range style.Naming.EnumLabels {
if err := cacheRegex(p); err != nil {
return err
}
}
for p := range style.Tags.Tests {
if err := cacheRegex(p); err != nil {
return err
Expand Down

0 comments on commit 96b7533

Please sign in to comment.