Skip to content

Commit 57794a9

Browse files
ldezbombsimon
authored andcommitted
review: follow the same design as the other linter
1 parent 118bc64 commit 57794a9

File tree

1 file changed

+54
-45
lines changed

1 file changed

+54
-45
lines changed

pkg/golinters/whitespace.go

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/golangci/golangci-lint/pkg/result"
1414
)
1515

16+
const whitespaceName = "whitespace"
17+
1618
func NewWhitespace(settings *config.WhitespaceSettings) *goanalysis.Linter {
1719
var mu sync.Mutex
1820
var resIssues []goanalysis.Issue
@@ -26,55 +28,18 @@ func NewWhitespace(settings *config.WhitespaceSettings) *goanalysis.Linter {
2628
}
2729
}
2830

29-
whitespaceAnalyzer := whitespace.NewAnalyzer(&wsSettings)
31+
a := whitespace.NewAnalyzer(&wsSettings)
3032

3133
return goanalysis.NewLinter(
32-
whitespaceAnalyzer.Name,
33-
whitespaceAnalyzer.Doc,
34-
[]*analysis.Analyzer{whitespaceAnalyzer},
34+
a.Name,
35+
a.Doc,
36+
[]*analysis.Analyzer{a},
3537
nil,
3638
).WithContextSetter(func(lintCtx *linter.Context) {
37-
whitespaceAnalyzer.Run = func(pass *analysis.Pass) (any, error) {
38-
whitespaceIssues := whitespace.Run(pass, &wsSettings)
39-
issues := make([]goanalysis.Issue, len(whitespaceIssues))
40-
41-
for i, issue := range whitespaceIssues {
42-
report := &result.Issue{
43-
FromLinter: whitespaceAnalyzer.Name,
44-
Pos: pass.Fset.PositionFor(issue.Diagnostic, false),
45-
Text: issue.Message,
46-
}
47-
48-
switch issue.MessageType {
49-
case whitespace.MessageTypeRemove:
50-
if len(issue.LineNumbers) == 0 {
51-
continue
52-
}
53-
54-
report.LineRange = &result.Range{
55-
From: issue.LineNumbers[0],
56-
To: issue.LineNumbers[len(issue.LineNumbers)-1],
57-
}
58-
59-
report.Replacement = &result.Replacement{
60-
NeedOnlyDelete: true,
61-
}
62-
63-
case whitespace.MessageTypeAdd:
64-
report.Pos = pass.Fset.PositionFor(issue.FixStart, false)
65-
report.Replacement = &result.Replacement{
66-
Inline: &result.InlineFix{
67-
StartCol: 0,
68-
Length: 1,
69-
NewString: "\n\t",
70-
},
71-
}
72-
73-
default:
74-
return nil, fmt.Errorf("unknown message type: %v", issue.MessageType)
75-
}
76-
77-
issues[i] = goanalysis.NewIssue(report, pass)
39+
a.Run = func(pass *analysis.Pass) (any, error) {
40+
issues, err := runWhitespace(pass, wsSettings)
41+
if err != nil {
42+
return nil, err
7843
}
7944

8045
if len(issues) == 0 {
@@ -91,3 +56,47 @@ func NewWhitespace(settings *config.WhitespaceSettings) *goanalysis.Linter {
9156
return resIssues
9257
}).WithLoadMode(goanalysis.LoadModeSyntax)
9358
}
59+
60+
func runWhitespace(pass *analysis.Pass, wsSettings whitespace.Settings) ([]goanalysis.Issue, error) {
61+
lintIssues := whitespace.Run(pass, &wsSettings)
62+
63+
issues := make([]goanalysis.Issue, len(lintIssues))
64+
for i, issue := range lintIssues {
65+
report := &result.Issue{
66+
FromLinter: whitespaceName,
67+
Pos: pass.Fset.PositionFor(issue.Diagnostic, false),
68+
Text: issue.Message,
69+
}
70+
71+
switch issue.MessageType {
72+
case whitespace.MessageTypeRemove:
73+
if len(issue.LineNumbers) == 0 {
74+
continue
75+
}
76+
77+
report.LineRange = &result.Range{
78+
From: issue.LineNumbers[0],
79+
To: issue.LineNumbers[len(issue.LineNumbers)-1],
80+
}
81+
82+
report.Replacement = &result.Replacement{NeedOnlyDelete: true}
83+
84+
case whitespace.MessageTypeAdd:
85+
report.Pos = pass.Fset.PositionFor(issue.FixStart, false)
86+
report.Replacement = &result.Replacement{
87+
Inline: &result.InlineFix{
88+
StartCol: 0,
89+
Length: 1,
90+
NewString: "\n\t",
91+
},
92+
}
93+
94+
default:
95+
return nil, fmt.Errorf("unknown message type: %v", issue.MessageType)
96+
}
97+
98+
issues[i] = goanalysis.NewIssue(report, pass)
99+
}
100+
101+
return issues, nil
102+
}

0 commit comments

Comments
 (0)