2023-07-18 08:16:27 +00:00
|
|
|
package noliteral
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
|
2023-08-16 14:45:32 +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,
|
|
|
|
}
|
|
|
|
|
2023-08-01 11:39:49 +00:00
|
|
|
type Configuration struct {
|
2023-08-04 11:26:40 +00:00
|
|
|
TargetMethods []string `mapstructure:"target-methods"`
|
2023-08-07 14:36:41 +00:00
|
|
|
DisablePackages []string `mapstructure:"disable-packages"`
|
2023-08-04 11:26:40 +00:00
|
|
|
ConstantsPackage string `mapstructure:"constants-package"`
|
2023-08-16 14:45:32 +00:00
|
|
|
Enable bool `mapstructure:"enable"`
|
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
|
|
|
|
2023-08-16 14:45:32 +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) {
|
2023-08-16 14:45:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-16 14:45:32 +00:00
|
|
|
isLog, _ := astutils.IsTargetMethod(expr.Fun, Config.TargetMethods)
|
2023-08-17 12:28:14 +00:00
|
|
|
if !isLog || len(expr.Args) == 0 || astutils.HasNoLintComment(pass, expr.Pos()) {
|
2023-07-31 09:59:51 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-08-16 14:45:32 +00:00
|
|
|
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]) == "" {
|
2023-08-04 11:26:40 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-08-07 14:36:41 +00:00
|
|
|
for _, pkgName := range Config.DisablePackages {
|
2023-08-16 14:45:32 +00:00
|
|
|
if pkgName == astutils.GetPackageName(expr.Args[0]) {
|
2023-08-07 14:36:41 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 11:26:40 +00:00
|
|
|
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
|
|
|
|
}
|