1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-30 08:57:07 +02:00
revive/rule/line-length-limit.go
chavacava 2a1838f501
adds default config to enable all rules work out of the box (#830)
* adds default config to enable all rules out of the box
2023-05-20 14:44:34 +02:00

103 lines
2.0 KiB
Go

package rule
import (
"bufio"
"bytes"
"fmt"
"go/token"
"strings"
"sync"
"unicode/utf8"
"github.com/mgechev/revive/lint"
)
// LineLengthLimitRule lints given else constructs.
type LineLengthLimitRule struct {
max int
sync.Mutex
}
const defaultLineLengthLimit = 80
func (r *LineLengthLimitRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.max == 0 {
if len(arguments) < 1 {
r.max = defaultLineLengthLimit
return
}
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`)
}
r.max = int(max)
}
}
// Apply applies the rule to given file.
func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
var failures []lint.Failure
checker := lintLineLengthNum{
max: r.max,
file: file,
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
}
checker.check()
return failures
}
// Name returns the rule name.
func (*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.ReplaceAll(t, "\t", spaces)
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),
})
}
l++
}
}