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

Fixed alias crash on unquote #545

Merged
merged 1 commit into from
Jan 9, 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
29 changes: 19 additions & 10 deletions apps/remote_control/lib/lexical/remote_control/analyzer/aliases.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,17 @@ defmodule Lexical.RemoteControl.Analyzer.Aliases do
aliases = Scope.alias_map(scope, line)

case module do
[{:__MODULE__, _, _} | suffix] ->
current_module =
aliases
|> Map.get(:__MODULE__)
|> Alias.to_module()
# unquote(__MODULE__).SubModule
[{:unquote, _, [{:__MODULE__, _, _}]} | suffix] ->
resolve_current_module(aliases, suffix)

Module.concat([current_module | suffix])
[{:__MODULE__, _, _} | suffix] ->
resolve_current_module(aliases, suffix)

[prefix | suffix] ->
case aliases do
%{^prefix => _} ->
current_module =
aliases
|> Map.get(prefix)
|> Alias.to_module()
current_module = resolve_alias(aliases, prefix, suffix)

Module.concat([current_module | suffix])

Expand All @@ -51,4 +47,17 @@ defmodule Lexical.RemoteControl.Analyzer.Aliases do
end
end
end

defp resolve_current_module(aliases, suffix) do
resolve_alias(aliases, :__MODULE__, suffix)
end

defp resolve_alias(aliases, prefix, suffix) do
current_module =
aliases
|> Map.get(prefix)
|> Alias.to_module()

Module.concat([current_module | suffix])
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -443,5 +443,22 @@ defmodule Lexical.Ast.Analysis.ImportsTest do

refute_imported(imports, Enum)
end

test "imports to the current module work in a quote block" do
imports =
~q[
defmodule Parent do
defmacro __using__(_) do
quote do
import unquote(__MODULE__).Child.ImportedModule
|
end
end
end
]
|> imports_at_cursor()

assert_imported(imports, Parent.Child.ImportedModule)
end
end
end
Loading