Recode.Context (Recode v0.8.0)

View Source

This module provides functions to traverse an AST with a %Context{}.

Examples

The following example shows the %Context{} for the definition of MyApp.Bar.bar/1.

iex> alias Rewrite.Source
...> alias Recode.Context
...>
...> code = """
...> defmodule MyApp.Foo do
...>   def foo, do: :foo
...> end
...>
...> defmodule MyApp.Bar do
...>   alias MyApp.Foo
...>
...>   def bar(x) do
...>     {Foo.foo(), x}
...>   end
...> end
...> """
...>
...> context =
...>   code
...>   |> Source.Ex.from_string()
...>   |> Source.get(:quoted)
...>   |> Zipper.zip()
...>   |> Context.traverse(nil, fn
...>     zipper, context, nil ->
...>       case context.definition do
...>         {{:def, :bar, 1}, _meta} -> {zipper, context, context}
...>         _def -> {zipper, context, nil}
...>       end
...>     zipper, context, acc ->
...>       {zipper, context, acc}
...>   end)
...>   |> elem(1)
...>
...> context |> Map.from_struct() |> Map.keys() |> Enum.sort()
[
  :aliases,
  :assigns,
  :definition,
  :doc,
  :impl,
  :imports,
  :module,
  :moduledoc,
  :node,
  :requirements,
  :spec,
  :usages
]

Summary

Functions

Assigns the given value under key to the context.

Merges the given map to the assigns of the context.

Returns true if definition satisfies the assumption.

Returns true if @doc is available and not set to @doc false.

Returns true if @doc false.

Expands the module alias for the given mfa.

Returns true if an impl is available.

Returns the current module of a context.

Returns true if @moduledoc is available and not set to @moduledoc false.

Returns true if @moduledoc false.

Returns true if a spec is available.

Traverses the given zipper and applies fun on each node.

Traverses the given zipper with an acc and applies fun on each node.

Types

t()

@type t() :: %Recode.Context{
  aliases: list(),
  assigns: map(),
  definition: term(),
  doc: {term() | nil, Macro.t()} | nil,
  impl: {term() | nil, Macro.t()} | nil,
  imports: list(),
  module: term() | nil,
  moduledoc: Macro.t() | nil,
  node: term() | nil,
  requirements: list(),
  spec: {term() | nil, Macro.t()} | nil,
  usages: list()
}

zipper()

@type zipper() :: Sourceror.Zipper.t()

Functions

assign(context, key, value)

@spec assign(t(), atom(), term()) :: t()

Assigns the given value under key to the context.

assigns(context, map)

@spec assigns(t(), map()) :: t()

Merges the given map to the assigns of the context.

definition?(context, atom)

@spec definition?(t(), :public | :visible) :: boolean()

Returns true if definition satisfies the assumption.

doc?(context)

@spec doc?(t()) :: boolean()

Returns true if @doc is available and not set to @doc false.

doc?(context, bool)

@spec doc?(t(), false) :: boolean()

Returns true if @doc false.

expand_mfa(context, arg)

@spec expand_mfa(t(), mfa()) :: {:ok, mfa()} | :error

Expands the module alias for the given mfa.

impl?(context)

@spec impl?(t()) :: boolean()

Returns true if an impl is available.

module(context)

@spec module(t()) :: module() | nil

Returns the current module of a context.

moduledoc?(context)

@spec moduledoc?(t()) :: boolean()

Returns true if @moduledoc is available and not set to @moduledoc false.

moduledoc?(context, bool)

@spec moduledoc?(t(), false) :: boolean()

Returns true if @moduledoc false.

spec?(context)

@spec spec?(t()) :: boolean()

Returns true if a spec is available.

traverse(zipper, fun)

@spec traverse(zipper(), fun) :: zipper() when fun: (zipper(), t() -> {zipper(), t()})

Traverses the given zipper and applies fun on each node.

The fun gets the current zipper and context as arguments.

traverse(zipper, acc, fun)

@spec traverse(zipper(), acc, fun) :: {zipper(), acc}
when acc: term(), fun: (zipper(), t(), acc -> {zipper(), t(), acc})

Traverses the given zipper with an acc and applies fun on each node.