Skip to content

Commit e524783

Browse files
committed
Add rule maintainability metadata and help patterns topic
1 parent 41e61a5 commit e524783

63 files changed

Lines changed: 165 additions & 13 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/mdsmith/main.go

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"io"
78
"math"
89
"os"
910
"path/filepath"
1011
"runtime/debug"
12+
"strings"
1113

1214
flag "github.com/spf13/pflag"
1315

@@ -1037,6 +1039,7 @@ Topics:
10371039
kinds Show concept page for file kinds
10381040
kinds-cli Summarize the 'kinds' subcommand surface
10391041
placeholder-grammar Show placeholder vocabulary reference
1042+
patterns Show maintainability patterns across rules
10401043
`
10411044

10421045
// runHelp implements the "help" subcommand.
@@ -1057,12 +1060,53 @@ func runHelp(args []string) int {
10571060
return runHelpKindsCLI()
10581061
case "placeholder-grammar":
10591062
return runHelpConcept("placeholder-grammar")
1063+
case "patterns":
1064+
return runHelpPatterns(args[1:])
10601065
default:
10611066
fmt.Fprintf(os.Stderr, "mdsmith: help: unknown topic %q\n", args[0])
10621067
return 2
10631068
}
10641069
}
10651070

1071+
func runHelpPatterns(args []string) int {
1072+
format := "text"
1073+
if len(args) >= 2 && (args[0] == "-f" || args[0] == "--format") {
1074+
format = args[1]
1075+
}
1076+
rules, err := ruledocs.ListRules()
1077+
if err != nil {
1078+
fmt.Fprintf(os.Stderr, "mdsmith: %v\n", err)
1079+
return 2
1080+
}
1081+
type rec struct {
1082+
ID string `json:"id"`
1083+
Name string `json:"name"`
1084+
Signal string `json:"signal"`
1085+
Fix string `json:"fix"`
1086+
ForDiagnostic bool `json:"for-diagnostic"`
1087+
}
1088+
items := make([]rec, 0)
1089+
for _, r := range rules {
1090+
if r.Maintainability == nil {
1091+
continue
1092+
}
1093+
items = append(items, rec{r.ID, r.Name, r.Maintainability.Signal, r.Maintainability.Fix, r.Maintainability.ForDiagnostic})
1094+
}
1095+
if format == "json" {
1096+
enc := json.NewEncoder(os.Stdout)
1097+
enc.SetIndent("", " ")
1098+
if err := enc.Encode(items); err != nil {
1099+
fmt.Fprintf(os.Stderr, "mdsmith: %v\n", err)
1100+
return 2
1101+
}
1102+
return 0
1103+
}
1104+
for _, it := range items {
1105+
fmt.Printf("%s %s\n signal: %s\n fix: %s\n for-diagnostic: %t\n\n", it.ID, it.Name, it.Signal, it.Fix, it.ForDiagnostic)
1106+
}
1107+
return 0
1108+
}
1109+
10661110
const helpKindsText = `File Kinds
10671111
10681112
A kind is a named bundle of rule settings that can be applied to a set of
@@ -1192,11 +1236,28 @@ func listAllRules() int {
11921236
}
11931237

11941238
func showRule(query string) int {
1195-
content, err := ruledocs.LookupRule(query)
1239+
rules, err := ruledocs.ListRules()
11961240
if err != nil {
11971241
fmt.Fprintf(os.Stderr, "mdsmith: %v\n", err)
11981242
return 2
11991243
}
1244+
var chosen *ruledocs.RuleInfo
1245+
q := strings.ToUpper(query)
1246+
for i := range rules {
1247+
if strings.ToUpper(rules[i].ID) == q || rules[i].Name == query {
1248+
chosen = &rules[i]
1249+
break
1250+
}
1251+
}
1252+
if chosen == nil {
1253+
fmt.Fprintf(os.Stderr, "mdsmith: unknown rule %q\n", query)
1254+
return 2
1255+
}
1256+
content := ruledocs.StripFrontMatter(chosen.Content)
1257+
if chosen.Maintainability != nil {
1258+
content += "\n\n## Maintainability pattern\n\n"
1259+
content += fmt.Sprintf("- Signal: %s\n- Fix: %s\n- For diagnostic: %t\n", chosen.Maintainability.Signal, chosen.Maintainability.Fix, chosen.Maintainability.ForDiagnostic)
1260+
}
12001261
fmt.Print(content)
12011262
return 0
12021263
}

internal/rules/MDS001-line-length/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ name: line-length
44
status: ready
55
description: Line exceeds maximum length.
66
nature: content
7+
maintainability: null
78
---
89
# MDS001: line-length
910

internal/rules/MDS002-heading-style/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ name: heading-style
44
status: ready
55
description: Heading style must be consistent.
66
nature: style
7+
maintainability: null
78
---
89
# MDS002: heading-style
910

internal/rules/MDS003-heading-increment/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ name: heading-increment
44
status: ready
55
description: Heading levels should increment by one. No jumping from `#` to `###`.
66
nature: structure
7+
maintainability: null
78
---
89
# MDS003: heading-increment
910

internal/rules/MDS004-first-line-heading/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ name: first-line-heading
44
status: ready
55
description: First line of the file should be a heading.
66
nature: structure
7+
maintainability: null
78
---
89
# MDS004: first-line-heading
910

internal/rules/MDS005-no-duplicate-headings/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ name: no-duplicate-headings
44
status: ready
55
description: No two headings should have the same text.
66
nature: structure
7+
maintainability: null
78
---
89
# MDS005: no-duplicate-headings
910

internal/rules/MDS006-no-trailing-spaces/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ name: no-trailing-spaces
44
status: ready
55
description: No trailing whitespace at the end of lines.
66
nature: style
7+
maintainability: null
78
---
89
# MDS006: no-trailing-spaces
910

internal/rules/MDS007-no-hard-tabs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ name: no-hard-tabs
44
status: ready
55
description: No tab characters. Use spaces instead.
66
nature: style
7+
maintainability: null
78
---
89
# MDS007: no-hard-tabs
910

internal/rules/MDS008-no-multiple-blanks/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ name: no-multiple-blanks
44
status: ready
55
description: No more than one consecutive blank line.
66
nature: style
7+
maintainability: null
78
---
89
# MDS008: no-multiple-blanks
910

internal/rules/MDS009-single-trailing-newline/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ name: single-trailing-newline
44
status: ready
55
description: File must end with exactly one newline character.
66
nature: style
7+
maintainability: null
78
---
89
# MDS009: single-trailing-newline
910

0 commit comments

Comments
 (0)