forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lll.go
73 lines (61 loc) · 1.5 KB
/
lll.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package golinters
import (
"bufio"
"context"
"fmt"
"go/token"
"os"
"strings"
"unicode/utf8"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)
type Lll struct{}
func (Lll) Name() string {
return "lll"
}
func (Lll) Desc() string {
return "Reports long lines"
}
func (lint Lll) getIssuesForFile(filename string, maxLineLen int) ([]result.Issue, error) {
var res []result.Issue
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("can't open file %s: %s", filename, err)
}
defer f.Close()
lineNumber := 1
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
line = strings.Replace(line, "\t", " ", -1)
lineLen := utf8.RuneCountInString(line)
if lineLen > maxLineLen {
res = append(res, result.Issue{
Pos: token.Position{
Filename: filename,
Line: lineNumber,
Column: 1,
},
Text: fmt.Sprintf("line is %d characters", lineLen),
FromLinter: lint.Name(),
})
}
lineNumber++
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("can't scan file %s: %s", filename, err)
}
return res, nil
}
func (lint Lll) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
var res []result.Issue
for _, f := range lintCtx.PkgProgram.Files(lintCtx.Cfg.Run.AnalyzeTests) {
issues, err := lint.getIssuesForFile(f, lintCtx.Settings().Lll.LineLength)
if err != nil {
return nil, err
}
res = append(res, issues...)
}
return res, nil
}