linters/internal/analyzers/noliteral/linter.go

100 lines
2.5 KiB
Go
Raw Normal View History

2023-07-18 08:16:27 +00:00
package noliteral
import (
"go/ast"
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 {
expr, ok := n.(*ast.CallExpr)
if !ok {
return true
}
isLog, _ := astutils.IsTargetMethod(expr.Fun, Config.TargetMethods)
if !isLog || len(expr.Args) == 0 {
return true
}
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 true
}
for _, pkgName := range Config.DisablePackages {
if pkgName == astutils.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,
})
return true
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
})
}
return nil, nil
}