Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.1.8 Parse alias with _module_ #11

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ umwelt-*.tar

# Temporary files, for example, from tests.
/tmp/
/trash/
51 changes: 31 additions & 20 deletions lib/umwelt/parser/aliases.ex
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
defmodule Umwelt.Parser.Aliases do
@moduledoc "Parses @attr AST"

def parse({:alias, _, [{:__aliases__, _, module}]}, aliases),
do: %{kind: :Alias, name: module_name(module), path: expand_module(module, aliases)}
def parse({:alias, _, [{:__aliases__, _, module}]}, aliases, context),
do: %{
kind: :Alias,
name: module_name(module, context),
path: full_path(module, aliases, context)
}

def parse(
{:alias, _, [{:__aliases__, _, module}, [as: {:__aliases__, _, alias_name}]]},
aliases
aliases,
context
),
do: %{kind: :Alias, name: module_name(alias_name), path: expand_module(module, aliases)}
do: %{
kind: :Alias,
name: module_name(alias_name, context),
path: full_path(module, aliases, context)
}

def parse({:alias, _, [{{:., _, [left, :{}]}, _, children}]}, aliases) do
%{path: left_alias} = parse(left, aliases)
def parse({:alias, _, [{{:., _, [left, :{}]}, _, children}]}, aliases, context) do
%{path: left_alias} = parse(left, aliases, context)

children
|> Enum.map(fn {:__aliases__, _, right} ->
%{
kind: :Alias,
name: module_name(right),
name: module_name(right, context),
path: stringify_path(left_alias ++ right)
}
end)
end

def parse({:__aliases__, _, module}, aliases),
do: %{kind: :Alias, name: module_name(module), path: expand_module(module, aliases)}
def parse({:__aliases__, _, module}, aliases, context),
do: %{
kind: :Alias,
name: module_name(module, context),
path: full_path(module, aliases, context)
}

def expand_module(module, []),
do: module |> stringify_path()
def full_path(module, [], context),
do: module |> expand_module(context) |> stringify_path()

def expand_module(module, aliases) do
def full_path(module, aliases, context) do
module = module |> expand_module(context)
[head | rest] = module |> stringify_path()

# matched_aliases = Enum.filter(aliases, &match?(%{name: ^head}, &1))

case Enum.filter(aliases, &match?(%{name: ^head}, &1)) do
[] ->
module
Expand All @@ -44,11 +56,10 @@ defmodule Umwelt.Parser.Aliases do
|> stringify_path()
end

defp module_name(module) do
module |> List.last() |> to_string
end
defp module_name(module, context),
do: module |> expand_module(context) |> List.last() |> to_string

defp stringify_path(module) do
Enum.map(module, &to_string/1)
end
defp expand_module([{:__MODULE__, _, nil} | rest], context), do: context ++ rest
defp expand_module(module, _context), do: module
defp stringify_path(module), do: Enum.map(module, &to_string/1)
end
10 changes: 5 additions & 5 deletions lib/umwelt/parser/defmodule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ defmodule Umwelt.Parser.Defmodule do

def parse_block({:__block__, _, block_children}, context) do
block_children
|> Enum.map(&parse_block_child(&1, context, aliases(block_children)))
|> Enum.map(&parse_block_child(&1, context, aliases(block_children, context)))
|> Enum.reject(&is_nil(&1))
end

def parse_block({:@, _, _} = ast, context),
do: Parser.Attrs.parse(ast, context)

def parse_block({term, _, block_children} = ast, _context)
def parse_block({term, _, block_children} = ast, context)
when term in ~w|def import require use|a,
do: [Parser.parse(ast, aliases(block_children))]
do: [Parser.parse(ast, aliases(block_children, context))]

def parse_block({term, _, _} = ast, context)
when term in [:@, :defguard, :defmodule, :defstruct],
Expand Down Expand Up @@ -84,10 +84,10 @@ defmodule Umwelt.Parser.Defmodule do
nil
end

defp aliases(children) do
defp aliases(children, context) do
Enum.flat_map(children, fn
{:alias, _, _} = ast ->
Parser.Aliases.parse(ast, [])
Parser.Aliases.parse(ast, [], context)
|> List.wrap()

_other ->
Expand Down
2 changes: 1 addition & 1 deletion lib/umwelt/parser/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Umwelt.Parser.Macro do
do: Parser.Literal.parse(ast)

def parse({:__aliases__, _, _} = ast, aliases),
do: Parser.Aliases.parse(ast, aliases)
do: Parser.Aliases.parse(ast, aliases, [])

def parse({:defmodule, _, _} = ast, context) when is_macro(ast),
do: Parser.Defmodule.parse(ast, context)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Umwelt.MixProject do
def project do
[
app: :umwelt,
version: "0.1.7",
version: "0.1.8",
elixir: "~> 1.15",
compilers: [:leex, :yecc] ++ Mix.compilers(),
start_permanent: Mix.env() == :prod,
Expand Down
87 changes: 54 additions & 33 deletions test/umwelt/parser/aliases_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,78 @@ defmodule Umwelt.Parser.AliasesTest do

alias Umwelt.Parser.Aliases

test "parse single alias" do
{:ok, ast} =
"""
alias Foo.Bar
"""
|> Code.string_to_quoted()

assert %{kind: :Alias, name: "Bar", path: ["Foo", "Bar"]} == Aliases.parse(ast, [])
end
describe "parse in context" do
test "parse single alias" do
{:ok, ast} =
"""
alias Foo.Bar
"""
|> Code.string_to_quoted()

test "parse single named alias" do
{:ok, ast} =
"""
alias Estructura.Config, as: Cfg
"""
|> Code.string_to_quoted()
assert %{kind: :Alias, name: "Bar", path: ["Foo", "Bar"]} == Aliases.parse(ast, [], ["Woo"])
end

assert %{kind: :Alias, name: "Cfg", path: ["Estructura", "Config"]} ==
Aliases.parse(ast, [])
end
test "parse single named alias" do
{:ok, ast} =
"""
alias Estructura.Config, as: Cfg
"""
|> Code.string_to_quoted()

test "parse multi alias" do
{:ok, ast} =
"""
alias Foo.{Bar, Baz}
"""
|> Code.string_to_quoted()

assert [
%{name: "Bar", path: ["Foo", "Bar"], kind: :Alias},
%{name: "Baz", path: ["Foo", "Baz"], kind: :Alias}
] == Aliases.parse(ast, [])
assert %{kind: :Alias, name: "Cfg", path: ["Estructura", "Config"]} ==
Aliases.parse(ast, [], ["Woo"])
end

test "parse multi alias" do
{:ok, ast} =
"""
alias Foo.{Bar, Baz}
"""
|> Code.string_to_quoted()

assert [
%{name: "Bar", path: ["Foo", "Bar"], kind: :Alias},
%{name: "Baz", path: ["Foo", "Baz"], kind: :Alias}
] == Aliases.parse(ast, [], ["Woo"])
end

test "parse multi alias and _module_" do
{:ok, ast} =
"""
alias __MODULE__.Foo.{Bar, Baz}
"""
|> Code.string_to_quoted()

assert [
%{name: "Bar", path: ["Woo", "Foo", "Bar"], kind: :Alias},
%{name: "Baz", path: ["Woo", "Foo", "Baz"], kind: :Alias}
] == Aliases.parse(ast, [], ["Woo"])
end
end

describe "expandind modules via aliases" do
describe "full module path" do
test "nothing to expand" do
module = [:Foo]
aliases = []
assert ["Foo"] == Aliases.expand_module(module, aliases)
assert ["Foo"] == Aliases.full_path(module, aliases, ["Woo"])
end

test "aliases not match" do
module = [:Foo, :Bar]
aliases = [%{kind: :Alias, name: "Baz", path: ["Bar", "Baz"]}]
assert ["Foo", "Bar"] == Aliases.expand_module(module, aliases)
assert ["Foo", "Bar"] == Aliases.full_path(module, aliases, ["Woo"])
end

test "aliases match and module expanded" do
module = [:Bar, :Baz]
aliases = [%{kind: :Alias, name: "Bar", path: ["Foo", "Bar"]}]
assert ["Foo", "Bar", "Baz"] == Aliases.expand_module(module, aliases)
assert ["Foo", "Bar", "Baz"] == Aliases.full_path(module, aliases, ["Woo"])
end

test "module expanded with context" do
module = [{:__MODULE__, [], nil}, :Bar, :Baz]
aliases = [%{kind: :Alias, name: "Bar", path: ["Foo", "Bar"]}]
assert ["Woo", "Bar", "Baz"] == Aliases.full_path(module, aliases, ["Woo"])
end
end
end