View Source Recode.AST (Recode v0.1.4)
This module provides functions to manipulate the AST.
Link to this section Summary
Functions
Returns the infos from an AST representing an alias expression.
Concatinates the aliases of an :__aliases__ tuple.
Returns the newlines value from meta[:end_of_expression], or nil.
Returns the value from a :__block__ with a single argument.
Returns a mfa-tuple for the given .-call.
Converts AST representing a name to a string.
Puts the given value newlines under the key nevlines in
meta[:end_of_expression].
Puts the given value in the :__block__ AST.
Update a function call.
Updates the function name of a capture.
Updates the AST representing a definition.
Update a dotted function call.
Updates a spec.
Link to this section Functions
Returns the infos from an AST representing an alias expression.
The function returns 3-tuple containing the alias, the multi part and the
:as.
examples
Examples
iex> ast = quote do
...> alias Foo.Bar
...> end
iex> alias_info(ast)
{Foo.Bar, [], nil}
iex> ast = quote do
...> alias Foo.{Bar, Baz}
...> end
iex> alias_info(ast)
{Foo, [Bar, Baz], nil}
iex> ast = quote do
...> alias Foo, as: Baz
...> end
iex> alias_info(ast)
{Foo, [], Baz}
@spec aliases_concat({:__aliases__, Macro.metadata(), [atom()]}) :: module()
Concatinates the aliases of an :__aliases__ tuple.
examples
Examples
iex> aliases_concat({:__aliases__, [], [:Alpha, :Bravo]})
Alpha.Bravo
Returns the newlines value from meta[:end_of_expression], or nil.
Returns the value from a :__block__ with a single argument.
examples
Examples
iex> "[1, 2]"
...> |> Sourceror.parse_string!()
...> |> get_value()
...> |> Enum.map(&get_value/1)
[1, 2]
@spec mfa({{:., keyword(), list()}, Macro.metadata(), Macro.t()}) :: {module(), atom(), non_neg_integer()}
Returns a mfa-tuple for the given .-call.
Converts AST representing a name to a string.
This function suppresses the prefix "Elixir.".
examples
Examples
iex> name([Recode, AST])
"Recode.AST"
iex> name(Recode.AST)
"Recode.AST"
Puts the given value newlines under the key nevlines in
meta[:end_of_expression].
Puts the given value in the :__block__ AST.
examples
Examples
iex> "[1, 2]"
...> |> Sourceror.parse_string!()
...> |> get_value()
...> |> Enum.map(fn ast -> put_value(ast, "0") end)
...> |> Enum.map(&get_value/1)
["0", "0"]
Update a function call.
The keyword list updates can have the keys name, meta and args.
examples
Examples
iex> ast = quote do
...> foo(x)
...> end
iex> update_call(ast, name: :bar)
{:bar, [], [{:x, [], Recode.ASTTest}]}
Updates the function name of a capture.
Updates the AST representing a definition.
The keyword list updates can have the keys name, meta and args.
examples
Examples
iex> ast = quote do
...> def foo(x), do: x
...> end
iex> update_definition(ast, name: :bar)
{:def, [context: Recode.ASTTest, import: Kernel],
[
{:bar, [context: Recode.ASTTest], [{:x, [], Recode.ASTTest}]},
[do: {:x, [], Recode.ASTTest}]
]}
iex> update_definition(ast, meta: [])
{:def, [],
[
{:foo, [context: Recode.ASTTest], [{:x, [], Recode.ASTTest}]},
[do: {:x, [], Recode.ASTTest}]
]}
iex> update_definition(ast, args: [{:y, [], Recode.ASTTest}], meta: [])
{:def, [],
[
{:foo, [context: Recode.ASTTest], [{:y, [], Recode.ASTTest}]},
[do: {:x, [], Recode.ASTTest}]
]}
Update a dotted function call.
examples
Examples
iex> ast = quote do
...> Foo.foo(x)
...> end
iex> update_dot_call(ast, name: :bar)
{{:., [], [{:__aliases__, [alias: false], [:Foo]}, :bar]}, [], [{:x, [], Recode.ASTTest}]}
Updates a spec.
The keyword list updates can have the keys name, meta, args and
return.
examples
Examples
iex> ast = quote do
...> @spec foo(integer()) :: integer()
...> end
{:@, [context: Recode.ASTTest, import: Kernel],
[
{:spec, [context: Recode.ASTTest],
[{:"::", [], [{:foo, [], [{:integer, [], []}]}, {:integer, [], []}]}]}
]}
iex> update_spec(ast, meta: [], name: :bar, return: {:term, [], []})
{:@, [],
[
{:spec, [context: Recode.ASTTest],
[{:"::", [], [{:bar, [], [{:integer, [], []}]}, {:term, [], []}]}]}
]}