Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions sloglint.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,40 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
})
}

checkKeysNaming(opts, pass, keys, attrs)

if len(opts.ForbiddenKeys) > 0 {
forEachKey(pass.TypesInfo, keys, attrs, func(key ast.Expr) {
if name, ok := getKeyName(key); ok && slices.Contains(opts.ForbiddenKeys, name) {
pass.Reportf(key.Pos(), "%q key is forbidden and should not be used", name)
}
})
}

if opts.ArgsOnSepLines && areArgsOnSameLine(pass.Fset, call, keys, attrs) {
pass.Reportf(call.Pos(), "arguments should be put on separate lines")
}
}

func checkKeysNaming(opts *Options, pass *analysis.Pass, keys, attrs []ast.Expr) {
checkKeyNamingCase := func(caseFn func(string) string, caseName string) {
forEachKey(pass.TypesInfo, keys, attrs, func(key ast.Expr) {
if name, ok := getKeyName(key); ok && name != caseFn(name) {
pass.Reportf(call.Pos(), "keys should be written in %s", caseName)
name, ok := getKeyName(key)
if !ok || name == caseFn(name) {
return
}

pass.Report(analysis.Diagnostic{
Pos: key.Pos(),
Message: fmt.Sprintf("keys should be written in %s", caseName),
SuggestedFixes: []analysis.SuggestedFix{{
TextEdits: []analysis.TextEdit{{
Pos: key.Pos(),
End: key.End(),
NewText: []byte(strconv.Quote(caseFn(name))),
}},
}},
})
})
}

Expand All @@ -338,18 +367,6 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
case pascalCase:
checkKeyNamingCase(strcase.ToPascal, "PascalCase")
}

if len(opts.ForbiddenKeys) > 0 {
forEachKey(pass.TypesInfo, keys, attrs, func(key ast.Expr) {
if name, ok := getKeyName(key); ok && slices.Contains(opts.ForbiddenKeys, name) {
pass.Reportf(key.Pos(), "%q key is forbidden and should not be used", name)
}
})
}

if opts.ArgsOnSepLines && areArgsOnSameLine(pass.Fset, call, keys, attrs) {
pass.Reportf(call.Pos(), "arguments should be put on separate lines")
}
}

func checkDiscardHandler(opts *Options, pass *analysis.Pass, name string, call *ast.CallExpr) {
Expand Down
2 changes: 1 addition & 1 deletion testdata/src/key_naming_case/key_naming_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
kebabKey = "foo-bar"
)

func tests() {
func _() {
slog.Info("msg")
slog.Info("msg", "foo_bar", 1)
slog.Info("msg", snakeKey, 1)
Expand Down
77 changes: 77 additions & 0 deletions testdata/src/key_naming_case/key_naming_case.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package key_naming_case

import (
"context"
"log/slog"
)

const (
snakeKey = "foo_bar"
kebabKey = "foo-bar"
)

func _() {
slog.Info("msg")
slog.Info("msg", "foo_bar", 1)
slog.Info("msg", snakeKey, 1)
slog.Info("msg", slog.Int("foo_bar", 1))
slog.Info("msg", slog.Int(snakeKey, 1))
slog.Info("msg", slog.Attr{})
slog.Info("msg", slog.Attr{"foo_bar", slog.IntValue(1)})
slog.Info("msg", slog.Attr{snakeKey, slog.IntValue(1)})
slog.Info("msg", slog.Attr{Key: "foo_bar"})
slog.Info("msg", slog.Attr{Key: snakeKey})
slog.Info("msg", slog.Attr{Key: "foo_bar", Value: slog.IntValue(1)})
slog.Info("msg", slog.Attr{Key: snakeKey, Value: slog.IntValue(1)})
slog.Info("msg", slog.Attr{Value: slog.IntValue(1), Key: "foo_bar"})
slog.Info("msg", slog.Attr{Value: slog.IntValue(1), Key: snakeKey})

slog.Info("msg", "foo_bar", 1) // want `keys should be written in snake_case`
slog.Info("msg", "foo_bar", 1) // want `keys should be written in snake_case`
slog.Info("msg", slog.Int("foo_bar", 1)) // want `keys should be written in snake_case`
slog.Info("msg", slog.Int("foo_bar", 1)) // want `keys should be written in snake_case`
slog.Info("msg", slog.Attr{"foo_bar", slog.IntValue(1)}) // want `keys should be written in snake_case`
slog.Info("msg", slog.Attr{"foo_bar", slog.IntValue(1)}) // want `keys should be written in snake_case`
slog.Info("msg", slog.Attr{Key: "foo_bar"}) // want `keys should be written in snake_case`
slog.Info("msg", slog.Attr{Key: "foo_bar"}) // want `keys should be written in snake_case`
slog.Info("msg", slog.Attr{Key: "foo_bar", Value: slog.IntValue(1)}) // want `keys should be written in snake_case`
slog.Info("msg", slog.Attr{Key: "foo_bar", Value: slog.IntValue(1)}) // want `keys should be written in snake_case`
slog.Info("msg", slog.Attr{Value: slog.IntValue(1), Key: "foo_bar"}) // want `keys should be written in snake_case`
slog.Info("msg", slog.Attr{Value: slog.IntValue(1), Key: "foo_bar"}) // want `keys should be written in snake_case`

// With snake_case key
slog.Info("msg")
slog.With("foo_bar", 1).Info("msg")
slog.With(snakeKey, 1).Info("msg")
slog.With(slog.Int("foo_bar", 1)).Info("msg")
slog.With(slog.Int(snakeKey, 1)).Info("msg")
slog.With(slog.Attr{}).Info("msg")
slog.With(slog.Attr{"foo_bar", slog.IntValue(1)}).Info("msg")
slog.With(slog.Attr{snakeKey, slog.IntValue(1)}).Info("msg")
slog.With(slog.Attr{Key: "foo_bar"}).Info("msg")
slog.With(slog.Attr{Key: snakeKey}).Info("msg")
slog.With(slog.Attr{Key: "foo_bar", Value: slog.IntValue(1)}).Info("msg")
slog.With(slog.Attr{Key: snakeKey, Value: slog.IntValue(1)}).Info("msg")
slog.With(slog.Attr{Value: slog.IntValue(1), Key: "foo_bar"}).Info("msg")
slog.With(slog.Attr{Value: slog.IntValue(1), Key: snakeKey}).Info("msg")

slog.With("foo_bar", 1).Info("msg") // want `keys should be written in snake_case`
slog.With("foo_bar", 1).Info("msg") // want `keys should be written in snake_case`
slog.With(slog.Int("foo_bar", 1)).Info("msg") // want `keys should be written in snake_case`
slog.With(slog.Int("foo_bar", 1)).Info("msg") // want `keys should be written in snake_case`
slog.With(slog.Attr{"foo_bar", slog.IntValue(1)}).Info("msg") // want `keys should be written in snake_case`
slog.With(slog.Attr{"foo_bar", slog.IntValue(1)}).Info("msg") // want `keys should be written in snake_case`
slog.With(slog.Attr{Key: "foo_bar"}).Info("msg") // want `keys should be written in snake_case`
slog.With(slog.Attr{Key: "foo_bar"}).Info("msg") // want `keys should be written in snake_case`
slog.With(slog.Attr{Key: "foo_bar", Value: slog.IntValue(1)}).Info("msg") // want `keys should be written in snake_case`
slog.With(slog.Attr{Key: "foo_bar", Value: slog.IntValue(1)}).Info("msg") // want `keys should be written in snake_case`
slog.With(slog.Attr{Value: slog.IntValue(1), Key: "foo_bar"}).Info("msg") // want `keys should be written in snake_case`
slog.With(slog.Attr{Value: slog.IntValue(1), Key: "foo_bar"}).Info("msg") // want `keys should be written in snake_case`

slog.LogAttrs(context.TODO(), slog.LevelInfo, "msg", slog.Attr{Value: slog.IntValue(1), Key: "foo_bar"}) // want `keys should be written in snake_case`
}

func issue35() {
intAttr := slog.Int
slog.Info("msg", intAttr("foo_bar", 1))
}
Loading