From 435397de49adc9740bc67416166aba4d0be8ae2a Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Tue, 24 Oct 2023 07:34:00 +0200 Subject: feat: Add cmds --- cmd/cvet/logger.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 cmd/cvet/logger.go (limited to 'cmd/cvet/logger.go') diff --git a/cmd/cvet/logger.go b/cmd/cvet/logger.go new file mode 100644 index 0000000..0852f38 --- /dev/null +++ b/cmd/cvet/logger.go @@ -0,0 +1,71 @@ +package main + +import ( + "go/ast" + + "golang.org/x/tools/go/analysis" +) + +func checkNotLog(pass *analysis.Pass, node *ast.ExprStmt) { + prevCall, ok := node.X.(*ast.CallExpr) + if !ok { + return + } + selector, ok := prevCall.Fun.(*ast.SelectorExpr) + if !ok { + return + } + switch { + case isIdent(selector.X, "slog"): + case isIdent(selector.X, "logger"): + pass.Reportf(node.Pos(), "no log before panic") + } +} + +func checkLogBeforePanic(pass *analysis.Pass, node *ast.BlockStmt) { + for i, stmt := range node.List { + expr, ok := stmt.(*ast.ExprStmt) + if !ok { + continue + } + call, ok := expr.X.(*ast.CallExpr) + if !ok { + continue + } + if i > 0 && isIdent(call.Fun, "panic") { + if prev, ok := node.List[i-1].(*ast.ExprStmt); ok { + checkNotLog(pass, prev) + } + } + } +} + +func checkLogWithArgs(node *ast.CallExpr, pass *analysis.Pass) { + for _, arg := range node.Args { + call, ok := arg.(*ast.CallExpr) + if !ok { + continue + } + if selector, ok := call.Fun.(*ast.SelectorExpr); ok { + ident, ok := selector.X.(*ast.Ident) + if ok && ident.Name != "slog" { + pass.Reportf(node.Pos(), "slog.With and logger.With must be called with a slog arg") + } + } else { + pass.Reportf(node.Pos(), "slog.With and logger.With must be called with a slog arg") + } + } +} + +func checkLogUsage(pass *analysis.Pass, node *ast.CallExpr) { + if selector, ok := node.Fun.(*ast.SelectorExpr); ok { + if ident, ok := selector.X.(*ast.Ident); ok { + if ident.Name == "log" { + pass.Reportf(node.Pos(), "found old log usage") + } + } + } + if isSelector(node.Fun, "slog", "With") || isSelector(node.Fun, "logger", "With") { + checkLogWithArgs(node, pass) + } +} -- cgit v1.3