2023-07-18 08:16:27 +00:00
|
|
|
package noliteral
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2023-08-04 11:26:40 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2023-07-18 08:16:27 +00:00
|
|
|
|
|
|
|
"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,
|
|
|
|
}
|
|
|
|
|
2023-08-04 11:26:40 +00:00
|
|
|
var (
|
|
|
|
aliasCache = sync.Map{}
|
|
|
|
)
|
|
|
|
|
2023-08-01 11:39:49 +00:00
|
|
|
type Configuration struct {
|
2023-08-04 11:26:40 +00:00
|
|
|
TargetMethods []string `mapstructure:"target-methods"`
|
|
|
|
ConstantsPackage string `mapstructure:"constants-package"`
|
2023-08-01 11:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var Config = Configuration{
|
|
|
|
TargetMethods: []string{"Debug", "Info", "Warn", "Error"},
|
|
|
|
}
|
2023-07-18 08:16:27 +00:00
|
|
|
|
|
|
|
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)
|
2023-08-04 11:26:40 +00:00
|
|
|
if !isLog || len(expr.Args) == 0 {
|
2023-07-31 09:59:51 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-08-04 11:26:40 +00:00
|
|
|
if !isStringValue(expr.Args[0]) {
|
|
|
|
alias, _ := getAliasByPkgName(file, Config.ConstantsPackage)
|
|
|
|
if Config.ConstantsPackage == "" || getPackageName(expr.Args[0]) == alias || getPackageName(expr.Args[0]) == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
pass.Report(analysis.Diagnostic{
|
|
|
|
Pos: expr.Pos(),
|
|
|
|
End: expr.End(),
|
|
|
|
Category: LinterName,
|
|
|
|
Message: "Wrong package for constants",
|
|
|
|
SuggestedFixes: nil,
|
|
|
|
})
|
2023-07-31 09:59:51 +00:00
|
|
|
return true
|
2023-07-18 08:16:27 +00:00
|
|
|
}
|
2023-07-31 09:59:51 +00:00
|
|
|
|
|
|
|
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
|
2023-07-18 08:16:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLogDot(expr ast.Expr) (bool, string) {
|
|
|
|
sel, ok := expr.(*ast.SelectorExpr)
|
|
|
|
if !ok {
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
|
2023-08-01 11:39:49 +00:00
|
|
|
for _, method := range Config.TargetMethods {
|
2023-07-18 08:16:27 +00:00
|
|
|
if isIdent(sel.Sel, method) {
|
|
|
|
return true, method
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func isIdent(expr ast.Expr, ident string) bool {
|
|
|
|
id, ok := expr.(*ast.Ident)
|
2023-07-31 09:59:51 +00:00
|
|
|
if ok && id.Name == ident {
|
2023-07-18 08:16:27 +00:00
|
|
|
return true
|
|
|
|
}
|
2023-07-31 09:59:51 +00:00
|
|
|
|
2023-07-18 08:16:27 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isStringValue(expr ast.Expr) bool {
|
|
|
|
basicLit, ok := expr.(*ast.BasicLit)
|
|
|
|
return ok && basicLit.Kind == token.STRING
|
|
|
|
}
|
2023-08-04 11:26:40 +00:00
|
|
|
|
|
|
|
func getAliasByPkgName(file *ast.File, pkgName string) (string, error) {
|
|
|
|
if alias, ok := aliasCache.Load(file); ok {
|
|
|
|
return alias.(string), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var alias string
|
|
|
|
specs := file.Imports
|
|
|
|
|
|
|
|
for _, spec := range specs {
|
|
|
|
alias = getAliasFromImportSpec(spec, pkgName)
|
|
|
|
if alias != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasCache.Store(file, alias)
|
|
|
|
return alias, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAliasFromImportSpec(spec *ast.ImportSpec, pkgName string) string {
|
|
|
|
if spec == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
importName := strings.Replace(spec.Path.Value, "\"", "", -1)
|
|
|
|
if importName != pkgName {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
split := strings.Split(importName, "/")
|
|
|
|
|
|
|
|
if len(split) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
alias := split[len(split)-1]
|
|
|
|
if spec.Name != nil {
|
|
|
|
alias = spec.Name.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return alias
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPackageName(expr ast.Expr) string {
|
|
|
|
if selectorExpr, ok := expr.(*ast.SelectorExpr); ok {
|
|
|
|
if ident, ok := selectorExpr.X.(*ast.Ident); ok {
|
|
|
|
return ident.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|