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

adds default config to enable all rules work out of the box #830

Merged
merged 2 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/argument-limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ type ArgumentsLimitRule struct {
sync.Mutex
}

const defaultArgumentsLimit = 8

func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.total == 0 {
checkNumberOfArguments(1, arguments, r.Name())
if len(arguments) < 1 {
r.total = defaultArgumentsLimit
return
}

total, ok := arguments[0].(int64) // Alt. non panicking version
if !ok {
panic(`invalid value passed as argument number to the "argument-limit" rule`)
}
r.total = int(total)
}
r.Unlock()
}

// Apply applies the rule to given file.
Expand Down
4 changes: 2 additions & 2 deletions rule/banned-characters.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const bannedCharsRuleName = "banned-characters"

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

// Apply applied the rule to the given file.
Expand Down
10 changes: 8 additions & 2 deletions rule/cognitive-complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ type CognitiveComplexityRule struct {
sync.Mutex
}

const defaultMaxCognitiveComplexity = 7

func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.maxComplexity == 0 {
checkNumberOfArguments(1, arguments, r.Name())

if len(arguments) < 1 {
r.maxComplexity = defaultMaxCognitiveComplexity
return
}

complexity, ok := arguments[0].(int64)
if !ok {
panic(fmt.Sprintf("invalid argument type for cognitive-complexity, expected int64, got %T", arguments[0]))
}
r.maxComplexity = int(complexity)
}
r.Unlock()
}

// Apply applies the rule to given file.
Expand Down
9 changes: 7 additions & 2 deletions rule/cyclomatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ type CyclomaticRule struct {
sync.Mutex
}

const defaultMaxCyclomaticComplexity = 10

func (r *CyclomaticRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.maxComplexity == 0 {
checkNumberOfArguments(1, arguments, r.Name())
if len(arguments) < 1 {
r.maxComplexity = defaultMaxCyclomaticComplexity
return
}

complexity, ok := arguments[0].(int64) // Alt. non panicking version
if !ok {
panic(fmt.Sprintf("invalid argument for cyclomatic complexity; expected int but got %T", arguments[0]))
}
r.maxComplexity = int(complexity)
}
r.Unlock()
}

// Apply applies the rule to given file.
Expand Down
13 changes: 10 additions & 3 deletions rule/file-header.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,28 @@ var (

func (r *FileHeaderRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.header == "" {
checkNumberOfArguments(1, arguments, r.Name())
if len(arguments) < 1 {
return
}

var ok bool
r.header, ok = arguments[0].(string)
if !ok {
panic(fmt.Sprintf("invalid argument for \"file-header\" rule: first argument should be a string, got %T", arguments[0]))
panic(fmt.Sprintf("invalid argument for \"file-header\" rule: argument should be a string, got %T", arguments[0]))
}
}
r.Unlock()
}

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

if r.header == "" {
return nil
}

failure := []lint.Failure{
{
Node: file.AST,
Expand Down
9 changes: 8 additions & 1 deletion rule/function-length.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ type FunctionLength struct {

func (r *FunctionLength) configure(arguments lint.Arguments) {
r.Lock()
r.Unlock()
if !r.configured {
maxStmt, maxLines := r.parseArguments(arguments)
r.maxStmt = int(maxStmt)
r.maxLines = int(maxLines)
r.configured = true
}
r.Unlock()
}

// Apply applies the rule to given file.
Expand Down Expand Up @@ -53,7 +53,14 @@ func (*FunctionLength) Name() string {
return "function-length"
}

const defaultFuncStmtsLimit = 50
const defaultFuncLinesLimit = 75

func (*FunctionLength) parseArguments(arguments lint.Arguments) (maxStmt, maxLines int64) {
if len(arguments) == 0 {
return defaultFuncStmtsLimit, defaultFuncLinesLimit
}

if len(arguments) != 2 {
panic(fmt.Sprintf(`invalid configuration for "function-length" rule, expected 2 arguments but got %d`, len(arguments)))
}
Expand Down
10 changes: 7 additions & 3 deletions rule/function-result-limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ type FunctionResultsLimitRule struct {
sync.Mutex
}

const defaultResultsLimit = 3

func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.max == 0 {
checkNumberOfArguments(1, arguments, r.Name())

if len(arguments) < 1 {
r.max = defaultResultsLimit
return
}
max, ok := arguments[0].(int64) // Alt. non panicking version
if !ok {
panic(fmt.Sprintf(`invalid value passed as return results number to the "function-result-limit" rule; need int64 but got %T`, arguments[0]))
Expand All @@ -28,7 +33,6 @@ func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) {
}
r.max = int(max)
}
r.Unlock()
}

// Apply applies the rule to given file.
Expand Down
9 changes: 7 additions & 2 deletions rule/line-length-limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ type LineLengthLimitRule struct {
sync.Mutex
}

const defaultLineLengthLimit = 80

func (r *LineLengthLimitRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.max == 0 {
checkNumberOfArguments(1, arguments, r.Name())
if len(arguments) < 1 {
r.max = defaultLineLengthLimit
return
}

max, ok := arguments[0].(int64) // Alt. non panicking version
if !ok || max < 0 {
Expand All @@ -30,7 +36,6 @@ func (r *LineLengthLimitRule) configure(arguments lint.Arguments) {

r.max = int(max)
}
r.Unlock()
}

// Apply applies the rule to given file.
Expand Down
9 changes: 8 additions & 1 deletion rule/max-public-structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ type MaxPublicStructsRule struct {
sync.Mutex
}

const defaultMaxPublicStructs = 5

func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.max < 1 {
if len(arguments) < 1 {
r.max = defaultMaxPublicStructs
return
}

checkNumberOfArguments(1, arguments, r.Name())

max, ok := arguments[0].(int64) // Alt. non panicking version
Expand All @@ -25,7 +33,6 @@ func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) {
}
r.max = max
}
r.Unlock()
}

// Apply applies the rule to given file.
Expand Down