1
1
package whitespace
2
2
3
3
import (
4
- "fmt"
5
- "sync"
6
-
7
4
"github.com/ultraware/whitespace"
8
5
"golang.org/x/tools/go/analysis"
9
6
10
7
"github.com/golangci/golangci-lint/pkg/config"
11
8
"github.com/golangci/golangci-lint/pkg/goanalysis"
12
- "github.com/golangci/golangci-lint/pkg/lint/linter"
13
- "github.com/golangci/golangci-lint/pkg/result"
14
9
)
15
10
16
- const linterName = "whitespace"
17
-
18
11
func New (settings * config.WhitespaceSettings ) * goanalysis.Linter {
19
- var mu sync.Mutex
20
- var resIssues []goanalysis.Issue
21
-
22
12
var wsSettings whitespace.Settings
23
13
if settings != nil {
24
14
wsSettings = whitespace.Settings {
25
- Mode : whitespace .RunningModeGolangCI ,
15
+ Mode : whitespace .RunningModeNative ,
26
16
MultiIf : settings .MultiIf ,
27
17
MultiFunc : settings .MultiFunc ,
28
18
}
@@ -35,68 +25,5 @@ func New(settings *config.WhitespaceSettings) *goanalysis.Linter {
35
25
a .Doc ,
36
26
[]* analysis.Analyzer {a },
37
27
nil ,
38
- ).WithContextSetter (func (_ * linter.Context ) {
39
- a .Run = func (pass * analysis.Pass ) (any , error ) {
40
- issues , err := runWhitespace (pass , wsSettings )
41
- if err != nil {
42
- return nil , err
43
- }
44
-
45
- if len (issues ) == 0 {
46
- return nil , nil
47
- }
48
-
49
- mu .Lock ()
50
- resIssues = append (resIssues , issues ... )
51
- mu .Unlock ()
52
-
53
- return nil , nil
54
- }
55
- }).WithIssuesReporter (func (* linter.Context ) []goanalysis.Issue {
56
- return resIssues
57
- }).WithLoadMode (goanalysis .LoadModeSyntax )
58
- }
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 : linterName ,
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
28
+ ).WithLoadMode (goanalysis .LoadModeSyntax )
102
29
}
0 commit comments