@@ -9,11 +9,19 @@ import (
9
9
"golang.org/x/tools/go/ast/inspector"
10
10
)
11
11
12
- var Analyzer = & analysis.Analyzer {
13
- Name : "goworkflows" ,
14
- Doc : "Checks for common errors when writing workflows" ,
15
- Run : run ,
16
- Requires : []* analysis.Analyzer {inspect .Analyzer },
12
+ var checkPrivateReturnValues bool
13
+
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 },
20
+ }
21
+
22
+ a .Flags .BoolVar (& checkPrivateReturnValues , "checkprivatereturnvalues" , false , "Check return values of workflows which aren't exported" )
23
+
24
+ return a
17
25
}
18
26
19
27
func run (pass * analysis.Pass ) (interface {}, error ) {
@@ -25,7 +33,20 @@ func run(pass *analysis.Pass) (interface{}, error) {
25
33
26
34
workflowImportName := "workflow"
27
35
28
- inspector .Nodes (nil , func (node ast.Node , push bool ) bool {
36
+ inspector .Nodes ([]ast.Node {
37
+ (* ast .File )(nil ),
38
+ (* ast .FuncDecl )(nil ),
39
+ (* ast .ImportSpec )(nil ),
40
+ (* ast .RangeStmt )(nil ),
41
+ (* ast .SelectStmt )(nil ),
42
+ (* ast .GoStmt )(nil ),
43
+ (* ast .CallExpr )(nil ),
44
+ }, func (node ast.Node , push bool ) bool {
45
+ if _ , ok := node .(* ast.File ); ok {
46
+ // New file, reset state
47
+ inWorkflow = true
48
+ }
49
+
29
50
if _ , ok := node .(* ast.FuncDecl ); ok {
30
51
if ! push {
31
52
// Finished with the current workflow
@@ -59,17 +80,19 @@ func run(pass *analysis.Pass) (interface{}, error) {
59
80
inWorkflow = true
60
81
61
82
// Check return types
62
- if n .Type .Results == nil || len (n .Type .Results .List ) == 0 {
63
- pass .Reportf (n .Pos (), "workflow `%v` doesn't return anything. needs to return at least `error`" , n .Name .Name )
64
- } else {
65
- if len (n .Type .Results .List ) > 2 {
66
- pass .Reportf (n .Pos (), "workflow `%v` returns more than two values" , n .Name .Name )
67
- return true
68
- }
69
-
70
- lastResult := n .Type .Results .List [len (n .Type .Results .List )- 1 ]
71
- if types .ExprString (lastResult .Type ) != "error" {
72
- pass .Reportf (n .Pos (), "workflow `%v` doesn't return `error` as last return value" , n .Name .Name )
83
+ if n .Name .IsExported () || checkPrivateReturnValues {
84
+ if n .Type .Results == nil || len (n .Type .Results .List ) == 0 {
85
+ pass .Reportf (n .Pos (), "workflow `%v` doesn't return anything. needs to return at least `error`" , n .Name .Name )
86
+ } else {
87
+ if len (n .Type .Results .List ) > 2 {
88
+ pass .Reportf (n .Pos (), "workflow `%v` returns more than two values" , n .Name .Name )
89
+ return true
90
+ }
91
+
92
+ lastResult := n .Type .Results .List [len (n .Type .Results .List )- 1 ]
93
+ if types .ExprString (lastResult .Type ) != "error" {
94
+ pass .Reportf (n .Pos (), "workflow `%v` doesn't return `error` as last return value" , n .Name .Name )
95
+ }
73
96
}
74
97
}
75
98
0 commit comments