|
1 | 1 | package funlen
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "go/token" |
5 |
| - "strings" |
6 |
| - "sync" |
7 |
| - |
8 | 4 | "github.com/ultraware/funlen"
|
9 | 5 | "golang.org/x/tools/go/analysis"
|
10 | 6 |
|
11 | 7 | "github.com/golangci/golangci-lint/pkg/config"
|
12 | 8 | "github.com/golangci/golangci-lint/pkg/goanalysis"
|
13 |
| - "github.com/golangci/golangci-lint/pkg/lint/linter" |
14 |
| - "github.com/golangci/golangci-lint/pkg/result" |
15 | 9 | )
|
16 | 10 |
|
17 |
| -const linterName = "funlen" |
| 11 | +type Config struct { |
| 12 | + lineLimit int |
| 13 | + stmtLimit int |
| 14 | + ignoreComments bool |
| 15 | +} |
18 | 16 |
|
19 | 17 | func New(settings *config.FunlenSettings) *goanalysis.Linter {
|
20 |
| - var mu sync.Mutex |
21 |
| - var resIssues []goanalysis.Issue |
22 |
| - |
23 |
| - analyzer := &analysis.Analyzer{ |
24 |
| - Name: linterName, |
25 |
| - Doc: goanalysis.TheOnlyanalyzerDoc, |
26 |
| - Run: func(pass *analysis.Pass) (any, error) { |
27 |
| - issues := runFunlen(pass, settings) |
28 |
| - |
29 |
| - if len(issues) == 0 { |
30 |
| - return nil, nil |
31 |
| - } |
32 |
| - |
33 |
| - mu.Lock() |
34 |
| - resIssues = append(resIssues, issues...) |
35 |
| - mu.Unlock() |
36 |
| - |
37 |
| - return nil, nil |
38 |
| - }, |
| 18 | + cfg := Config{} |
| 19 | + if settings != nil { |
| 20 | + cfg.lineLimit = settings.Lines |
| 21 | + cfg.stmtLimit = settings.Statements |
| 22 | + cfg.ignoreComments = !settings.IgnoreComments |
39 | 23 | }
|
40 | 24 |
|
| 25 | + a := funlen.NewAnalyzer(cfg.lineLimit, cfg.stmtLimit, cfg.ignoreComments) |
| 26 | + |
41 | 27 | return goanalysis.NewLinter(
|
42 |
| - linterName, |
43 |
| - "Tool for detection of long functions", |
44 |
| - []*analysis.Analyzer{analyzer}, |
| 28 | + a.Name, |
| 29 | + a.Doc, |
| 30 | + []*analysis.Analyzer{a}, |
45 | 31 | nil,
|
46 |
| - ).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { |
47 |
| - return resIssues |
48 |
| - }).WithLoadMode(goanalysis.LoadModeSyntax) |
49 |
| -} |
50 |
| - |
51 |
| -func runFunlen(pass *analysis.Pass, settings *config.FunlenSettings) []goanalysis.Issue { |
52 |
| - var lintIssues []funlen.Message |
53 |
| - for _, file := range pass.Files { |
54 |
| - fileIssues := funlen.Run(file, pass.Fset, settings.Lines, settings.Statements, settings.IgnoreComments) |
55 |
| - lintIssues = append(lintIssues, fileIssues...) |
56 |
| - } |
57 |
| - |
58 |
| - if len(lintIssues) == 0 { |
59 |
| - return nil |
60 |
| - } |
61 |
| - |
62 |
| - issues := make([]goanalysis.Issue, len(lintIssues)) |
63 |
| - for k, i := range lintIssues { |
64 |
| - issues[k] = goanalysis.NewIssue(&result.Issue{ |
65 |
| - Pos: token.Position{ |
66 |
| - Filename: i.Pos.Filename, |
67 |
| - Line: i.Pos.Line, |
68 |
| - }, |
69 |
| - Text: strings.TrimRight(i.Message, "\n"), |
70 |
| - FromLinter: linterName, |
71 |
| - }, pass) |
72 |
| - } |
73 |
| - |
74 |
| - return issues |
| 32 | + ).WithLoadMode(goanalysis.LoadModeSyntax) |
75 | 33 | }
|
0 commit comments