Helper.Macro (helper v1.0.1) View Source

Common functions for manipulating ast / quoted code

Link to this section Summary

Types

t()

AST type

Functions

Parses the AST and returns the node and value in a keyword list

Returns the module from the AST alias.

Parses the AST and returns the node and value in a map

Split the AST with nodes matching the values in the first value and the remain in the second value

Link to this section Types

Specs

t() :: Macro.t()

AST type

Link to this section Functions

Specs

ast2kw(t()) :: keyword()

Parses the AST and returns the node and value in a keyword list

## Example

    iex> ast2kw([{:primary_key, [], [:id]}, {:timestamps, [], [[type: :utc_datetime_usec]]}])
    [primary_key: :id, timestamps: [type: :utc_datetime_usec]]

Specs

ast2mod!(module() | t()) :: module() | no_return()

Returns the module from the AST alias.

## Example

    iex> ast2mod!({:__aliases__, [], [Elixir, String]})
    Elixir.String

    iex> ast2mod!([{:__aliases__, [], [Elixir, String]}])
    Elixir.String

    iex> ast2mod!(String)
    String

    iex> ast2mod!("String")
    ** (ArgumentError) "String": Invalid value for module

Specs

attr2map(t()) :: map()

Parses the AST and returns the node and value in a map

## Example

    iex> attr2map([{:source, [], ["embedded"]}])
    %{source: "embedded"}

    iex> attr2map([{:repo, [], [{Helper.Example.Repo, []}]}])
    %{repo: {Helper.Example.Repo, []}}

    iex> attr2map([{:name, [], [{:__aliases__, [], [Helper, Example, User]}]}])
    %{name: Helper.Example.User}

Specs

split(t(), [atom()] | atom()) :: {t(), t()}

Split the AST with nodes matching the values in the first value and the remain in the second value

## Example

    iex> split({:primary_key, [], []}, :primary_key)
    {[{:primary_key, [], []}], []}

    iex> split({:timestamps, [], []}, :primary_key)
    {[], [{:timestamps, [], []}]}

    iex> split([{:primary_key, [], []}], [:timestamps, :primary_key])
    {[{:primary_key, [], []}], []}