1
1
package analyzer
2
2
3
3
import (
4
- "fmt"
5
4
"go/ast"
6
5
"go/types"
7
6
@@ -20,9 +19,28 @@ var Analyzer = &analysis.Analyzer{
20
19
func run (pass * analysis.Pass ) (interface {}, error ) {
21
20
inspector := pass .ResultOf [inspect .Analyzer ].(* inspector.Inspector )
22
21
22
+ // Expect workflows to be top level functions in a file. Therefore it should be enough to just keep track if the current
23
+ // AST node is a descendant of a workflow FuncDecl.
24
+ inWorkflow := true
25
+
23
26
inspector .Nodes (nil , func (node ast.Node , push bool ) bool {
24
- if ! push {
25
- return false
27
+
28
+ if _ , ok := node .(* ast.FuncDecl ); ok {
29
+ if ! push {
30
+ // Finished with the current workflow
31
+ inWorkflow = false
32
+ return false
33
+ }
34
+ } else {
35
+ if ! push {
36
+ // Only check nodes while descending
37
+ return false
38
+ }
39
+
40
+ if ! inWorkflow {
41
+ // Only check nodes while in a top-level workflow func
42
+ return false
43
+ }
26
44
}
27
45
28
46
switch n := node .(type ) {
@@ -32,6 +50,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
32
50
return false
33
51
}
34
52
53
+ inWorkflow = true
54
+
35
55
// Check return types
36
56
if n .Type .Results == nil || len (n .Type .Results .List ) == 0 {
37
57
pass .Reportf (n .Pos (), "workflow `%v` doesn't return anything. needs to return at least `error`" , n .Name .Name )
@@ -69,8 +89,6 @@ func run(pass *analysis.Pass) (interface{}, error) {
69
89
case * types.Chan :
70
90
pass .Reportf (n .Pos (), "using native channels is not allowed in workflows, use `workflow.Channel` instead" )
71
91
}
72
-
73
- // checkStatements(pass, n.Body.List)
74
92
}
75
93
76
94
case * ast.SelectStmt :
@@ -141,8 +159,6 @@ func checkVarsInScope(pass *analysis.Pass, scope *types.Scope) {
141
159
func checkNamed (pass * analysis.Pass , ref types.Object , named * types.Named ) {
142
160
if obj := named .Obj (); obj != nil {
143
161
if pkg := obj .Pkg (); pkg != nil {
144
- fmt .Println (pkg .Path (), obj .Name (), obj .Id ())
145
-
146
162
switch pkg .Path () {
147
163
case "sync" :
148
164
if obj .Name () == "WaitGroup" {
0 commit comments