@@ -2,40 +2,27 @@ package forbidigo
2
2
3
3
import (
4
4
"fmt"
5
- "sync"
6
5
7
6
"github.com/ashanbrown/forbidigo/forbidigo"
8
7
"golang.org/x/tools/go/analysis"
9
8
10
9
"github.com/golangci/golangci-lint/pkg/config"
11
10
"github.com/golangci/golangci-lint/pkg/goanalysis"
12
- "github.com/golangci/golangci-lint/pkg/lint/linter"
13
11
"github.com/golangci/golangci-lint/pkg/logutils"
14
- "github.com/golangci/golangci-lint/pkg/result"
15
12
)
16
13
17
14
const linterName = "forbidigo"
18
15
19
16
func New (settings * config.ForbidigoSettings ) * goanalysis.Linter {
20
- var mu sync.Mutex
21
- var resIssues []goanalysis.Issue
22
-
23
17
analyzer := & analysis.Analyzer {
24
18
Name : linterName ,
25
19
Doc : goanalysis .TheOnlyanalyzerDoc ,
26
20
Run : func (pass * analysis.Pass ) (any , error ) {
27
- issues , err := runForbidigo (pass , settings )
21
+ err := runForbidigo (pass , settings )
28
22
if err != nil {
29
23
return nil , err
30
24
}
31
25
32
- if len (issues ) == 0 {
33
- return nil , nil
34
- }
35
-
36
- mu .Lock ()
37
- resIssues = append (resIssues , issues ... )
38
- mu .Unlock ()
39
26
return nil , nil
40
27
},
41
28
}
@@ -48,12 +35,10 @@ func New(settings *config.ForbidigoSettings) *goanalysis.Linter {
48
35
"Forbids identifiers" ,
49
36
[]* analysis.Analyzer {analyzer },
50
37
nil ,
51
- ).WithIssuesReporter (func (* linter.Context ) []goanalysis.Issue {
52
- return resIssues
53
- }).WithLoadMode (goanalysis .LoadModeTypesInfo )
38
+ ).WithLoadMode (goanalysis .LoadModeTypesInfo )
54
39
}
55
40
56
- func runForbidigo (pass * analysis.Pass , settings * config.ForbidigoSettings ) ([]goanalysis. Issue , error ) {
41
+ func runForbidigo (pass * analysis.Pass , settings * config.ForbidigoSettings ) error {
57
42
options := []forbidigo.Option {
58
43
forbidigo .OptionExcludeGodocExamples (settings .ExcludeGodocExamples ),
59
44
// disable "//permit" directives so only "//nolint" directives matters within golangci-lint
@@ -66,38 +51,42 @@ func runForbidigo(pass *analysis.Pass, settings *config.ForbidigoSettings) ([]go
66
51
for _ , pattern := range settings .Forbid {
67
52
buffer , err := pattern .MarshalString ()
68
53
if err != nil {
69
- return nil , err
54
+ return err
70
55
}
56
+
71
57
patterns = append (patterns , string (buffer ))
72
58
}
73
59
74
60
forbid , err := forbidigo .NewLinter (patterns , options ... )
75
61
if err != nil {
76
- return nil , fmt .Errorf ("failed to create linter %q: %w" , linterName , err )
62
+ return fmt .Errorf ("failed to create linter %q: %w" , linterName , err )
77
63
}
78
64
79
- var issues []goanalysis.Issue
80
65
for _ , file := range pass .Files {
81
66
runConfig := forbidigo.RunConfig {
82
67
Fset : pass .Fset ,
83
68
DebugLog : logutils .Debug (logutils .DebugKeyForbidigo ),
84
69
}
85
- if settings != nil && settings .AnalyzeTypes {
70
+
71
+ if settings .AnalyzeTypes {
86
72
runConfig .TypesInfo = pass .TypesInfo
87
73
}
74
+
88
75
hints , err := forbid .RunWithConfig (runConfig , file )
89
76
if err != nil {
90
- return nil , fmt .Errorf ("forbidigo linter failed on file %q: %w" , file .Name .String (), err )
77
+ return fmt .Errorf ("forbidigo linter failed on file %q: %w" , file .Name .String (), err )
91
78
}
92
79
93
80
for _ , hint := range hints {
94
- issues = append (issues , goanalysis .NewIssue (& result.Issue {
95
- Pos : hint .Position (),
96
- Text : hint .Details (),
97
- FromLinter : linterName ,
98
- }, pass ))
81
+ pass .Report (analysis.Diagnostic {
82
+ Pos : hint .Pos (),
83
+ Message : hint .Details (),
84
+ URL : "" ,
85
+ SuggestedFixes : nil ,
86
+ Related : nil ,
87
+ })
99
88
}
100
89
}
101
90
102
- return issues , nil
91
+ return nil
103
92
}
0 commit comments