Skip to content

Commit

Permalink
Support use macro (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
kieraneglin authored Jul 7, 2024
1 parent 7104256 commit 751a466
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
21 changes: 21 additions & 0 deletions fixtures/macro.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Rewire.Macro do
defmacro __using__(opts) do
case opts do
[use_alias: true] ->
quote do
alias Rewire.Hello
alias Rewire.Macro
end

_ ->
quote do
import Rewire.Hello
import Rewire.Macro
end
end
end

def good_afternoon do
"good afternoon"
end
end
11 changes: 11 additions & 0 deletions fixtures/module_with_macro.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Rewire.ModuleWithMacro do
use Rewire.Macro

def hello_passthrough do
hello()
end

def good_afternoon_passthrough do
good_afternoon()
end
end
11 changes: 11 additions & 0 deletions fixtures/module_with_macro_with_args.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Rewire.ModuleWithMacroWithArgs do
use Rewire.Macro, use_alias: true

def hello_passthrough do
Hello.hello()
end

def good_afternoon_passthrough do
Macro.good_afternoon()
end
end
8 changes: 8 additions & 0 deletions lib/rewire/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ defmodule Rewire.Module do
)
end

defp rewrite(use_ast = {:use, _l1, [{:__aliases__, _l2, _macro_ast} | _args]}, acc) do
{:__block__, [], [{:__block__, [], [req, using]}]} = Macro.expand(use_ast, __ENV__)
{:require, _counter_ctx, [required_module]} = req
env = %{__ENV__ | requires: [required_module] ++ __ENV__.requires}

{Macro.expand(using, env), acc}
end

# Removes a single alias (ie `alias A.A`) pointing to an overriden module dependency. Keeps the others.
defp rewrite(
expr = {:alias, _, [{:__aliases__, _, module_ast}]},
Expand Down
10 changes: 10 additions & 0 deletions test/rewire_alias_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,15 @@ defmodule RewireAliasTest do
assert ModuleWithDependency.bye() == "au revoir"
end
end

test "works for macro" do
rewire Rewire.ModuleWithMacro, Hello: Bonjour, Macro: BonApresMidi
assert ModuleWithMacro.hello_passthrough() == "bonjour"
assert ModuleWithMacro.good_afternoon_passthrough() == "bon apres-midi"

rewire Rewire.ModuleWithMacroWithArgs, Hello: Bonjour, Macro: BonApresMidi
assert ModuleWithMacroWithArgs.hello_passthrough() == "bonjour"
assert ModuleWithMacroWithArgs.good_afternoon_passthrough() == "bon apres-midi"
end
end
end
4 changes: 4 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ defmodule AuRevoir do
def bye(), do: "au revoir"
end

defmodule BonApresMidi do
def good_afternoon(), do: "bon apres-midi"
end

Mox.defmock(HelloMock, for: Rewire.Hello)

defmodule TestHelpers do
Expand Down

0 comments on commit 751a466

Please sign in to comment.