Skip to content

Commit 1ff7144

Browse files
committed
Make analyzer work for multiple files
1 parent 4af9ccd commit 1ff7144

File tree

6 files changed

+64
-21
lines changed

6 files changed

+64
-21
lines changed

analyzer/analyzer.go

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ import (
99
"golang.org/x/tools/go/ast/inspector"
1010
)
1111

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
1725
}
1826

1927
func run(pass *analysis.Pass) (interface{}, error) {
@@ -25,7 +33,20 @@ func run(pass *analysis.Pass) (interface{}, error) {
2533

2634
workflowImportName := "workflow"
2735

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+
2950
if _, ok := node.(*ast.FuncDecl); ok {
3051
if !push {
3152
// Finished with the current workflow
@@ -59,17 +80,19 @@ func run(pass *analysis.Pass) (interface{}, error) {
5980
inWorkflow = true
6081

6182
// 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+
}
7396
}
7497
}
7598

analyzer/analyzer_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@ package analyzer
33
import (
44
"testing"
55

6+
"github.com/stretchr/testify/require"
67
"golang.org/x/tools/go/analysis/analysistest"
78
)
89

910
func TestAll(t *testing.T) {
10-
analysistest.Run(t, analysistest.TestData(), Analyzer, "p", "q")
11+
a := New()
12+
a.Flags.Set("checkprivatereturnvalues", "true")
13+
analysistest.Run(t, analysistest.TestData(), a, "p", "q")
14+
}
15+
16+
func TestComplex(t *testing.T) {
17+
a := New()
18+
a.Flags.Set("checkprivatereturnvalues", "true")
19+
result := analysistest.Run(t, analysistest.TestData(), a, "q")
20+
for _, r := range result {
21+
require.NoError(t, r.Err)
22+
require.Equal(t, 1, len(r.Diagnostics))
23+
}
1124
}

analyzer/plugin/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type analyzerPlugin struct{}
1313

1414
func (*analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
1515
return []*analysis.Analyzer{
16-
analyzer.Analyzer,
16+
analyzer.New(),
1717
}
1818
}
1919

analyzer/testdata/src/p/p.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// nolint
22
package p
33

4-
// Work around module issues. The analyzer just looks for `workflow.Context` currently
54
import (
65
"context"
76
"fmt"

analyzer/testdata/src/q/q1.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// nolint
2+
package q
3+
4+
type MyKey int
5+
6+
const (
7+
MyKey1 MyKey = iota
8+
MyKey2
9+
)

analyzer/testdata/src/q/q.go renamed to analyzer/testdata/src/q/q2.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// nolint
22
package q
33

4-
// Work around module issues. The analyzer just looks for `workflow.Context` currently
54
import (
65
wf "github.com/cschleiden/go-workflows/workflow"
76
)

0 commit comments

Comments
 (0)