Credence.Syntax.FixMissingModuleEnd (credence v0.8.0)

Copy Markdown

Repairs source whose trailing do blocks were never closed — most commonly a defmodule (or def) missing its final end.

LLMs repeatedly emit a complete module body but drop the closing end(s), so the parser reaches EOF with an open do block and reports missing terminator: end. It is purely structural, so this is a REPAIR: append the missing end at end-of-file.

Detection is driven by the parser — Code.string_to_quoted/1 reports missing terminator: end with expected_delimiter: :end only when an opened block ran off the end of the file. The fix appends one end, re-parses, and repeats, so several missing ends are closed in order; it stops the moment the source parses or the error changes (e.g. an unclosed ( expecting ), or a mismatched delimiter — both left for other rules). It never fires when the first parse error is something other than a missing end.

Bad (won't parse — missing terminator: end)

defmodule Solution do
  def f(x), do: x

Good

defmodule Solution do
  def f(x), do: x
end