Skip to content

Commit 421ddbc

Browse files
committed
plan 50: use shared settings.ToInt and settings.ToStringSlice helpers
PR #150 landed the settings helpers during our PR's review window. Swap MDS037's local toInt and toStringSlice for the package-level versions so the rule picks up the project-wide coercion semantics: settings.ToInt truncates fractional floats (instead of rejecting them) and rejects NaN/Inf/out-of-range. Update the fractional-float test accordingly.
1 parent b011847 commit 421ddbc

2 files changed

Lines changed: 11 additions & 43 deletions

File tree

internal/rules/duplicatedcontent/rule.go

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/gobwas/glob"
1717
"github.com/jeduden/mdsmith/internal/lint"
1818
"github.com/jeduden/mdsmith/internal/rule"
19+
"github.com/jeduden/mdsmith/internal/rules/settings"
1920
"github.com/yuin/goldmark/ast"
2021
)
2122

@@ -360,11 +361,11 @@ func configDiag(f *lint.File, r *Rule, err error) lint.Diagnostic {
360361
}
361362

362363
// ApplySettings implements rule.Configurable.
363-
func (r *Rule) ApplySettings(settings map[string]any) error {
364-
for k, v := range settings {
364+
func (r *Rule) ApplySettings(cfg map[string]any) error {
365+
for k, v := range cfg {
365366
switch k {
366367
case "include":
367-
list, ok := toStringSlice(v)
368+
list, ok := settings.ToStringSlice(v)
368369
if !ok {
369370
return fmt.Errorf(
370371
"duplicated-content: include must be a list of strings, got %T",
@@ -373,7 +374,7 @@ func (r *Rule) ApplySettings(settings map[string]any) error {
373374
}
374375
r.Include = list
375376
case "exclude":
376-
list, ok := toStringSlice(v)
377+
list, ok := settings.ToStringSlice(v)
377378
if !ok {
378379
return fmt.Errorf(
379380
"duplicated-content: exclude must be a list of strings, got %T",
@@ -382,7 +383,7 @@ func (r *Rule) ApplySettings(settings map[string]any) error {
382383
}
383384
r.Exclude = list
384385
case "min-chars":
385-
n, ok := toInt(v)
386+
n, ok := settings.ToInt(v)
386387
if !ok {
387388
return fmt.Errorf(
388389
"duplicated-content: min-chars must be an integer, got %T",
@@ -425,39 +426,4 @@ func (r *Rule) DefaultSettings() map[string]any {
425426
}
426427
}
427428

428-
func toStringSlice(v any) ([]string, bool) {
429-
switch s := v.(type) {
430-
case []string:
431-
return append([]string(nil), s...), true
432-
case []any:
433-
out := make([]string, 0, len(s))
434-
for _, it := range s {
435-
str, ok := it.(string)
436-
if !ok {
437-
return nil, false
438-
}
439-
out = append(out, str)
440-
}
441-
return out, true
442-
default:
443-
return nil, false
444-
}
445-
}
446-
447-
func toInt(v any) (int, bool) {
448-
switch n := v.(type) {
449-
case int:
450-
return n, true
451-
case int64:
452-
return int(n), true
453-
case float64:
454-
if n != float64(int(n)) {
455-
return 0, false
456-
}
457-
return int(n), true
458-
default:
459-
return 0, false
460-
}
461-
}
462-
463429
var _ rule.Configurable = (*Rule)(nil)

internal/rules/duplicatedcontent/rule_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,12 @@ func TestApplySettings_AcceptsIntegerTypesForMinChars(t *testing.T) {
423423
}
424424
}
425425

426-
func TestApplySettings_RejectsFractionalFloat(t *testing.T) {
426+
func TestApplySettings_TruncatesFractionalFloat(t *testing.T) {
427+
// settings.ToInt truncates toward zero, matching the rest of the
428+
// codebase, so 1.5 becomes 1 rather than being rejected.
427429
r := &Rule{}
428-
err := r.ApplySettings(map[string]any{"min-chars": 1.5})
429-
require.Error(t, err)
430+
require.NoError(t, r.ApplySettings(map[string]any{"min-chars": 1.5}))
431+
assert.Equal(t, 1, r.MinChars)
430432
}
431433

432434
func newLintFileWithRoot(t *testing.T, path, root string) *lint.File {

0 commit comments

Comments
 (0)