forked from mgechev/revive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule: line-length-limit (mgechev#86)
* New rule: line-length-limit * simplify the panic message of rule line-length-limit * Refactors `line-length-limit` rule, use private `check` method instead of `Visit`
- Loading branch information
Showing
6 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package fixtures | ||
|
||
import "fmt" | ||
|
||
func foo(a, b int) { | ||
fmt.Printf("single line characters out of limit") // MATCH /line is 105 characters, out of limit 100/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package rule | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"go/token" | ||
"strings" | ||
"unicode/utf8" | ||
|
||
"github.com/mgechev/revive/lint" | ||
) | ||
|
||
// LineLengthLimitRule lints given else constructs. | ||
type LineLengthLimitRule struct{} | ||
|
||
// Apply applies the rule to given file. | ||
func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { | ||
if len(arguments) != 1 { | ||
panic(`invalid configuration for "line-length-limit"`) | ||
} | ||
|
||
max, ok := arguments[0].(int64) // Alt. non panicking version | ||
if !ok || max < 0 { | ||
panic(`invalid value passed as argument number to the "line-length-limit" rule`) | ||
} | ||
|
||
var failures []lint.Failure | ||
checker := lintLineLengthNum{ | ||
max: int(max), | ||
file: file, | ||
onFailure: func(failure lint.Failure) { | ||
failures = append(failures, failure) | ||
}, | ||
} | ||
|
||
checker.check() | ||
|
||
return failures | ||
} | ||
|
||
// Name returns the rule name. | ||
func (r *LineLengthLimitRule) Name() string { | ||
return "line-length-limit" | ||
} | ||
|
||
type lintLineLengthNum struct { | ||
max int | ||
file *lint.File | ||
onFailure func(lint.Failure) | ||
} | ||
|
||
func (r lintLineLengthNum) check() { | ||
f := bytes.NewReader(r.file.Content()) | ||
spaces := strings.Repeat(" ", 4) // tab width = 4 | ||
l := 1 | ||
s := bufio.NewScanner(f) | ||
for s.Scan() { | ||
t := s.Text() | ||
t = strings.Replace(t, "\t", spaces, -1) | ||
c := utf8.RuneCountInString(t) | ||
if c > r.max { | ||
r.onFailure(lint.Failure{ | ||
Category: "code-style", | ||
Position: lint.FailurePosition{ | ||
// Offset not set; it is non-trivial, and doesn't appear to be needed. | ||
Start: token.Position{ | ||
Filename: r.file.Name, | ||
Line: l, | ||
Column: 0, | ||
}, | ||
End: token.Position{ | ||
Filename: r.file.Name, | ||
Line: l, | ||
Column: c, | ||
}, | ||
}, | ||
Confidence: 1, | ||
Failure: fmt.Sprintf("line is %d characters, out of limit %d", c, r.max), | ||
URL: "", | ||
}) | ||
} | ||
l++ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mgechev/revive/lint" | ||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
func TestLineLengthLimit(t *testing.T) { | ||
testRule(t, "line-length-limit", &rule.LineLengthLimitRule{}, &lint.RuleConfig{ | ||
Arguments: []interface{}{int64(100)}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters