Credence.FunctionMatcher (credence v0.6.0)

Copy Markdown

Finds the closest matching defined function for an undefined function call.

When the compiler reports undefined function NAME/ARITY, this module searches the source for defined functions in the same module with matching arity and ranks them by name similarity. Used by UndefinedLocalFunction and UndefinedFunction to fix misspelled or mangled function calls.

Scoring ladder

ScoreMatch type
100Exact name + ? suffix (palindromepalindrome?)
95Exact name + ! suffix (savesave!)
90__ demangles to ? (perfect__perfect?)
85__ demangles to ! (save__save!)
80Candidate is prefix of name (fibfibonacci)
75Name is prefix of candidate (findfind_largest)
70One contains the other (fibonaccido_fibonacci)
0–60Jaro distance scaled to 0–60

The module never "gives up" — if any function with matching arity exists, it's returned. The pipeline validates the fix by compiling.

Summary

Functions

Returns all candidate functions with matching arity, sorted by score descending. Each candidate has :name, :arity, :visibility, and :score.

Returns the best matching function name, or :no_candidates if the module has zero functions with the given arity.

Types

candidate()

@type candidate() :: %{
  name: String.t(),
  arity: non_neg_integer(),
  visibility: :def | :defp,
  score: non_neg_integer()
}

Functions

candidates(source, module_name, undefined_name, arity, opts \\ [])

@spec candidates(String.t(), String.t(), String.t(), non_neg_integer(), keyword()) ::
  [candidate()]

Returns all candidate functions with matching arity, sorted by score descending. Each candidate has :name, :arity, :visibility, and :score.

suggest(source, module_name, undefined_name, arity, opts \\ [])

@spec suggest(String.t(), String.t(), String.t(), non_neg_integer(), keyword()) ::
  {:ok, String.t()} | :no_candidates

Returns the best matching function name, or :no_candidates if the module has zero functions with the given arity.