Skip to content

Commit

Permalink
refactor: replace mutex with sync.Once for rule configuration (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Nov 15, 2024
1 parent 7ee4500 commit 3378f70
Show file tree
Hide file tree
Showing 32 changed files with 118 additions and 297 deletions.
8 changes: 3 additions & 5 deletions rule/add_constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ type AddConstantRule struct {
allowList allowList
ignoreFunctions []*regexp.Regexp
strLitLimit int
sync.Mutex

configureOnce sync.Once
}

// Apply applies the rule to given file.
func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
r.configureOnce.Do(func() { r.configure(arguments) })

var failures []lint.Failure

Expand Down Expand Up @@ -201,9 +202,6 @@ func (w *lintAddConstantRule) isStructTag(n *ast.BasicLit) bool {
}

func (r *AddConstantRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()

if r.allowList == nil {
r.strLitLimit = defaultStrLitLimit
r.allowList = newAllowList()
Expand Down
11 changes: 3 additions & 8 deletions rule/argument_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@ import (
// ArgumentsLimitRule lints given else constructs.
type ArgumentsLimitRule struct {
max int
sync.Mutex

configureOnce sync.Once
}

const defaultArgumentsLimit = 8

func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.max != 0 {
return
}

if len(arguments) < 1 {
r.max = defaultArgumentsLimit
return
Expand All @@ -37,7 +32,7 @@ func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) {

// Apply applies the rule to given file.
func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
r.configureOnce.Do(func() { r.configure(arguments) })

var failures []lint.Failure
onFailure := func(failure lint.Failure) {
Expand Down
9 changes: 4 additions & 5 deletions rule/banned_characters.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@ import (
// BannedCharsRule checks if a file contains banned characters.
type BannedCharsRule struct {
bannedCharList []string
sync.Mutex

configureOnce sync.Once
}

const bannedCharsRuleName = "banned-characters"

func (r *BannedCharsRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.bannedCharList == nil && len(arguments) > 0 {
if len(arguments) > 0 {
checkNumberOfArguments(1, arguments, bannedCharsRuleName)
r.bannedCharList = r.getBannedCharsList(arguments)
}
}

// Apply applied the rule to the given file.
func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
r.configureOnce.Do(func() { r.configure(arguments) })

var failures []lint.Failure
onFailure := func(failure lint.Failure) {
Expand Down
11 changes: 3 additions & 8 deletions rule/cognitive_complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@ import (
// CognitiveComplexityRule lints given else constructs.
type CognitiveComplexityRule struct {
maxComplexity int
sync.Mutex

configureOnce sync.Once
}

const defaultMaxCognitiveComplexity = 7

func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.maxComplexity != 0 {
return // already configured
}

if len(arguments) < 1 {
r.maxComplexity = defaultMaxCognitiveComplexity
return
Expand All @@ -40,7 +35,7 @@ func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) {

// Apply applies the rule to given file.
func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
r.configureOnce.Do(func() { r.configure(arguments) })

var failures []lint.Failure

Expand Down
11 changes: 3 additions & 8 deletions rule/comment_spacings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@ import (
// the comment symbol( // ) and the start of the comment text
type CommentSpacingsRule struct {
allowList []string
sync.Mutex

configureOnce sync.Once
}

func (r *CommentSpacingsRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.allowList != nil {
return // already configured
}

r.allowList = []string{}
for _, arg := range arguments {
allow, ok := arg.(string) // Alt. non panicking version
Expand All @@ -34,7 +29,7 @@ func (r *CommentSpacingsRule) configure(arguments lint.Arguments) {

// Apply the rule.
func (r *CommentSpacingsRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
r.configure(args)
r.configureOnce.Do(func() { r.configure(args) })

var failures []lint.Failure

Expand Down
15 changes: 3 additions & 12 deletions rule/comments_density.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,13 @@ import (
// CommentsDensityRule lints given else constructs.
type CommentsDensityRule struct {
minimumCommentsDensity int64
configured bool
sync.Mutex

configureOnce sync.Once
}

const defaultMinimumCommentsPercentage = 0

func (r *CommentsDensityRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()

if r.configured {
return
}

r.configured = true

if len(arguments) < 1 {
r.minimumCommentsDensity = defaultMinimumCommentsPercentage
return
Expand All @@ -42,7 +33,7 @@ func (r *CommentsDensityRule) configure(arguments lint.Arguments) {

// Apply applies the rule to given file.
func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
r.configureOnce.Do(func() { r.configure(arguments) })

commentsLines := countDocLines(file.AST.Comments)
statementsCount := countStatements(file.AST)
Expand Down
15 changes: 7 additions & 8 deletions rule/context_as_argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,31 @@ import (
// ContextAsArgumentRule lints given else constructs.
type ContextAsArgumentRule struct {
allowTypesLUT map[string]struct{}
sync.Mutex

configureOnce sync.Once
}

// Apply applies the rule to given file.
func (r *ContextAsArgumentRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
r.Lock()
if r.allowTypesLUT == nil {
r.allowTypesLUT = getAllowTypesFromArguments(args)
}
r.Unlock()
r.configureOnce.Do(func() { r.configure(args) })

var failures []lint.Failure
r.Lock()
walker := lintContextArguments{
allowTypesLUT: r.allowTypesLUT,
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
}
r.Unlock()

ast.Walk(walker, file.AST)

return failures
}

func (r *ContextAsArgumentRule) configure(arguments lint.Arguments) {
r.allowTypesLUT = getAllowTypesFromArguments(arguments)
}

// Name returns the rule name.
func (*ContextAsArgumentRule) Name() string {
return "context-as-argument"
Expand Down
11 changes: 3 additions & 8 deletions rule/cyclomatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ import (
// CyclomaticRule lints given else constructs.
type CyclomaticRule struct {
maxComplexity int
sync.Mutex

configureOnce sync.Once
}

const defaultMaxCyclomaticComplexity = 10

func (r *CyclomaticRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.maxComplexity != 0 {
return // already configured
}

if len(arguments) < 1 {
r.maxComplexity = defaultMaxCyclomaticComplexity
return
Expand All @@ -40,7 +35,7 @@ func (r *CyclomaticRule) configure(arguments lint.Arguments) {

// Apply applies the rule to given file.
func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
r.configureOnce.Do(func() { r.configure(arguments) })

var failures []lint.Failure
fileAst := file.AST
Expand Down
11 changes: 3 additions & 8 deletions rule/defer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,17 @@ import (
// DeferRule lints unused params in functions.
type DeferRule struct {
allow map[string]bool
sync.Mutex

configureOnce sync.Once
}

func (r *DeferRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.allow != nil {
return // already configured
}

r.allow = r.allowFromArgs(arguments)
}

// Apply applies the rule to given file.
func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
r.configureOnce.Do(func() { r.configure(arguments) })

var failures []lint.Failure
onFailure := func(failure lint.Failure) {
Expand Down
12 changes: 3 additions & 9 deletions rule/dot_imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (

// DotImportsRule lints given else constructs.
type DotImportsRule struct {
sync.Mutex
allowedPackages allowPackages

configureOnce sync.Once
}

// Apply applies the rule to given file.
func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
r.configureOnce.Do(func() { r.configure(arguments) })

var failures []lint.Failure

Expand All @@ -41,13 +42,6 @@ func (*DotImportsRule) Name() string {
}

func (r *DotImportsRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()

if r.allowedPackages != nil {
return
}

r.allowedPackages = make(allowPackages)
if len(arguments) == 0 {
return
Expand Down
14 changes: 3 additions & 11 deletions rule/enforce_map_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,12 @@ func mapStyleFromString(s string) (enforceMapStyleType, error) {

// EnforceMapStyleRule implements a rule to enforce `make(map[type]type)` over `map[type]type{}`.
type EnforceMapStyleRule struct {
configured bool
enforceMapStyle enforceMapStyleType
sync.Mutex

configureOnce sync.Once
}

func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()

if r.configured {
return
}
r.configured = true

if len(arguments) < 1 {
r.enforceMapStyle = enforceMapStyleTypeAny
return
Expand All @@ -72,7 +64,7 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) {

// Apply applies the rule to given file.
func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
r.configureOnce.Do(func() { r.configure(arguments) })

if r.enforceMapStyle == enforceMapStyleTypeAny {
// this linter is not configured
Expand Down
13 changes: 2 additions & 11 deletions rule/enforce_repeated_arg_type_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,13 @@ func repeatedArgTypeStyleFromString(s string) enforceRepeatedArgTypeStyleType {

// EnforceRepeatedArgTypeStyleRule implements a rule to enforce repeated argument type style.
type EnforceRepeatedArgTypeStyleRule struct {
configured bool
funcArgStyle enforceRepeatedArgTypeStyleType
funcRetValStyle enforceRepeatedArgTypeStyleType

sync.Mutex
configureOnce sync.Once
}

func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()

if r.configured {
return
}
r.configured = true

r.funcArgStyle = enforceRepeatedArgTypeStyleTypeAny
r.funcRetValStyle = enforceRepeatedArgTypeStyleTypeAny

Expand Down Expand Up @@ -94,7 +85,7 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) {

// Apply applies the rule to a given file.
func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
r.configureOnce.Do(func() { r.configure(arguments) })

if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny {
// This linter is not configured, return no failures.
Expand Down
14 changes: 3 additions & 11 deletions rule/enforce_slice_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,12 @@ func sliceStyleFromString(s string) (enforceSliceStyleType, error) {

// EnforceSliceStyleRule implements a rule to enforce `make([]type)` over `[]type{}`.
type EnforceSliceStyleRule struct {
configured bool
enforceSliceStyle enforceSliceStyleType
sync.Mutex

configureOnce sync.Once
}

func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()

if r.configured {
return
}
r.configured = true

if len(arguments) < 1 {
r.enforceSliceStyle = enforceSliceStyleTypeAny
return
Expand All @@ -76,7 +68,7 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) {

// Apply applies the rule to given file.
func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
r.configureOnce.Do(func() { r.configure(arguments) })

if r.enforceSliceStyle == enforceSliceStyleTypeAny {
// this linter is not configured
Expand Down
Loading

0 comments on commit 3378f70

Please sign in to comment.