View Source Sourceror.Code.Module (Sourceror v1.11.0)

Utilities for working with Elixir modules

Summary

Functions

Returns true if the zipper is at a module alias.

Checks if the value is a module that matches a given predicate.

Move to an attribute definition inside a module.

Moves the zipper to a defmodule call.

Moves the zipper to a specific defmodule call.

Moves the zipper to the body of a module that uses the provided module (or one of the provided modules).

Moves the zipper to the use statement for a provided module.

Functions

Returns true if the zipper is at a module alias.

Examples

iex> zipper = Sourceror.parse_string!("Foo.Bar") |> Sourceror.Zipper.zip()
iex> Sourceror.Code.Module.module?(zipper)
true
iex> zipper = Sourceror.parse_string!("123") |> Sourceror.Zipper.zip()
iex> Sourceror.Code.Module.module?(zipper)
false
Link to this function

module_matching?(zipper, pred)

View Source

Checks if the value is a module that matches a given predicate.

Examples

iex> zipper = Sourceror.parse_string!("Foo.Bar") |> Sourceror.Zipper.zip()
iex> Sourceror.Code.Module.module_matching?(zipper, fn mod -> mod == Foo.Bar end)
true
Link to this function

move_to_attribute_definition(zipper, name)

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

Move to an attribute definition inside a module.

Example

Given this module:

defmodule MyAppWeb.Endpoint do
  @doc "My App Endpoint"

  @session_options [
    store: :cookie,
    ...
  ]
end

You can move into @doc attribute with:

Sourceror.Code.Module.move_to_attribute_definition(zipper, :doc)

Or you can move into @session_options constant with:

Sourceror.Code.Module.move_to_attribute_definition(zipper, :session_options)

Examples

iex> zipper = Sourceror.parse_string!("defmodule Foo do\n  @doc \"Hello\"\nend") |> Sourceror.Zipper.zip()
iex> {:ok, result} = Sourceror.Code.Module.move_to_attribute_definition(zipper, :doc)
iex> match?({:@, _, [{:doc, _, _}]}, result.node)
true
Link to this function

move_to_defmodule(zipper)

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

Moves the zipper to a defmodule call.

Examples

iex> zipper = Sourceror.parse_string!("defmodule Foo do\nend") |> Sourceror.Zipper.zip()
iex> {:ok, result} = Sourceror.Code.Module.move_to_defmodule(zipper)
iex> match?({:defmodule, _, _}, result.node)
true

See also move_to_defmodule/2.

Link to this function

move_to_defmodule(zipper, module)

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

Moves the zipper to a specific defmodule call.

Examples

iex> zipper = Sourceror.parse_string!("defmodule Foo do\nend") |> Sourceror.Zipper.zip()
iex> {:ok, result} = Sourceror.Code.Module.move_to_defmodule(zipper, Foo)
iex> match?({:defmodule, _, _}, result.node)
true

See also move_to_defmodule/1.

Link to this function

move_to_module_using(zipper, one_of_modules)

View Source
@spec move_to_module_using(Sourceror.Zipper.t(), module() | [module()]) ::
  {:ok, Sourceror.Zipper.t()} | :error

Moves the zipper to the body of a module that uses the provided module (or one of the provided modules).

Examples

iex> zipper = Sourceror.parse_string!("defmodule Foo do\n  use Bar\nend") |> Sourceror.Zipper.zip()
iex> {:ok, result} = Sourceror.Code.Module.move_to_module_using(zipper, Bar)
iex> match?({:defmodule, _, _}, result.node)
true

See also move_to_use/2.

Link to this function

move_to_use(zipper, module)

View Source

Moves the zipper to the use statement for a provided module.

Examples

iex> zipper = Sourceror.parse_string!("defmodule Foo do\n  use Bar\nend") |> Sourceror.Zipper.zip()
iex> {:ok, mod} = Sourceror.Code.Module.move_to_defmodule(zipper)
iex> {:ok, body} = Sourceror.Code.Common.move_to_do_block(mod)
iex> {:ok, result} = Sourceror.Code.Module.move_to_use(body, Bar)
iex> match?({:use, _, _}, result.node)
true

See also move_to_module_using/2.