linters/internal/analyzers/noliteral/linter.go
Alexander Chuprov df29d08821
All checks were successful
Tests and linters / Lint (pull_request) Successful in 1m19s
Tests and linters / Tests (pull_request) Successful in 1m58s
Tests and linters / Build lib (pull_request) Successful in 2m17s
Tests and linters / Staticcheck (pull_request) Successful in 1m7s
[#5] linters: refactoring
Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
2023-07-31 12:59:51 +03:00

78 lines
1.5 KiB
Go

package noliteral
import (
"go/ast"
"go/token"
"golang.org/x/tools/go/analysis"
)
const LinterName = "noliteral"
var LogsAnalyzer = &analysis.Analyzer{
Name: LinterName,
Doc: LinterName + " is a helper tool that ensures logging messages in Go code are structured and not written as simple text.",
Run: run,
}
var methodsToSearch = []string{"Debug", "Info", "Warn", "Error", "reportFlushError", "reportError"}
func run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool {
expr, ok := n.(*ast.CallExpr)
if !ok {
return true
}
isLog, _ := isLogDot(expr.Fun)
if !isLog {
return true
}
if len(expr.Args) == 0 || !isStringValue(expr.Args[0]) {
return true
}
pass.Report(analysis.Diagnostic{
Pos: expr.Pos(),
End: expr.End(),
Category: LinterName,
Message: "Literals are not allowed in the body of the logger",
SuggestedFixes: nil,
})
return false
})
}
return nil, nil
}
func isLogDot(expr ast.Expr) (bool, string) {
sel, ok := expr.(*ast.SelectorExpr)
if !ok {
return false, ""
}
for _, method := range methodsToSearch {
if isIdent(sel.Sel, method) {
return true, method
}
}
return false, ""
}
func isIdent(expr ast.Expr, ident string) bool {
id, ok := expr.(*ast.Ident)
if ok && id.Name == ident {
return true
}
return false
}
func isStringValue(expr ast.Expr) bool {
basicLit, ok := expr.(*ast.BasicLit)
return ok && basicLit.Kind == token.STRING
}