Skip to content

Commit c5defd0

Browse files
authored
Merge pull request #201 from cschleiden/more-reliable-analyzer
Make the analyzer workflow detection more robust
2 parents e0728be + 4af9ccd commit c5defd0

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

analyzer/analyzer.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ func run(pass *analysis.Pass) (interface{}, error) {
2323
// AST node is a descendant of a workflow FuncDecl.
2424
inWorkflow := true
2525

26-
inspector.Nodes(nil, func(node ast.Node, push bool) bool {
26+
workflowImportName := "workflow"
2727

28+
inspector.Nodes(nil, func(node ast.Node, push bool) bool {
2829
if _, ok := node.(*ast.FuncDecl); ok {
2930
if !push {
3031
// Finished with the current workflow
@@ -44,9 +45,14 @@ func run(pass *analysis.Pass) (interface{}, error) {
4445
}
4546

4647
switch n := node.(type) {
48+
case *ast.ImportSpec:
49+
if n.Path.Value == `"github.com/cschleiden/go-workflows/workflow"` {
50+
workflowImportName = n.Name.Name
51+
}
52+
4753
case *ast.FuncDecl:
4854
// Only check functions that look like workflows
49-
if !isWorkflow(n) {
55+
if !isWorkflow(workflowImportName, n) {
5056
return false
5157
}
5258

@@ -170,7 +176,7 @@ func checkNamed(pass *analysis.Pass, ref types.Object, named *types.Named) {
170176

171177
}
172178

173-
func isWorkflow(funcDecl *ast.FuncDecl) bool {
179+
func isWorkflow(workflowImportName string, funcDecl *ast.FuncDecl) bool {
174180
params := funcDecl.Type.Params.List
175181

176182
// Need at least workflow.Context
@@ -189,7 +195,7 @@ func isWorkflow(funcDecl *ast.FuncDecl) bool {
189195
}
190196

191197
selname := firstParam.Sel.Name
192-
if xname.Name+"."+selname != "workflow.Context" {
198+
if xname.Name+"."+selname != workflowImportName+".Context" {
193199
return false
194200
}
195201

analyzer/analyzer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import (
77
)
88

99
func TestAll(t *testing.T) {
10-
analysistest.Run(t, analysistest.TestData(), Analyzer, "p")
10+
analysistest.Run(t, analysistest.TestData(), Analyzer, "p", "q")
1111
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package workflow
2+
3+
type Context interface{}

analyzer/testdata/src/p/p.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
//nolint
1+
// nolint
22
package p
33

44
// Work around module issues. The analyzer just looks for `workflow.Context` currently
55
import (
66
"context"
7-
workflow "context"
87
"fmt"
98
"time"
109

10+
workflow "github.com/cschleiden/go-workflows/workflow"
11+
1112
"sync"
1213
)
1314

1415
var foo int = 42
1516

16-
func wf(ctx workflow.Context) error {
17+
func wfSimple(ctx workflow.Context) error {
1718
fmt.Println(foo)
1819

1920
return nil

analyzer/testdata/src/q/q.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// nolint
2+
package q
3+
4+
// Work around module issues. The analyzer just looks for `workflow.Context` currently
5+
import (
6+
wf "github.com/cschleiden/go-workflows/workflow"
7+
)
8+
9+
func wfWrongOrder2(ctx wf.Context) (error, string) { // want "workflow `wfWrongOrder2` doesn't return `error` as last return value"
10+
return nil, ""
11+
}

0 commit comments

Comments
 (0)