From f3e8da7493ef26870c17e244805c2c90b9e58ab2 Mon Sep 17 00:00:00 2001 From: Aleksey Bakin Date: Fri, 8 Jan 2021 10:40:07 +0300 Subject: [PATCH] refactor: collect reports before flush --- pkg/analyzer/analyzer.go | 16 ++++++++++------ pkg/analyzer/report.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 pkg/analyzer/report.go diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index f9cd29c..ba1d5fd 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -123,6 +123,7 @@ func (t thelper) run(pass *analysis.Pass) (interface{}, error) { return nil, nil } + var reports reports inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ (*ast.FuncDecl)(nil), @@ -145,9 +146,12 @@ func (t thelper) run(pass *analysis.Pass) (interface{}, error) { return } - checkFunc(pass, fd, tCheckOpts) - checkFunc(pass, fd, bCheckOpts) + checkFunc(pass, &reports, fd, tCheckOpts) + checkFunc(pass, &reports, fd, bCheckOpts) }) + + reports.Flush(pass) + return nil, nil } @@ -215,7 +219,7 @@ type funcDecl struct { Body *ast.BlockStmt } -func checkFunc(pass *analysis.Pass, funcDecl funcDecl, opts checkFuncOpts) { +func checkFunc(pass *analysis.Pass, reports *reports, funcDecl funcDecl, opts checkFuncOpts) { if strings.HasPrefix(funcDecl.Name.Name, opts.skipPrefix) { return } @@ -234,20 +238,20 @@ func checkFunc(pass *analysis.Pass, funcDecl funcDecl, opts checkFuncOpts) { } if !checkFirstPassed { - pass.Reportf(funcDecl.Pos, "parameter %s should be the first or after context.Context", opts.tbType) + reports.Reportf(funcDecl.Pos, "parameter %s should be the first or after context.Context", opts.tbType) } } } if opts.checkName { if len(p.Names) > 0 && p.Names[0].Name != opts.varName { - pass.Reportf(funcDecl.Pos, "parameter %s should have name %s", opts.tbType, opts.varName) + reports.Reportf(funcDecl.Pos, "parameter %s should have name %s", opts.tbType, opts.varName) } } if opts.checkBegin { if len(funcDecl.Body.List) == 0 || !isTHelperCall(pass, funcDecl.Body.List[0], opts.tbHelper) { - pass.Reportf(funcDecl.Pos, "test helper function should start from %s.Helper()", opts.varName) + reports.Reportf(funcDecl.Pos, "test helper function should start from %s.Helper()", opts.varName) } } } diff --git a/pkg/analyzer/report.go b/pkg/analyzer/report.go new file mode 100644 index 0000000..272fc08 --- /dev/null +++ b/pkg/analyzer/report.go @@ -0,0 +1,31 @@ +package analyzer + +import ( + "go/token" + + "golang.org/x/tools/go/analysis" +) + +type reports struct { + reports []report +} + +type report struct { + pos token.Pos + format string + args []interface{} +} + +func (rr *reports) Reportf(pos token.Pos, format string, args ...interface{}) { + rr.reports = append(rr.reports, report{ + pos: pos, + format: format, + args: args, + }) +} + +func (rr reports) Flush(pass *analysis.Pass) { + for _, r := range rr.reports { + pass.Reportf(r.pos, r.format, r.args...) + } +}