Skip to content

Commit

Permalink
refactor exported rule configuration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
comdiv committed Jul 3, 2024
1 parent 8f7a95a commit 2f9bc25
Showing 1 changed file with 19 additions and 45 deletions.
64 changes: 19 additions & 45 deletions rule/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,29 @@ type ExportedRule struct {

func (r *ExportedRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if !r.configured {
var sayRepetitiveInsteadOfStutters bool

r.checkPrivateReceivers,
r.disableStutteringCheck,
sayRepetitiveInsteadOfStutters,
r.checkPublicInterface = r.getConf(arguments)

r.stuttersMsg = "stutters"
if sayRepetitiveInsteadOfStutters {
r.stuttersMsg = "is repetitive"
for _, flag := range arguments {
flagStr, ok := flag.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument for the %s rule: expecting a string, got %T", r.Name(), flag))
}
r.stuttersMsg = "stutters"
switch flagStr {
case "checkPrivateReceivers":
r.checkPrivateReceivers = true
case "disableStutteringCheck":
r.disableStutteringCheck = true
case "sayRepetitiveInsteadOfStutters":
r.stuttersMsg = "is repetitive"
case "checkPublicInterface":
r.checkPublicInterface = true
default:
panic(fmt.Sprintf("Unknown configuration flag %s for %s rule", flagStr, r.Name()))
}
}

r.configured = true
}
r.Unlock()
}

// Apply applies the rule to given file.
Expand Down Expand Up @@ -77,39 +84,6 @@ func (*ExportedRule) Name() string {
return "exported"
}

func (r *ExportedRule) getConf(args lint.Arguments) (
checkPrivateReceivers,
disableStutteringCheck,
sayRepetitiveInsteadOfStutters bool,
checkPublicInterface bool,
) {
// if any, we expect a slice of strings as configuration
if len(args) < 1 {
return
}
for _, flag := range args {
flagStr, ok := flag.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument for the %s rule: expecting a string, got %T", r.Name(), flag))
}

switch flagStr {
case "checkPrivateReceivers":
checkPrivateReceivers = true
case "disableStutteringCheck":
disableStutteringCheck = true
case "sayRepetitiveInsteadOfStutters":
sayRepetitiveInsteadOfStutters = true
case "checkPublicInterface":
checkPublicInterface = true
default:
panic(fmt.Sprintf("Unknown configuration flag %s for %s rule", flagStr, r.Name()))
}
}

return
}

type lintExported struct {
file *lint.File
fileAst *ast.File
Expand Down

0 comments on commit 2f9bc25

Please sign in to comment.