linters/internal/analyzers/noliteral/linter.go

118 lines
2.9 KiB
Go
Raw Permalink Normal View History

2023-07-18 08:16:27 +00:00
package noliteral
import (
"go/ast"
"go/token"
2023-07-18 08:16:27 +00:00
astutils "git.frostfs.info/TrueCloudLab/linters/pkg/ast-utils"
"github.com/mitchellh/mapstructure"
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,
}
type Configuration struct {
TargetMethods []string `mapstructure:"target-methods"`
DisablePackages []string `mapstructure:"disable-packages"`
ConstantsPackage string `mapstructure:"constants-package"`
Enable bool `mapstructure:"enable"`
}
var Config = Configuration{
TargetMethods: []string{"Debug", "Info", "Warn", "Error"},
}
2023-07-18 08:16:27 +00:00
func New(conf any) (*analysis.Analyzer, error) {
var config Configuration
config.Enable = true
err := mapstructure.Decode(conf, &config)
if err != nil {
Config.Enable = true
return LogsAnalyzer, nil
}
Config.TargetMethods = append(Config.TargetMethods, config.TargetMethods...)
Config.ConstantsPackage = config.ConstantsPackage
Config.DisablePackages = config.DisablePackages
Config.Enable = config.Enable
return LogsAnalyzer, nil
}
2023-07-18 08:16:27 +00:00
func run(pass *analysis.Pass) (interface{}, error) {
if !Config.Enable {
return nil, nil
}
2023-07-18 08:16:27 +00:00
for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool {
switch v := n.(type) {
// a := zap.Error()
case *ast.AssignStmt:
if _, ok := v.Rhs[0].(*ast.CallExpr); ok {
return false
}
// a := &log.Error()
case *ast.UnaryExpr:
if _, ok := v.X.(*ast.CallExpr); ok && v.Op == token.AND {
return false
}
// log.Error()
case *ast.CallExpr:
if expr, ok := n.(*ast.CallExpr); ok {
return analyzeCallExpr(pass, expr, file)
}
2023-07-18 08:16:27 +00:00
}
return true
})
}
2023-07-18 08:16:27 +00:00
return nil, nil
}
func analyzeCallExpr(pass *analysis.Pass, expr *ast.CallExpr, file *ast.File) bool {
isLog, _ := astutils.IsTargetMethod(expr.Fun, Config.TargetMethods)
if !isLog || len(expr.Args) == 0 || astutils.HasNoLintComment(pass, expr.Pos()) {
return false
}
if !astutils.IsStringValue(expr.Args[0]) {
alias, _ := astutils.GetAliasByPkgName(file, Config.ConstantsPackage)
if Config.ConstantsPackage == "" || astutils.GetPackageName(expr.Args[0]) == alias || astutils.GetPackageName(expr.Args[0]) == "" {
return false
}
for _, pkgName := range Config.DisablePackages {
if pkgName == astutils.GetPackageName(expr.Args[0]) {
return false
2023-07-18 08:16:27 +00:00
}
}
pass.Report(analysis.Diagnostic{
Pos: expr.Pos(),
End: expr.End(),
Category: LinterName,
Message: "Wrong package for constants",
SuggestedFixes: nil,
2023-07-18 08:16:27 +00:00
})
return false
2023-07-18 08:16:27 +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
}