View Source Recode.Context (Recode v0.1.0)
This moudle provides functions to traverse an AST with a %Context{}
.
examples
Examples
The following example shows the %Context{}
for the definition of
MyApp.Bar.bar/1
.
iex> alias Recode.{Source, Context}
...> """
...> 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
...> """
...> |> Source.from_string()
...> |> Source.zipper()
...> |> 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{
aliases: [
{MyApp.Foo,
[
trailing_comments: [],
leading_comments: [],
end_of_expression: [newlines: 2, line: 6, column: 18],
line: 6,
column: 3
], nil}
],
assigns: %{},
definition:
{{:def, :bar, 1},
[
trailing_comments: [],
leading_comments: [],
do: [line: 8, column: 14],
end: [line: 10, column: 3],
line: 8,
column: 3
]},
imports: [],
module:
{MyApp.Bar,
[
trailing_comments: [],
leading_comments: [],
do: [line: 5, column: 21],
end: [line: 11, column: 1],
line: 5,
column: 1
]},
requirements: [],
usages: []
}
Link to this section 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
has the given value
.
Expands the module alias for the given mfa
.
Returns true
if an impl
is availbale.
Returns the current module of a context.
Returns true
if @moduledoc
has the given value
.
Returns true
if a spec
is availbale.
Traverses the given zipper
and applys fun
on each node.
Traverses the given zipper
with an acc
and applys fun
on each node.
Link to this section Types
@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, requirements: list(), spec: {term() | nil, Macro.t()} | nil, usages: list() }
@type zipper() :: Sourceror.Zipper.zipper()
Link to this section 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
has the given value
.
Usually used to check if @doc
is set to false:
Context.moduledoc?(context, false)
Expands the module alias for the given mfa
.
Returns true
if an impl
is availbale.
Returns the current module of a context.
Returns true
if @moduledoc
has the given value
.
Usually used to check if @moduldoc
is set to false:
Context.moduledoc?(context, false)
Returns true
if a spec
is availbale.
Traverses the given zipper
and applys fun
on each node.
The fun
gets the current zipper
and context
as arguments.
@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 applys fun
on each node.