# `CredoHotUpgrades.AST`
[🔗](https://github.com/deployex/credo_hot_upgrades/blob/main/lib/credo_hot_upgrades/ast.ex#L1)

Small, shared AST predicates used by more than one check.

None of this does data-flow analysis. Every function answers a narrow
syntactic question about one node, the same way the rest of Credo's own
checks do.

# `clause_blocks`

The blocks of a `def`/`defp` clause, as the keyword list Elixir quotes them
into: `[do: body]`, and `rescue:`, `catch:`, `else:` and `after:` when the
function has them. `nil` for a clause with no body.

Walk the whole list to look at everything the function does, or take `:do`
from it for its statements.

# `closure?`

True for an anonymous function (`fn ... end`) or a capture of a *local*
function (`&foo/1`, `&(&1 + 1)`).

False for a capture of a remote function, whatever names the module:
an alias (`&Module.foo/1`), `__MODULE__` (`&__MODULE__.foo/1`), an atom
(`&:erlang.send/2`) or a variable (`&module.foo/1`). All of them resolve
through the module by name when called, and survive a hot upgrade of the
module that merely captured them. So does a capture of a function of
`Kernel`, `&length/1`, which is `&:erlang.length/1` once compiled.

# `def_clauses`

Finds a `def`/`defp` clause by name anywhere at the top level of a module's
`do` block, returning its clauses (there can be more than one, for
different arities or pattern matches).

# `holds_closure?`

True when the value holds a closure that is kept with it: the value is one,
or it is a tuple, a list, a map or a struct with one somewhere inside.

The arguments of a call are not looked at. In
`{:rows, Enum.map(rows, &format/1)}` the capture is used by `Enum.map/2`
while the tuple is built, and what is kept is the list it returns.

# `line`

The line number of an AST node, or `nil`.

# `module_name`

Turns a module reference AST (`{:__aliases__, _, [:Foo, :Bar]}` or an atom)
into a dotted name, or `nil` when it does not statically name one.

`__MODULE__.Foo` is a valid alias at compile time, but its AST is an
`:__aliases__` whose first part is the `__MODULE__` node itself rather
than an atom. Which module that resolves to is only known once the
module compiles, so it can never match one of the known modules a check
looks for, and the only correct answer here is `nil`, not a crash.

# `remote_call?`

True for a remote call `Mod.fun(...)` or `:mod.fun(...)`, false otherwise.

# `remote_call_mfa`

The `{module_ast, function_name, arity}` of a remote call, or `nil`.

`module_ast` is the raw alias/atom AST, use `module_name/1` to turn it
into a list of atoms or a plain atom.

# `uses_behaviour?`

True when the module body uses or implements the given behaviour (`GenServer`, `Supervisor`, `Application`, ...).

---

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