Skip to content

Commit ed5057e

Browse files
authored
feat: implement -no-mixed-args (#25)
1 parent dca7ec3 commit ed5057e

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ The linter has several options, so you can adjust it to your own code style.
2222

2323
## 🚀 Features
2424

25-
* Forbid mixing key-value pairs and attributes within a single function call (default)
26-
* Enforce using either key-value pairs or attributes for the entire project (optional)
25+
* Enforce not mixing key-value pairs and attributes (default)
26+
* Enforce using either key-value pairs only or attributes only (optional)
2727
* Enforce using methods that accept a context (optional)
2828
* Enforce using static log messages (optional)
2929
* Enforce using constants instead of raw keys (optional)
@@ -51,6 +51,16 @@ See the list of [available options][3] to configure the linter.
5151

5252
When using `sloglint` standalone, pass the options as flags of the same name.
5353

54+
### No mixed arguments
55+
56+
The `no-mixed-args` option causes `sloglint` to report mixing key-values pairs and attributes within a single function call:
57+
58+
```go
59+
slog.Info("a user has logged in", "user_id", 42, slog.String("ip_address", "192.0.2.0")) // sloglint: key-value pairs and attributes should not be mixed
60+
```
61+
62+
It is enabled by default.
63+
5464
### Key-value pairs only
5565

5666
The `kv-only` option causes `sloglint` to report any use of attributes:

sloglint.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import (
1919

2020
// Options are options for the sloglint analyzer.
2121
type Options struct {
22-
KVOnly bool // Enforce using key-value pairs only (incompatible with AttrOnly).
23-
AttrOnly bool // Enforce using attributes only (incompatible with KVOnly).
22+
NoMixedArgs bool // Enforce not mixing key-value pairs and attributes (default true).
23+
KVOnly bool // Enforce using key-value pairs only (overrides NoMixedArgs, incompatible with AttrOnly).
24+
AttrOnly bool // Enforce using attributes only (overrides NoMixedArgs, incompatible with KVOnly).
2425
ContextOnly bool // Enforce using methods that accept a context.
2526
StaticMsg bool // Enforce using static log messages.
2627
NoRawKeys bool // Enforce using constants instead of raw keys.
@@ -31,7 +32,7 @@ type Options struct {
3132
// New creates a new sloglint analyzer.
3233
func New(opts *Options) *analysis.Analyzer {
3334
if opts == nil {
34-
opts = new(Options)
35+
opts = &Options{NoMixedArgs: true}
3536
}
3637
return &analysis.Analyzer{
3738
Name: "sloglint",
@@ -69,8 +70,9 @@ func flags(opts *Options) flag.FlagSet {
6970
})
7071
}
7172

72-
boolVar(&opts.KVOnly, "kv-only", "enforce using key-value pairs only (incompatible with -attr-only)")
73-
boolVar(&opts.AttrOnly, "attr-only", "enforce using attributes only (incompatible with -kv-only)")
73+
boolVar(&opts.NoMixedArgs, "no-mixed-args", "enforce not mixing key-value pairs and attributes (default true)")
74+
boolVar(&opts.KVOnly, "kv-only", "enforce using key-value pairs only (overrides -no-mixed-args, incompatible with -attr-only)")
75+
boolVar(&opts.AttrOnly, "attr-only", "enforce using attributes only (overrides -no-mixed-args, incompatible with -kv-only)")
7476
boolVar(&opts.ContextOnly, "context-only", "enforce using methods that accept a context")
7577
boolVar(&opts.StaticMsg, "static-msg", "enforce using static log messages")
7678
boolVar(&opts.NoRawKeys, "no-raw-keys", "enforce using constants instead of raw keys")
@@ -180,7 +182,7 @@ func run(pass *analysis.Pass, opts *Options) {
180182
pass.Reportf(call.Pos(), "attributes should not be used")
181183
case opts.AttrOnly && len(attrs) < len(args):
182184
pass.Reportf(call.Pos(), "key-value pairs should not be used")
183-
case 0 < len(attrs) && len(attrs) < len(args):
185+
case opts.NoMixedArgs && 0 < len(attrs) && len(attrs) < len(args):
184186
pass.Reportf(call.Pos(), "key-value pairs and attributes should not be mixed")
185187
}
186188

sloglint_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
func TestAnalyzer(t *testing.T) {
1111
testdata := analysistest.TestData()
1212

13-
t.Run("mixed arguments", func(t *testing.T) {
13+
t.Run("no mixed arguments", func(t *testing.T) {
1414
analyzer := sloglint.New(nil)
15-
analysistest.Run(t, testdata, analyzer, "mixed_args")
15+
analysistest.Run(t, testdata, analyzer, "no_mixed_args")
1616
})
1717

1818
t.Run("key-value pairs only", func(t *testing.T) {

0 commit comments

Comments
 (0)