Skip to content

Latest commit

 

History

History
130 lines (106 loc) · 4.09 KB

File metadata and controls

130 lines (106 loc) · 4.09 KB
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

No space inside code spans rule

Goal

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.

Background

What goldmark exposes

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).

Why a separate rule

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.

Design

Configuration

rules:
  no-space-in-code-spans: true

Category: whitespace. Disabled by default (opt-in). No tunables in v1 — the only choice is whether to enforce.

Detection

Walk *ast.CodeSpan. For each node:

  1. 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).
  2. If the segment's first byte is ASCII whitespace, emit code span has leading whitespace.
  3. 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.

Auto-fix

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.

Error messages

code span has leading whitespace
code span has trailing whitespace

Tasks

  1. Scaffold internal/rules/nospaceincodespans/ with rule.go, rule_test.go, and the init() rule.Register call.
  2. Implement Check() walking *ast.CodeSpan and inspecting goldmark's post-CommonMark-trim segment to detect whitespace that is visible after rendering (not the raw source bytes).
  3. Implement Fix() that trims whitespace inside the delimiters while preserving backtick count.
  4. Implement rule.Defaultable returning false.
  5. Register as MDS052 in category whitespace (MDS048 was taken by git-hook-sync; MDS049–MDS051 taken by rules merged to main first).
  6. 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.
  7. Add rule README following the MDS012 template.

Acceptance Criteria

  • `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 run reports no issues
  • mdsmith check . passes on the repo with the rule disabled (no regression for existing docs).