| id | 124 |
|---|---|
| title | No space inside code spans rule |
| status | ✅ |
| summary | New rule MDS052 that flags inline code spans with leading or trailing whitespace inside the backticks (e.g. `` ` x` `` or `` `x ` ``). Closes the gap with markdownlint MD038. |
| model | sonnet |
Let users forbid stray whitespace inside inline code
spans. CommonMark strips one optional space on each
side of a code span when both sides have one, but any
other leading or trailing whitespace renders verbatim
and is almost always a typo (` x`,
`x `, `x `). markdownlint covers this as
MD038; mdsmith does not.
Inline code spans are *ast.CodeSpan. The node's
text segment range covers the bytes between the
backtick delimiters, before CommonMark's "trim one
space on each side if both sides have one" rule is
applied. The rule must read those raw bytes to
distinguish "balanced single space" (legal) from any
other whitespace pattern (flagged).
MDS010 pins fence style and MDS011 requires a fence language. Neither inspects the contents of inline code spans. A dedicated rule keeps the toggle independent of fenced-block policy.
rules:
no-space-in-code-spans: trueCategory: whitespace. Disabled by default (opt-in).
No tunables in v1 — the only choice is whether to
enforce.
Walk *ast.CodeSpan. For each node:
- Inspect goldmark's post-CommonMark-trim text segment (the bytes the AST records after stripping one space from each side when both sides have a space and the content is not all-whitespace).
- If the segment's first byte is ASCII whitespace,
emit
code span has leading whitespace. - If the segment's last byte is ASCII whitespace,
emit
code span has trailing whitespace.
Using the post-trim segment avoids false positives.
For ` x `, CommonMark strips one space from
each side, leaving one visible leading space — not
two.
Trim leading and trailing whitespace from the span bytes. Preserve the delimiter count (one or more backticks). When the trimmed body becomes empty, do not auto-fix — emit only the diagnostic.
code span has leading whitespace
code span has trailing whitespace
- Scaffold
internal/rules/nospaceincodespans/withrule.go,rule_test.go, and theinit()rule.Registercall. - Implement
Check()walking*ast.CodeSpanand inspecting goldmark's post-CommonMark-trim segment to detect whitespace that is visible after rendering (not the raw source bytes). - Implement
Fix()that trims whitespace inside the delimiters while preserving backtick count. - Implement
rule.Defaultablereturningfalse. - Register as MDS052 in category
whitespace(MDS048 was taken bygit-hook-sync; MDS049–MDS051 taken by rules merged to main first). - Add fixture tests in
internal/rules/MDS052-no-space-in-code-spans/covering: balanced single space (legal), leading space, trailing space, both-side double space, tab, and the empty-after-trim edge case. - Add rule README following the MDS012 template.
-
`x`emits no diagnostic. -
` x `(balanced single space) emits no diagnostic. -
` x`emits one leading-whitespace diagnostic and fixes to`x`. -
`x `emits one trailing-whitespace diagnostic and fixes to`x`. -
` x `(double space each side) emits both diagnostics and fixes to`x`. -
`\tx`(leading tab) emits a leading- whitespace diagnostic. - An empty-after-trim span (e.g.
` `) emits diagnostics but is not auto-fixed. - Rule is disabled by default.
- All tests pass:
go test ./... -
go tool golangci-lint runreports no issues -
mdsmith check .passes on the repo with the rule disabled (no regression for existing docs).