-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename Dealias to AliasEnv, now that elixir 1.17 gave me much better …
…words for everything
- Loading branch information
Showing
3 changed files
with
55 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule Styler.AliasEnv do | ||
@moduledoc """ | ||
A datastructure for maintaining something like compiler alias state when traversing AST. | ||
Not anywhere as correct as what the compiler gives us, but close enough for open source work. | ||
A `dealias` is a map from what an alias's `as` to its resolution in a context. In other words | ||
alias Foo.Bar | ||
would create the dealias map | ||
%{:Bar => [:Foo, :Bar]} | ||
""" | ||
def define(dealiases \\ %{}, ast) | ||
def define(d, list) when is_list(list), do: Enum.reduce(list, d, &define(&2, &1)) | ||
def define(d, {:alias, _, [{:__aliases__, _, aliases}]}), do: do_define(d, aliases, List.last(aliases)) | ||
def define(d, {:alias, _, [{:__aliases__, _, aliases}, [{_as, {:__aliases__, _, [as]}}]]}), do: do_define(d, aliases, as) | ||
# `alias __MODULE__` or other oddities i'm not bothering to get right | ||
def define(dealiases, {:alias, _, _}), do: dealiases | ||
|
||
defp do_define(dealiases, modules, as), do: Map.put(dealiases, as, do_expand(dealiases, modules)) | ||
|
||
# no need to traverse ast if there are no aliases | ||
def expand(dealiases, ast) when map_size(dealiases) == 0, do: ast | ||
|
||
def expand(dealiases, {:alias, m, [{:__aliases__, m_, modules} | rest]}), | ||
do: {:alias, m, [{:__aliases__, m_, do_expand(dealiases, modules)} | rest]} | ||
|
||
def expand(dealiases, ast) do | ||
Macro.prewalk(ast, fn | ||
{:__aliases__, meta, modules} -> {:__aliases__, meta, do_expand(dealiases, modules)} | ||
ast -> ast | ||
end) | ||
end | ||
|
||
# if the list of modules is itself already aliased, dealias it with the compound alias | ||
# given: | ||
# alias Foo.Bar | ||
# Bar.Baz.Bop.baz() | ||
# | ||
# lifting Bar.Baz.Bop should result in: | ||
# alias Foo.Bar | ||
# alias Foo.Bar.Baz.Bop | ||
# Bop.baz() | ||
defp do_expand(dealiases, [first | rest] = modules) do | ||
if dealias = dealiases[first], do: dealias ++ rest, else: modules | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters