Credence.Semantic.RequireDefmoduleWrapper (credence v0.8.0)

Copy Markdown

The canonical "module attributes / code outside a defmodule" rule.

Matches the Elixir compiler diagnostic emitted when @moduledoc/@doc/ @spec (or a top-level def/defp) appear at file scope with no enclosing module (cannot invoke @/1 outside module, ... outside module) and repairs it in one of two ways:

  • No module at all → wrap the whole source in defmodule Solution do … end.
  • Doc/spec attributes orphaned ABOVE an existing defmodule → MOVE that contiguous run of attributes inside the module (their original order, prepended to the body). Wrapping would be wrong here — it nests the existing module and mis-attaches the attributes.

This is a SEMANTIC-round rule by design: the offending code PARSES but does not COMPILE, so it is diagnostic-driven (the syntax round is for code that won't parse; the pattern round is skipped for code that won't compile). It supersedes the redundant rules the tunex evolution runs generated for this one concern — Syntax.NoOrphanedModuleAttributes, Syntax.WrapBareModuleAttrsInDefmodule, Syntax.PreferDefmoduleWrapper, Semantic.NoDocSpecOutsideModule, Semantic.PreferDefmoduleWrapper — and the dead Pattern-round NoAttrBeforeDefmodule (whose fix never fired, since the input never compiles). Its move-into-module logic now lives here, where the round can actually run it.