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
2 changes: 1 addition & 1 deletion PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ footer: |
| 82 | ✅ | [YAML billion-laughs mitigation](plan/82_yaml-billion-laughs.md) |
| 83 | 🔳 | [Security hardening batch](plan/83_security-hardening-batch.md) |
| 84 | 🔲 | [Symlink default-deny for file discovery](plan/84_symlink-default-deny.md) |
| 85 | 🔲 | [Increase test coverage to 95% by extracting shared rule helpers](plan/85_coverage-to-95-percent.md) |
| 85 | 🔳 | [Increase test coverage to 95% by extracting shared rule helpers](plan/85_coverage-to-95-percent.md) |
| 86 | 🔲 | [Markdown flavor validation](plan/86_markdown-flavor-validation.md) |
| 87 | 🔲 | [Flavor validation for GitHub Alerts](plan/87_markdown-flavor-github-alerts.md) |
| 88 | ✅ | [TOC directive migration aid](plan/88_toc-directive-migration.md) |
Expand Down
33 changes: 5 additions & 28 deletions internal/rules/concisenessscoring/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/jeduden/mdsmith/internal/lint"
"github.com/jeduden/mdsmith/internal/mdtext"
"github.com/jeduden/mdsmith/internal/rule"
"github.com/jeduden/mdsmith/internal/rules/settings"
"github.com/yuin/goldmark/ast"
)

Expand Down Expand Up @@ -168,8 +169,8 @@ func isTable(para *ast.Paragraph, f *lint.File) bool {
}

// ApplySettings implements rule.Configurable.
func (r *Rule) ApplySettings(settings map[string]any) error {
for k, v := range settings {
func (r *Rule) ApplySettings(s map[string]any) error {
for k, v := range s {
switch k {
case "min-score":
if err := r.setMinScore(v); err != nil {
Expand All @@ -187,7 +188,7 @@ func (r *Rule) ApplySettings(settings map[string]any) error {
}

func (r *Rule) setMinScore(v any) error {
n, ok := toFloat(v)
n, ok := settings.ToFloat(v)
if !ok {
return fmt.Errorf(
"conciseness-scoring: min-score must be a number, got %T",
Expand All @@ -205,7 +206,7 @@ func (r *Rule) setMinScore(v any) error {
}

func (r *Rule) setMinWords(v any) error {
n, ok := toInt(v)
n, ok := settings.ToInt(v)
if !ok {
return fmt.Errorf(
"conciseness-scoring: min-words must be an integer, got %T",
Expand All @@ -230,29 +231,5 @@ func (r *Rule) DefaultSettings() map[string]any {
}
}

func toFloat(v any) (float64, bool) {
switch n := v.(type) {
case float64:
return n, true
case int:
return float64(n), true
case int64:
return float64(n), true
}
return 0, false
}

func toInt(v any) (int, bool) {
switch n := v.(type) {
case int:
return n, true
case float64:
return int(n), true
case int64:
return int(n), true
}
return 0, false
}

var _ rule.Configurable = (*Rule)(nil)
var _ rule.Defaultable = (*Rule)(nil)
20 changes: 4 additions & 16 deletions internal/rules/firstlineheading/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/jeduden/mdsmith/internal/lint"
"github.com/jeduden/mdsmith/internal/rule"
"github.com/jeduden/mdsmith/internal/rules/settings"
"github.com/yuin/goldmark/ast"
)

Expand Down Expand Up @@ -74,11 +75,11 @@ func (r *Rule) diag(f *lint.File, msg string) []lint.Diagnostic {
}

// ApplySettings implements rule.Configurable.
func (r *Rule) ApplySettings(settings map[string]any) error {
for k, v := range settings {
func (r *Rule) ApplySettings(s map[string]any) error {
for k, v := range s {
switch k {
case "level":
n, ok := toInt(v)
n, ok := settings.ToInt(v)
if !ok {
return fmt.Errorf("first-line-heading: level must be an integer, got %T", v)
}
Expand All @@ -100,19 +101,6 @@ func (r *Rule) DefaultSettings() map[string]any {
}
}

// toInt converts a value to int.
func toInt(v any) (int, bool) {
switch n := v.(type) {
case int:
return n, true
case float64:
return int(n), true
case int64:
return int(n), true
}
return 0, false
}

var _ rule.Configurable = (*Rule)(nil)

func headingLine(heading *ast.Heading, f *lint.File) int {
Expand Down
45 changes: 6 additions & 39 deletions internal/rules/linelength/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/jeduden/mdsmith/internal/lint"
"github.com/jeduden/mdsmith/internal/rule"
"github.com/jeduden/mdsmith/internal/rules/settings"
"github.com/yuin/goldmark/ast"
)

Expand Down Expand Up @@ -55,8 +56,8 @@ func (r *Rule) isExcluded(category string) bool {
}

// ApplySettings implements rule.Configurable.
func (r *Rule) ApplySettings(settings map[string]any) error {
for k, v := range settings {
func (r *Rule) ApplySettings(s map[string]any) error {
for k, v := range s {
if err := r.applySetting(k, v); err != nil {
return err
}
Expand Down Expand Up @@ -84,7 +85,7 @@ func (r *Rule) applySetting(k string, v any) error {
}

func (r *Rule) applyMax(v any) error {
n, ok := toInt(v)
n, ok := settings.ToInt(v)
if !ok {
return fmt.Errorf("line-length: max must be an integer, got %T", v)
}
Expand All @@ -93,7 +94,7 @@ func (r *Rule) applyMax(v any) error {
}

func (r *Rule) applyPositiveIntPtr(v any, name string, target **int) error {
n, ok := toInt(v)
n, ok := settings.ToInt(v)
if !ok {
return fmt.Errorf("line-length: %s must be an integer, got %T", name, v)
}
Expand All @@ -114,7 +115,7 @@ func (r *Rule) applyStern(v any) error {
}

func (r *Rule) applyExclude(v any) error {
list, ok := toStringSlice(v)
list, ok := settings.ToStringSlice(v)
if !ok {
return fmt.Errorf("line-length: exclude must be a list of strings, got %T", v)
}
Expand Down Expand Up @@ -315,40 +316,6 @@ func headingLineNum(h *ast.Heading, f *lint.File) int {
return 0
}

// toInt converts a value to int. Supports int and float64 (YAML decodes
// numbers as int or float64 depending on context).
func toInt(v any) (int, bool) {
switch n := v.(type) {
case int:
return n, true
case float64:
return int(n), true
case int64:
return int(n), true
}
return 0, false
}

// toStringSlice converts a value to []string. YAML decodes sequences as
// []any with string elements.
func toStringSlice(v any) ([]string, bool) {
switch s := v.(type) {
case []string:
return s, true
case []any:
result := make([]string, 0, len(s))
for _, item := range s {
str, ok := item.(string)
if !ok {
return nil, false
}
result = append(result, str)
}
return result, true
}
return nil, false
}

func isValidExclude(s string) bool {
return s == "code-blocks" || s == "tables" || s == "urls"
}
Expand Down
20 changes: 4 additions & 16 deletions internal/rules/maxfilelength/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/jeduden/mdsmith/internal/lint"
"github.com/jeduden/mdsmith/internal/rule"
"github.com/jeduden/mdsmith/internal/rules/settings"
)

func init() {
Expand Down Expand Up @@ -52,11 +53,11 @@ func (r *Rule) Check(f *lint.File) []lint.Diagnostic {
}

// ApplySettings implements rule.Configurable.
func (r *Rule) ApplySettings(settings map[string]any) error {
for k, v := range settings {
func (r *Rule) ApplySettings(s map[string]any) error {
for k, v := range s {
switch k {
case "max":
n, ok := toInt(v)
n, ok := settings.ToInt(v)
if !ok {
return fmt.Errorf(
"max-file-length: max must be an integer, got %T", v,
Expand All @@ -77,17 +78,4 @@ func (r *Rule) DefaultSettings() map[string]any {
return map[string]any{"max": 300}
}

// toInt converts a value to int.
func toInt(v any) (int, bool) {
switch n := v.(type) {
case int:
return n, true
case float64:
return int(n), true
case int64:
return int(n), true
}
return 0, false
}

var _ rule.Configurable = (*Rule)(nil)
20 changes: 4 additions & 16 deletions internal/rules/nomultipleblanks/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/jeduden/mdsmith/internal/lint"
"github.com/jeduden/mdsmith/internal/rule"
"github.com/jeduden/mdsmith/internal/rules/settings"
)

func init() {
Expand Down Expand Up @@ -100,11 +101,11 @@ func (r *Rule) Fix(f *lint.File) []byte {
}

// ApplySettings implements rule.Configurable.
func (r *Rule) ApplySettings(settings map[string]any) error {
for k, v := range settings {
func (r *Rule) ApplySettings(s map[string]any) error {
for k, v := range s {
switch k {
case "max":
n, ok := toInt(v)
n, ok := settings.ToInt(v)
if !ok {
return fmt.Errorf("no-multiple-blanks: max must be an integer, got %T", v)
}
Expand All @@ -123,17 +124,4 @@ func (r *Rule) DefaultSettings() map[string]any {
}
}

// toInt converts a value to int.
func toInt(v any) (int, bool) {
switch n := v.(type) {
case int:
return n, true
case float64:
return int(n), true
case int64:
return int(n), true
}
return 0, false
}

var _ rule.Configurable = (*Rule)(nil)
33 changes: 5 additions & 28 deletions internal/rules/paragraphreadability/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/jeduden/mdsmith/internal/lint"
"github.com/jeduden/mdsmith/internal/mdtext"
"github.com/jeduden/mdsmith/internal/rule"
"github.com/jeduden/mdsmith/internal/rules/settings"
"github.com/yuin/goldmark/ast"
)

Expand Down Expand Up @@ -111,11 +112,11 @@ func paragraphLine(para *ast.Paragraph, f *lint.File) int {
}

// ApplySettings implements rule.Configurable.
func (r *Rule) ApplySettings(settings map[string]any) error {
for k, v := range settings {
func (r *Rule) ApplySettings(s map[string]any) error {
for k, v := range s {
switch k {
case "max-index":
n, ok := toFloat(v)
n, ok := settings.ToFloat(v)
if !ok {
return fmt.Errorf(
"paragraph-readability: max-index must be a number, got %T",
Expand All @@ -124,7 +125,7 @@ func (r *Rule) ApplySettings(settings map[string]any) error {
}
r.MaxIndex = n
case "min-words":
n, ok := toInt(v)
n, ok := settings.ToInt(v)
if !ok {
return fmt.Errorf(
"paragraph-readability: min-words must be an integer, got %T",
Expand All @@ -149,30 +150,6 @@ func (r *Rule) DefaultSettings() map[string]any {
}
}

func toFloat(v any) (float64, bool) {
switch n := v.(type) {
case float64:
return n, true
case int:
return float64(n), true
case int64:
return float64(n), true
}
return 0, false
}

func toInt(v any) (int, bool) {
switch n := v.(type) {
case int:
return n, true
case float64:
return int(n), true
case int64:
return int(n), true
}
return 0, false
}

// isTable returns true if the paragraph's first line starts with a pipe,
// indicating it is a markdown table (goldmark without the table extension
// parses tables as paragraphs).
Expand Down
Loading
Loading