Capstone.Vendor.Sourceror.Code.Function (Capstone v0.33.3)

Copy Markdown View Source

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 and arity of a local function call.

Gets the name of a local function call.

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

append_argument(zipper, value)

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

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

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("foo(1, 2)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> {:ok, zipper} = Capstone.Vendor.Sourceror.Code.Function.append_argument(zipper, 3)
iex> Capstone.Vendor.Sourceror.to_string(zipper.node)
"foo(1, 2, 3)"

argument_equals?(zipper, index, term)

@spec argument_equals?(Capstone.Vendor.Sourceror.Zipper.t(), integer(), any()) ::
  boolean()

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

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("foo(1, 2, 3)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.Function.argument_equals?(zipper, 0, 1)
true
iex> Capstone.Vendor.Sourceror.Code.Function.argument_equals?(zipper, 0, 2)
false

See also argument_matches_predicate?/3.

argument_matches_pattern?(zipper, index, pattern)

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

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("foo(1, 2, 3)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.Function.argument_matches_pattern?(zipper, 1, {:__block__, _, [2]})
true

See also argument_matches_predicate?/3.

argument_matches_predicate?(zipper, index, func)

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

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("foo(1, 2, 3)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.Function.argument_matches_predicate?(zipper, 0, fn z ->
...>   match?({:__block__, _, [1]}, z.node)
...> end)
true

See also argument_equals?/3.

function(zipper, atom, arity)

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

@spec function?(
  Capstone.Vendor.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

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("fn x -> x end") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.Function.function?(zipper, :anonymous, 1)
true
iex> zipper = Capstone.Vendor.Sourceror.parse_string!("&(&1 + &2)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.Function.function?(zipper, :anonymous, 2)
true

function_call?(zipper)

@spec function_call?(Capstone.Vendor.Sourceror.Zipper.t()) :: boolean()

Returns true if the node is a function call.

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("foo(1, 2)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.Function.function_call?(zipper)
true
iex> zipper = Capstone.Vendor.Sourceror.parse_string!("1 + 2") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.Function.function_call?(zipper)
true

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

@spec function_call?(
  Capstone.Vendor.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 aliases.

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("foo(1, 2)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.Function.function_call?(zipper, :foo, 2)
true
iex> Capstone.Vendor.Sourceror.Code.Function.function_call?(zipper, :bar, 2)
false

See also function_call?/1.

get_local_function_call(zipper)

@spec get_local_function_call(Capstone.Vendor.Sourceror.Zipper.t()) ::
  {:ok, {atom(), non_neg_integer()}} | :error

Gets the name and arity of a local function call.

Returns :error if the node is not a function call or cannot be determined.

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("foo(1, 2)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.Function.get_local_function_call(zipper)
{:ok, {:foo, 2}}

See also get_local_function_call_name/1.

get_local_function_call_name(zipper)

@spec get_local_function_call_name(Capstone.Vendor.Sourceror.Zipper.t()) ::
  {:ok, atom()} | :error

Gets the name of a local function call.

Returns :error if the node is not a function call or cannot be determined.

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("foo(1, 2)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.Function.get_local_function_call_name(zipper)
{:ok, :foo}

move_to_def(zipper)

move_to_def(zipper, fun, arity)

@spec move_to_def(
  Capstone.Vendor.Sourceror.Zipper.t(),
  fun :: atom(),
  arity :: integer() | [integer()]
) ::
  {:ok, Capstone.Vendor.Sourceror.Zipper.t()} | :error

move_to_defp(zipper, fun, arity)

@spec move_to_defp(
  Capstone.Vendor.Sourceror.Zipper.t(),
  fun :: atom(),
  arity :: integer() | [integer()]
) :: {:ok, Capstone.Vendor.Sourceror.Zipper.t()} | :error

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

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

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("bar = foo(1, 2)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> {:ok, result} = Capstone.Vendor.Sourceror.Code.Function.move_to_function_call(zipper, :foo, 2)
iex> match?({:foo, _, _}, result.node)
true

See also move_to_function_call_in_current_scope/4.

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

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

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("bar = 1\nfoo(1, 2)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> {:ok, result} = Capstone.Vendor.Sourceror.Code.Function.move_to_function_call_in_current_scope(zipper, :foo, 2)
iex> match?({:foo, _, _}, result.node)
true

See also move_to_function_call/4.

move_to_nth_argument(zipper, index)

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

Moves to the nth argument of a function call.

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("foo(1, 2, 3)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> {:ok, result} = Capstone.Vendor.Sourceror.Code.Function.move_to_nth_argument(zipper, 1)
iex> match?({:__block__, _, [2]}, result.node)
true

See also update_nth_argument/3.

update_nth_argument(zipper, index, func)

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

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("foo(1, 2, 3)") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> {:ok, zipper} = Capstone.Vendor.Sourceror.Code.Function.update_nth_argument(zipper, 1, fn z ->
...>   {:ok, Capstone.Vendor.Sourceror.Code.Common.replace_code(z, "99")}
...> end)
iex> Capstone.Vendor.Sourceror.to_string(zipper.node) |> String.contains?("99")
true

See also move_to_nth_argument/2.