@@ -4,27 +4,55 @@ import (
4
4
"go/ast"
5
5
"go/types"
6
6
7
+ "github.com/golangci/plugin-module-register/register"
7
8
"golang.org/x/tools/go/analysis"
8
9
"golang.org/x/tools/go/analysis/passes/inspect"
9
10
"golang.org/x/tools/go/ast/inspector"
10
11
)
11
12
12
- var checkPrivateReturnValues bool
13
+ func init () {
14
+ register .Plugin ("goworkflows" , New )
15
+ }
13
16
14
- func New () * analysis. Analyzer {
15
- a := & analysis. Analyzer {
16
- Name : "goworkflows" ,
17
- Doc : "Checks for common errors when writing workflows" ,
18
- Run : run ,
19
- Requires : [] * analysis. Analyzer { inspect . Analyzer },
17
+ func New (settings any ) (register. LinterPlugin , error ) {
18
+ // The configuration type will be map[string]any or []interface, it depends on your configuration.
19
+ // You can use https://github.com/go-viper/mapstructure to convert map to struct.
20
+ s , err := register. DecodeSettings [ Settings ]( settings )
21
+ if err != nil {
22
+ return nil , err
20
23
}
21
24
22
- a .Flags .BoolVar (& checkPrivateReturnValues , "checkprivatereturnvalues" , false , "Check return values of workflows which aren't exported" )
25
+ return & GoWorkflowsPlugin {Settings : s }, nil
26
+ }
27
+
28
+ type GoWorkflowsPlugin struct {
29
+ Settings Settings
30
+ }
31
+
32
+ type Settings struct {
33
+ CheckPrivateReturnValues bool `json:"checkprivatereturnvalues"`
34
+ }
35
+
36
+ func (w * GoWorkflowsPlugin ) BuildAnalyzers () ([]* analysis.Analyzer , error ) {
37
+ return []* analysis.Analyzer {
38
+ {
39
+ Name : "goworkflows" ,
40
+ Doc : "Checks for common errors when writing workflows" ,
41
+ Run : w .run ,
42
+ Requires : []* analysis.Analyzer {inspect .Analyzer },
43
+ },
44
+ }, nil
45
+ }
46
+
47
+ func (w * GoWorkflowsPlugin ) GetLoadMode () string {
48
+ // NOTE: the mode can be `register.LoadModeSyntax` or `register.LoadModeTypesInfo`.
49
+ // - `register.LoadModeSyntax`: if the linter doesn't use types information.
50
+ // - `register.LoadModeTypesInfo`: if the linter uses types information.
23
51
24
- return a
52
+ return register . LoadModeSyntax
25
53
}
26
54
27
- func run (pass * analysis.Pass ) (interface {}, error ) {
55
+ func ( w * GoWorkflowsPlugin ) run (pass * analysis.Pass ) (interface {}, error ) {
28
56
inspector := pass .ResultOf [inspect .Analyzer ].(* inspector.Inspector )
29
57
30
58
// Expect workflows to be top level functions in a file. Therefore it should be enough to just keep track if the current
@@ -84,7 +112,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
84
112
inWorkflow = true
85
113
86
114
// Check return types
87
- if n .Name .IsExported () || checkPrivateReturnValues {
115
+ if n .Name .IsExported () || w . Settings . CheckPrivateReturnValues {
88
116
if n .Type .Results == nil || len (n .Type .Results .List ) == 0 {
89
117
pass .Reportf (n .Pos (), "workflow `%v` doesn't return anything. needs to return at least `error`" , n .Name .Name )
90
118
} else {
0 commit comments