# `Juggernaut.Schemata.Lift`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/schemata/lift.ex#L1)

Lifted dispatch for mutants a `case` cannot stand in for: patterns, guards, dropped
clauses, pipe stages (design notes §10.3).

The function's clauses stay where they are, under their own name, kind and arity, so at
switch `0` the code runs as written: `__ENV__.function`, stack frames, a
`FunctionClauseError` and Logger's `:mfa` all name the function itself. Each clause
binds its arguments (`pattern = jg_a1`) and its body becomes a dispatch on the site:

    def f(x = jg_a1) when x > 0, do: (case Juggernaut.Peer.Switch.site(17, 2) do 0 -> (x); 1 -> __jg_17_f__(jg_a1); 2 -> __jg_18_f__(jg_a1) end)

Arguments no clause matches reach a catch-all clause written after the last one (left
out when the last clause matches anything), which dispatches the same way and at
switch `0` raises the `FunctionClauseError` the function would raise. A body with
`rescue`, `catch`, `else` or `after` keeps them in a `try` inside the `0` branch, so a
mutant's copy never runs inside the original's `try`.

Each lifted mutant gets a copy of every clause with only that mutant applied, renamed
`__jg_<switch>_f__` (private) and written after the last clause. A module attribute a
copy reads is snapshotted where its clause is (`@jg_<site>_<clause>_<name> @name;` on
the clause's line), so the copy sees the value the clause sees even when the attribute
is set again between clauses. Exports never change: only private functions are added.

Copies always get every argument. Default arguments written on a clause with a body
move to a header before the first clause (a function with defaults and more than one
clause needs one); a mutant inside a default argument therefore cannot be lifted
(`in_default?/3`). Inline sites inside a default are rendered into the header.

Known limit: while a lifted mutant is on, its code runs under the copy's name, so a
test that checks the function's own name (a stack frame, `FunctionClauseError`) kills
that mutant.

# `arg_plan`

```elixir
@type arg_plan() :: %{
  pattern_to: non_neg_integer(),
  stop: non_neg_integer(),
  default: offsets() | nil
}
```

# `clause_plan`

```elixir
@type clause_plan() :: %{
  from: non_neg_integer(),
  to: non_neg_integer(),
  args: [arg_plan()],
  body: {:block | :try | :keyword, non_neg_integer(), non_neg_integer()} | nil,
  total?: boolean(),
  defaults: %{required(non_neg_integer()) =&gt; offsets()}
}
```

# `offsets`

```elixir
@type offsets() :: {non_neg_integer(), non_neg_integer()}
```

# `t`

```elixir
@type t() :: %{
  name: atom(),
  arity: arity(),
  kind: :def | :defp,
  clauses: [clause_plan()],
  defaults: %{required(non_neg_integer()) =&gt; offsets()},
  header?: boolean()
}
```

# `edits`

```elixir
@spec edits(
  t(),
  Juggernaut.Schemata.Text.t(),
  [{pos_integer(), non_neg_integer(), non_neg_integer(), String.t()}],
  [Juggernaut.Schemata.Text.edit()]
) :: {[Juggernaut.Schemata.Text.edit()], [pos_integer()]}
```

The edits that lift the function: the dispatch in each clause (and the header, the
catch-all clause and the attribute snapshots), and the mutants' copies after the last
clause. `mutants` are `{switch, from, to, replacement}`, consecutive from `first`;
`inline` are the file's inline edits (those inside a default argument are copied into
the header). Returns the edits and the switches whose copy could not be written.

# `helper`

```elixir
@spec helper(atom(), pos_integer()) :: String.t()
```

The name of a lifted mutant's copy of the function.

# `in_default?`

```elixir
@spec in_default?(t(), non_neg_integer(), non_neg_integer()) :: boolean()
```

Whether the bytes `from..to` lie inside a default argument of the lifted function.

# `plan`

```elixir
@spec plan(Juggernaut.Schemata.Text.t(), [Juggernaut.Mutator.ModuleScan.clause()]) ::
  {:ok, t()} | {:error, String.t()}
```

Plans the lift of the function whose `clauses` (`ModuleScan` clauses, source order)
are in `text`, or says why it cannot be lifted.

# `rename`

```elixir
@spec rename(String.t(), atom(), atom(), String.t(), String.t() | nil) ::
  {:ok, String.t(), [atom()]} | :error
```

`clause` (the text of one `def`/`defp` clause) as a private clause named `new_name`
that takes every argument (default arguments dropped); an empty clause (dropped by its
mutant) stays empty. With an `attribute_prefix`, each module attribute the clause reads
is read as `@<prefix><name>` instead, and the names read are returned. `:error` when
the text does not parse as one clause of `name`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
