This repository has been archived on 2023-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
temple/lib/temple/ast/anonymous_functions.ex

37 lines
829 B
Elixir

defmodule Temple.Ast.AnonymousFunctions do
@moduledoc false
@behaviour Temple.Parser
use TypedStruct
typedstruct do
field :elixir_ast, Macro.t()
field :children, [map()]
end
@impl true
def applicable?({_, _, args}) do
import Temple.Ast.Utils, only: [split_args: 1]
args
|> split_args()
|> elem(1)
|> Enum.any?(fn x -> match?({:fn, _, _}, x) end)
end
def applicable?(_), do: false
@impl true
def run({_name, _, args} = expression) do
{_do_and_else, args} = Temple.Ast.Utils.split_args(args)
{_args, func_arg, _args2} = Temple.Ast.Utils.split_on_fn(args, {[], nil, []})
{_func, _, [{_arrow, _, [[{_arg, _, _}], block]}]} = func_arg
children = Temple.Parser.parse(block)
Temple.Ast.new(__MODULE__, elixir_ast: expression, children: children)
end
end