Credence.Syntax.NoMarkdownCodeFences
(credence v0.8.0)
Copy Markdown
Removes the markdown code-fence lines that wrap generated Elixir source.
LLMs frequently wrap their answer in a markdown code fence — a line that is
nothing but three backticks (U+0060×3), optionally with a language tag like
elixir. Such a line is never valid Elixir, so the wrapped module fails to
parse. This is a REPAIR: strip the wrapping fence lines.
Why only the wrapping fences
A fence line can also appear legitimately inside a @moduledoc/@doc
heredoc (documentation that shows a fenced code block). Because the syntax
phase runs every rule's fix whenever the source fails to parse for any
reason, a rule that stripped every fence-shaped line would corrupt those
in-docstring fences when some unrelated part of the file fails to parse.
Two guards keep the repair to the genuine bug:
- Anchor — only the file's first and last non-blank lines are eligible.
A docstring fence sits between
defmodule … do/an attribute and the closingend, so it is never the first or last non-blank line and is never touched. - Parse gate — the stripped result is committed only when it then parses. If the file was unparseable for an unrelated reason, stripping a wrapping fence cannot fix it, so the result still fails to parse and the original is returned unchanged.
Bad (won't parse)
```elixir
defmodule Solution do
def hello, do: :world
end
```Good
defmodule Solution do
def hello, do: :world
end