Skip to content

Commit 568deb5

Browse files
committed
Set 0 to disable the rule
1 parent c49c323 commit 568deb5

File tree

5 files changed

+33
-1014
lines changed

5 files changed

+33
-1014
lines changed

RULES_DESCRIPTIONS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ _Description_: This rule enforces a maximum number of lines per file, in order t
508508

509509
_Configuration_:
510510

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

515515
Example:
516516

rule/file-length-limit.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
"github.com/mgechev/revive/lint"
1313
)
1414

15-
const defaultFileLengthLimitMax = 1000
16-
1715
// FileLengthLimitRule lints the number of lines in a file.
1816
type FileLengthLimitRule struct {
19-
max int
20-
skipComments bool
17+
// max is the maximum number of lines allowed in a file. 0 means the rule is disabled.
18+
max int
19+
// skipComments indicates whether to skip comment lines when counting lines.
20+
skipComments bool
21+
// skipBlankLines indicates whether to skip blank lines when counting lines.
2122
skipBlankLines bool
2223
sync.Mutex
2324
}
@@ -26,6 +27,11 @@ type FileLengthLimitRule struct {
2627
func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
2728
r.configure(arguments)
2829

30+
if r.max <= 0 {
31+
// when max is negative or 0 the rule is disabled
32+
return nil
33+
}
34+
2935
all := 0
3036
blank := 0
3137
scanner := bufio.NewScanner(bytes.NewReader(file.Content()))
@@ -72,9 +78,12 @@ func (r *FileLengthLimitRule) configure(arguments lint.Arguments) {
7278
r.Lock()
7379
defer r.Unlock()
7480

75-
if len(arguments) == 0 {
76-
r.max = defaultFileLengthLimitMax
77-
return
81+
if r.max != 0 {
82+
return // already configured
83+
}
84+
85+
if len(arguments) < 1 {
86+
return // use default
7887
}
7988

8089
argKV, ok := arguments[0].(map[string]any)
@@ -85,8 +94,8 @@ func (r *FileLengthLimitRule) configure(arguments lint.Arguments) {
8594
switch k {
8695
case "max":
8796
maxLines, ok := v.(int64)
88-
if !ok || maxLines < 1 {
89-
panic(fmt.Sprintf(`invalid configuration value for max lines in "file-length-limit" rule; need int64 but got %T`, arguments[0]))
97+
if !ok || maxLines < 0 {
98+
panic(fmt.Sprintf(`invalid configuration value for max lines in "file-length-limit" rule; need positive int64 but got %T`, arguments[0]))
9099
}
91100
r.max = int(maxLines)
92101
case "skipComments":

test/file-length-limit_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import (
88
)
99

1010
func TestFileLengthLimit(t *testing.T) {
11-
testRule(t, "file-length-limit-default", &rule.FileLengthLimitRule{}, &lint.RuleConfig{
11+
testRule(t, "file-length-limit-disabled", &rule.FileLengthLimitRule{}, &lint.RuleConfig{
1212
Arguments: []any{},
1313
})
14+
testRule(t, "file-length-limit-disabled", &rule.FileLengthLimitRule{}, &lint.RuleConfig{
15+
Arguments: []any{map[string]any{"max": int64(0)}},
16+
})
1417
testRule(t, "file-length-limit-9", &rule.FileLengthLimitRule{}, &lint.RuleConfig{
1518
Arguments: []any{map[string]any{"max": int64(9)}},
1619
})

0 commit comments

Comments
 (0)