Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes #1103 by fixing the processing of max-public-structs arguments #1114

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
chavacava marked this conversation as resolved.
Show resolved Hide resolved
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),
chavacava marked this conversation as resolved.
Show resolved Hide resolved
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 {
}