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.7 Fix: stringify path in Aliases #10

Merged
merged 1 commit into from
Jul 7, 2024
Merged
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
Fix: stringify path in Aliases
sovetnik committed Jul 7, 2024
commit 106fa31286f5a64e6298a0864c96e59d6ea668cc
32 changes: 21 additions & 11 deletions lib/umwelt/parser/aliases.ex
Original file line number Diff line number Diff line change
@@ -2,13 +2,13 @@ defmodule Umwelt.Parser.Aliases do
@moduledoc "Parses @attr AST"

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

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

def parse({:alias, _, [{{:., _, [left, :{}]}, _, children}]}, aliases) do
%{path: left_alias} = parse(left, aliases)
@@ -17,28 +17,38 @@ defmodule Umwelt.Parser.Aliases do
|> Enum.map(fn {:__aliases__, _, right} ->
%{
kind: :Alias,
name: List.last(right),
path: left_alias ++ right
name: module_name(right),
path: stringify_path(left_alias ++ right)
}
end)
end

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

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

def expand_module([head | rest] = module, aliases) do
matched_aliases =
aliases
|> Enum.filter(&match?(%{name: ^head}, &1))
def expand_module(module, aliases) do
[head | rest] = module |> stringify_path()

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

case Enum.filter(aliases, &match?(%{name: ^head}, &1)) do
[] ->
module

[%{path: path}] ->
path ++ rest
end
|> stringify_path()
end

defp module_name(module) do
module |> List.last() |> to_string
end

defp stringify_path(module) do
Enum.map(module, &to_string/1)
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ defmodule Umwelt.MixProject do
def project do
[
app: :umwelt,
version: "0.1.6",
version: "0.1.7",
elixir: "~> 1.15",
compilers: [:leex, :yecc] ++ Mix.compilers(),
start_permanent: Mix.env() == :prod,
19 changes: 10 additions & 9 deletions test/umwelt/parser/aliases_test.exs
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ defmodule Umwelt.Parser.AliasesTest do
"""
|> Code.string_to_quoted()

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

test "parse single named alias" do
@@ -20,7 +20,8 @@ defmodule Umwelt.Parser.AliasesTest do
"""
|> Code.string_to_quoted()

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

test "parse multi alias" do
@@ -31,28 +32,28 @@ defmodule Umwelt.Parser.AliasesTest do
|> Code.string_to_quoted()

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

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

test "aliases not match" do
module = [:Foo, :Bar]
aliases = [%{kind: :Alias, name: :Baz, path: [:Bar, :Baz]}]
assert module == Aliases.expand_module(module, aliases)
aliases = [%{kind: :Alias, name: "Baz", path: ["Bar", "Baz"]}]
assert ["Foo", "Bar"] == Aliases.expand_module(module, aliases)
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)
aliases = [%{kind: :Alias, name: "Bar", path: ["Foo", "Bar"]}]
assert ["Foo", "Bar", "Baz"] == Aliases.expand_module(module, aliases)
end
end
end
8 changes: 4 additions & 4 deletions test/umwelt/parser/def_test.exs
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ defmodule Umwelt.Parser.DefTest do
%{
body: "bar",
kind: :Variable,
type: %{name: :Baz, path: [:Bar, :Baz], kind: :Alias},
type: %{name: "Baz", path: ["Bar", "Baz"], kind: :Alias},
keyword: []
}
],
@@ -66,13 +66,13 @@ defmodule Umwelt.Parser.DefTest do
keyword: [],
kind: :Variable,
type: %{
name: :Baz,
path: [:Foo, :Bar, :Baz],
name: "Baz",
path: ["Foo", "Bar", "Baz"],
kind: :Alias
}
}
]
} == Def.parse(ast, [%{name: :Bar, path: [:Foo, :Bar], kind: :Alias}])
} == Def.parse(ast, [%{name: "Bar", path: ["Foo", "Bar"], kind: :Alias}])
end

test "match in argument" do
16 changes: 8 additions & 8 deletions test/umwelt/parser/defmodule_test.exs
Original file line number Diff line number Diff line change
@@ -285,7 +285,7 @@ defmodule Umwelt.Parser.DefmoduleTest do
calls: [
%{
arguments: [
%{name: :List, path: [:List], kind: :Alias},
%{name: "List", path: ["List"], kind: :Alias},
%{
type: %{kind: :Structure, type: :list},
values: [
@@ -382,7 +382,7 @@ defmodule Umwelt.Parser.DefmoduleTest do
calls: [
%{
arguments: [
%{name: :Mailer, path: [:Swoosh, :Mailer], kind: :Alias},
%{name: "Mailer", path: ["Swoosh", "Mailer"], kind: :Alias},
%{
type: %{kind: :Structure, type: :list},
values: [
@@ -436,7 +436,7 @@ defmodule Umwelt.Parser.DefmoduleTest do
calls: [
%{
arguments: [
%{name: :Feature, path: [:Feature], kind: :Alias},
%{name: "Feature", path: ["Feature"], kind: :Alias},
%{
type: %{kind: :Structure, type: :list},
values: [
@@ -519,7 +519,7 @@ defmodule Umwelt.Parser.DefmoduleTest do
context: ["Feature"]
},
%{
arguments: [%{name: :Feature, path: [:Feature], kind: :Alias}],
arguments: [%{name: "Feature", path: ["Feature"], kind: :Alias}],
body: "require",
kind: :Call
}
@@ -558,13 +558,13 @@ defmodule Umwelt.Parser.DefmoduleTest do
%{
arguments: [
%{
type: %{name: :Bar, path: [:Foo, :Bar], kind: :Alias},
type: %{name: "Bar", path: ["Foo", "Bar"], kind: :Alias},
body: "bar",
kind: :Variable,
keyword: []
},
%{
type: %{name: :Baz, path: [:Foo, :Baz], kind: :Alias},
type: %{name: "Baz", path: ["Foo", "Baz"], kind: :Alias},
body: "baz",
kind: :Variable,
keyword: []
@@ -602,13 +602,13 @@ defmodule Umwelt.Parser.DefmoduleTest do
%{
arguments: [
%{
type: %{name: :Bar, path: [:Foo, :Bar], kind: :Alias},
type: %{name: "Bar", path: ["Foo", "Bar"], kind: :Alias},
body: "bar",
kind: :Variable,
keyword: []
},
%{
type: %{name: :Baz, path: [:Foo, :Baz], kind: :Alias},
type: %{name: "Baz", path: ["Foo", "Baz"], kind: :Alias},
body: "baz",
kind: :Variable,
keyword: []
12 changes: 6 additions & 6 deletions test/umwelt/parser/macro_test.exs
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ defmodule Umwelt.Parser.MacroTest do
term: %{
kind: :Value,
keyword: [],
type: %{name: :Bar, path: [:Bar], kind: :Alias}
type: %{name: "Bar", path: ["Bar"], kind: :Alias}
}
} == Macro.parse(ast, [])
end
@@ -69,7 +69,7 @@ defmodule Umwelt.Parser.MacroTest do
kind: :Match,
term: %{
kind: :Value,
type: %{name: :Baz, path: [:Bar, :Baz], kind: :Alias},
type: %{name: "Baz", path: ["Bar", "Baz"], kind: :Alias},
keyword: []
}
} == Macro.parse(ast, [])
@@ -83,10 +83,10 @@ defmodule Umwelt.Parser.MacroTest do
kind: :Match,
term: %{
kind: :Value,
type: %{name: :Baz, path: [:Foo, :Bar, :Baz], kind: :Alias},
type: %{name: "Baz", path: ["Foo", "Bar", "Baz"], kind: :Alias},
keyword: []
}
} == Macro.parse(ast, [%{name: :Bar, path: [:Foo, :Bar], kind: :Alias}])
} == Macro.parse(ast, [%{name: "Bar", path: ["Foo", "Bar"], kind: :Alias}])
end
end

@@ -169,7 +169,7 @@ defmodule Umwelt.Parser.MacroTest do
kind: :Match,
term: %{
kind: :Value,
type: %{name: :Foo, path: [:Foo], kind: :Alias},
type: %{name: "Foo", path: ["Foo"], kind: :Alias},
keyword: []
}
} == Macro.parse(ast, [])
@@ -181,7 +181,7 @@ defmodule Umwelt.Parser.MacroTest do
assert %{
keyword: [],
kind: :Value,
type: %{name: :Foo, path: [:Foo], kind: :Alias}
type: %{name: "Foo", path: ["Foo"], kind: :Alias}
} == Macro.parse(ast, [])
end

2 changes: 1 addition & 1 deletion test/umwelt/parser/operator_test.exs
Original file line number Diff line number Diff line change
@@ -117,7 +117,7 @@ defmodule Umwelt.Parser.OperatorTest do
kind: :Match,
term: %{
kind: :Value,
type: %{name: :Bar, path: [:Bar], kind: :Alias},
type: %{name: "Bar", path: ["Bar"], kind: :Alias},
keyword: []
}
} == Operator.parse(ast, [])
2 changes: 1 addition & 1 deletion test/umwelt/parser/structure_test.exs
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ defmodule Umwelt.Parser.StructureTest do
}
],
kind: :Value,
type: %{name: :Foobar, path: [:Foobar], kind: :Alias}
type: %{name: "Foobar", path: ["Foobar"], kind: :Alias}
} == Structure.parse(ast, [])
end

2 changes: 1 addition & 1 deletion test/umwelt/parser/tuple_test.exs
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ defmodule Umwelt.Parser.TupleTest do
term: %{
keyword: [],
kind: :Value,
type: %{name: :Result, path: [:Result], kind: :Alias}
type: %{name: "Result", path: ["Result"], kind: :Alias}
}
}
]