Skip to content

Commit

Permalink
Set 0 to disable the rule
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Oct 26, 2024
1 parent c49c323 commit d92f14d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1,014 deletions.
6 changes: 3 additions & 3 deletions RULES_DESCRIPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,9 @@ _Description_: This rule enforces a maximum number of lines per file, in order t

_Configuration_:

* `max` (int) a maximum number of lines in a file (default `1000`)
* `skipComments` (bool) if true ignore and do not count lines containing just comments (default `false`)
* `skipBlankLines` (bool) if true ignore and do not count lines made up purely of whitespace (default `false`)
* `max` (int) a maximum number of lines in a file. Must be non-negative integers. 0 means the rule is disabled (default `0`);
* `skipComments` (bool) if true ignore and do not count lines containing just comments (default `false`);
* `skipBlankLines` (bool) if true ignore and do not count lines made up purely of whitespace (default `false`).

Example:

Expand Down
27 changes: 18 additions & 9 deletions rule/file-length-limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
"github.com/mgechev/revive/lint"
)

const defaultFileLengthLimitMax = 1000

// FileLengthLimitRule lints the number of lines in a file.
type FileLengthLimitRule struct {
max int
skipComments bool
// max is the maximum number of lines allowed in a file. 0 means the rule is disabled.
max int
// skipComments indicates whether to skip comment lines when counting lines.
skipComments bool
// skipBlankLines indicates whether to skip blank lines when counting lines.
skipBlankLines bool
sync.Mutex
}
Expand All @@ -26,6 +27,11 @@ type FileLengthLimitRule struct {
func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)

if r.max <= 0 {
// when max is negative or 0 the rule is disabled
return nil
}

all := 0
blank := 0
scanner := bufio.NewScanner(bytes.NewReader(file.Content()))
Expand Down Expand Up @@ -72,9 +78,12 @@ func (r *FileLengthLimitRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()

if len(arguments) == 0 {
r.max = defaultFileLengthLimitMax
return
if r.max != 0 {
return // already configured
}

if len(arguments) < 1 {
return // use default
}

argKV, ok := arguments[0].(map[string]any)
Expand All @@ -85,8 +94,8 @@ func (r *FileLengthLimitRule) configure(arguments lint.Arguments) {
switch k {
case "max":
maxLines, ok := v.(int64)
if !ok || maxLines < 1 {
panic(fmt.Sprintf(`invalid configuration value for max lines in "file-length-limit" rule; need int64 but got %T`, arguments[0]))
if !ok || maxLines < 0 {
panic(fmt.Sprintf(`invalid configuration value for max lines in "file-length-limit" rule; need positive int64 but got %T`, arguments[0]))
}
r.max = int(maxLines)
case "skipComments":
Expand Down
8 changes: 7 additions & 1 deletion test/file-length-limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import (
)

func TestFileLengthLimit(t *testing.T) {
testRule(t, "file-length-limit-default", &rule.FileLengthLimitRule{}, &lint.RuleConfig{
testRule(t, "file-length-limit-disabled", &rule.FileLengthLimitRule{}, &lint.RuleConfig{
Arguments: []any{},
})
testRule(t, "file-length-limit-disabled", &rule.FileLengthLimitRule{}, &lint.RuleConfig{
Arguments: []any{map[string]any{"max": int64(0)}},
})
testRule(t, "file-length-limit-disabled", &rule.FileLengthLimitRule{}, &lint.RuleConfig{
Arguments: []any{map[string]any{"skipComments": true, "skipBlankLines": true}},
})
testRule(t, "file-length-limit-9", &rule.FileLengthLimitRule{}, &lint.RuleConfig{
Arguments: []any{map[string]any{"max": int64(9)}},
})
Expand Down
Loading

0 comments on commit d92f14d

Please sign in to comment.