View Source Igniter.Code.Function (igniter v0.4.7)

Utilities for working with functions.

Summary

Functions

Appends an argument to a function call, leaving the zipper at the function call's node.

Checks if the provided function call (in a Zipper) has an argument that equals term at index.

Returns true if the argument at the provided index exists and matches the provided pattern

Returns true if the argument at the given index matches the provided predicate

Returns true if the value is a function literal.

Returns true if the node is a function call

Returns true if the node is a function call of the given name

Gets the name of a local function call, or :error if the node is not a function call or it cannot be determined

Moves to a function call by the given name and arity, matching the given predicate, in the current or lower scope

Moves to a function call by the given name and arity, matching the given predicate, in the current scope

Moves to the nth argument of a function call.

Updates the nth argument of a function call, leaving the zipper at the function call's node.

Functions

Link to this function

append_argument(zipper, value)

View Source
@spec append_argument(Sourceror.Zipper.t(), any()) ::
  {:ok, Sourceror.Zipper.t()} | :error

Appends an argument to a function call, leaving the zipper at the function call's node.

Link to this function

argument_equals?(zipper, index, term)

View Source
@spec argument_equals?(Sourceror.Zipper.t(), integer(), any()) :: boolean()

Checks if the provided function call (in a Zipper) has an argument that equals term at index.

Link to this macro

argument_matches_pattern?(zipper, index, pattern)

View Source (macro)

Returns true if the argument at the provided index exists and matches the provided pattern

Note: to check for argument equality, use argument_equals?/3 instead.

Link to this function

argument_matches_predicate?(zipper, index, func)

View Source
@spec argument_matches_predicate?(
  Sourceror.Zipper.t(),
  non_neg_integer(),
  (Sourceror.Zipper.t() ->
     boolean())
) :: boolean()

Returns true if the argument at the given index matches the provided predicate

Link to this function

function(zipper, atom, arity)

View Source
Link to this function

function?(zipper, name \\ :any, arity \\ :any)

View Source
@spec function?(
  Sourceror.Zipper.t(),
  name :: :any | :any_named | {module(), atom()} | :anonymous,
  arity :: :any | non_neg_integer() | [non_neg_integer()]
) :: boolean()

Returns true if the value is a function literal.

Examples:

  • fn x -> x end
  • &(&1 + &2)
  • &SomeMod.fun/2

To refine the check, you can use name and arity.

Names

  • :any - matches any function literal, named or not
  • :any_named - matches any named function literal
  • :anonymous - matches any anonymous function literal
  • {module, name} - matches a function literal with the given module and name
@spec function_call?(Sourceror.Zipper.t()) :: boolean()

Returns true if the node is a function call

Link to this function

function_call?(zipper, name, arity \\ :any)

View Source
@spec function_call?(
  Sourceror.Zipper.t(),
  atom() | {module(), atom()},
  arity :: integer() | :any | [integer()]
) :: boolean()

Returns true if the node is a function call of the given name

If an atom is provided, it only matches functions in the form of function(name).

If an {module, atom} is provided, it matches functions called on the given module, taking into account any imports or aliases.

Link to this function

get_local_function_call_name(zipper)

View Source

Gets the name of a local function call, or :error if the node is not a function call or it cannot be determined

@spec move_to_def(Sourceror.Zipper.t()) :: {:ok, Sourceror.Zipper.t()} | :error
Link to this function

move_to_def(zipper, fun, arity)

View Source
@spec move_to_def(
  Sourceror.Zipper.t(),
  fun :: atom(),
  arity :: integer() | [integer()]
) ::
  {:ok, Sourceror.Zipper.t()} | :error
Link to this function

move_to_defp(zipper, fun, arity)

View Source
@spec move_to_defp(
  Sourceror.Zipper.t(),
  fun :: atom(),
  arity :: integer() | [integer()]
) ::
  {:ok, Sourceror.Zipper.t()} | :error
Link to this function

move_to_function_call(zipper, name, arity, predicate \\ fn _ -> true end)

View Source

Moves to a function call by the given name and arity, matching the given predicate, in the current or lower scope

Link to this function

move_to_function_call_in_current_scope(zipper, name, arity, predicate \\ fn _ -> true end)

View Source

Moves to a function call by the given name and arity, matching the given predicate, in the current scope

Link to this function

move_to_nth_argument(zipper, index)

View Source
@spec move_to_nth_argument(
  Sourceror.Zipper.t(),
  non_neg_integer()
) :: {:ok, Sourceror.Zipper.t()} | :error

Moves to the nth argument of a function call.

Link to this function

update_nth_argument(zipper, index, func)

View Source
@spec update_nth_argument(
  Sourceror.Zipper.t(),
  non_neg_integer(),
  (Sourceror.Zipper.t() -> {:ok, Sourceror.Zipper.t()} | :error)
) :: {:ok, Sourceror.Zipper.t()} | :error

Updates the nth argument of a function call, leaving the zipper at the function call's node.