CredoHotUpgrades.AST (CredoHotUpgrades v1.0.0)

Copy Markdown View Source

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.

Summary

Functions

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.

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

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).

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 line number of an AST node, or nil.

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

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

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

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

Functions

clause_blocks(arg1)

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?(arg1)

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(arg1, fun_name)

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?(ast)

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(arg1)

The line number of an AST node, or nil.

module_name(atom)

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?(arg1)

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

remote_call_mfa(arg1)

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?(arg1, behaviour)

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