Skip to content

Commit 600e10e

Browse files
committed
chore: migrate nestif
1 parent 7d46339 commit 600e10e

File tree

1 file changed

+19
-41
lines changed

1 file changed

+19
-41
lines changed

pkg/golinters/nestif/nestif.go

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,21 @@
11
package nestif
22

33
import (
4-
"sort"
5-
"sync"
6-
74
"github.com/nakabonne/nestif"
85
"golang.org/x/tools/go/analysis"
96

107
"github.com/golangci/golangci-lint/pkg/config"
118
"github.com/golangci/golangci-lint/pkg/goanalysis"
12-
"github.com/golangci/golangci-lint/pkg/lint/linter"
13-
"github.com/golangci/golangci-lint/pkg/result"
149
)
1510

1611
const linterName = "nestif"
1712

1813
func New(settings *config.NestifSettings) *goanalysis.Linter {
19-
var mu sync.Mutex
20-
var resIssues []goanalysis.Issue
21-
2214
analyzer := &analysis.Analyzer{
23-
Name: goanalysis.TheOnlyAnalyzerName,
15+
Name: linterName,
2416
Doc: goanalysis.TheOnlyanalyzerDoc,
2517
Run: func(pass *analysis.Pass) (any, error) {
26-
issues := runNestIf(pass, settings)
27-
28-
if len(issues) == 0 {
29-
return nil, nil
30-
}
31-
32-
mu.Lock()
33-
resIssues = append(resIssues, issues...)
34-
mu.Unlock()
18+
runNestIf(pass, settings)
3519

3620
return nil, nil
3721
},
@@ -42,37 +26,31 @@ func New(settings *config.NestifSettings) *goanalysis.Linter {
4226
"Reports deeply nested if statements",
4327
[]*analysis.Analyzer{analyzer},
4428
nil,
45-
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
46-
return resIssues
47-
}).WithLoadMode(goanalysis.LoadModeSyntax)
29+
).WithLoadMode(goanalysis.LoadModeSyntax)
4830
}
4931

50-
func runNestIf(pass *analysis.Pass, settings *config.NestifSettings) []goanalysis.Issue {
32+
func runNestIf(pass *analysis.Pass, settings *config.NestifSettings) {
5133
checker := &nestif.Checker{
5234
MinComplexity: settings.MinComplexity,
5335
}
5436

55-
var lintIssues []nestif.Issue
56-
for _, f := range pass.Files {
57-
lintIssues = append(lintIssues, checker.Check(f, pass.Fset)...)
58-
}
37+
for _, file := range pass.Files {
38+
position := goanalysis.GetFilePosition(pass, file)
5939

60-
if len(lintIssues) == 0 {
61-
return nil
62-
}
40+
issues := checker.Check(file, pass.Fset)
41+
if len(issues) == 0 {
42+
continue
43+
}
6344

64-
sort.SliceStable(lintIssues, func(i, j int) bool {
65-
return lintIssues[i].Complexity > lintIssues[j].Complexity
66-
})
45+
nonAdjPosition := pass.Fset.PositionFor(file.Pos(), false)
6746

68-
issues := make([]goanalysis.Issue, 0, len(lintIssues))
69-
for _, i := range lintIssues {
70-
issues = append(issues, goanalysis.NewIssue(&result.Issue{
71-
Pos: i.Pos,
72-
Text: i.Message,
73-
FromLinter: linterName,
74-
}, pass))
75-
}
47+
f := pass.Fset.File(file.Pos())
7648

77-
return issues
49+
for _, issue := range issues {
50+
pass.Report(analysis.Diagnostic{
51+
Pos: f.LineStart(goanalysis.AdjustPos(issue.Pos.Line, nonAdjPosition.Line, position.Line)),
52+
Message: issue.Message,
53+
})
54+
}
55+
}
7856
}

0 commit comments

Comments
 (0)