Skip to content

Commit

Permalink
fix: fixes #1103 by fixing the processing of max-public-structs argum…
Browse files Browse the repository at this point in the history
…ents (#1114)
  • Loading branch information
chavacava authored Nov 15, 2024
1 parent cc3ad5f commit 7ee4500
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
9 changes: 7 additions & 2 deletions rule/max_public_structs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rule

import (
"fmt"
"go/ast"
"strings"
"sync"
Expand All @@ -19,7 +20,7 @@ const defaultMaxPublicStructs = 5
func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.max == 0 {
if r.max != 0 {
return // already configured
}

Expand All @@ -43,6 +44,10 @@ func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments)

var failures []lint.Failure

if r.max < 1 {
return failures
}

fileAst := file.AST

walker := &lintMaxPublicStructs{
Expand All @@ -56,7 +61,7 @@ func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments)

if walker.current > r.max {
walker.onFailure(lint.Failure{
Failure: "you have exceeded the maximum number of public struct declarations",
Failure: fmt.Sprintf("you have exceeded the maximum number (%d) of public struct declarations", r.max),
Confidence: 1,
Node: fileAst,
Category: "style",
Expand Down
4 changes: 4 additions & 0 deletions test/max_public_structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ func TestMaxPublicStructs(t *testing.T) {
Arguments: []any{int64(1)},
})
}

func TestMaxPublicStructsDefaultConfig(t *testing.T) {
testRule(t, "max_public_structs_ok", &rule.MaxPublicStructsRule{}, &lint.RuleConfig{})
}
2 changes: 1 addition & 1 deletion testdata/max_public_structs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package pkg ...
package pkg // MATCH /you have exceeded the maximum number of public struct declarations/
package pkg // MATCH /you have exceeded the maximum number (1) of public struct declarations/

type Foo struct {
}
Expand Down
11 changes: 11 additions & 0 deletions testdata/max_public_structs_ok.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Package pkg ...
package pkg

type Foo struct {
}

type Bar struct {
}

type Baz struct {
}

0 comments on commit 7ee4500

Please sign in to comment.