Repository: adobe/elixir-styler (elixir)

Issue:
Incorrect transformation
## Versions

* Elixir: Elixir 1.16.0-dev (7795adc) (compiled with Erlang/OTP 26)
* Styler: locked at 0.8.0 (styler) 32c76963

## Example Input

This code:

```elixir
  defp expand_alias({:__aliases__, _, _} = alias, env),
    do: Macro.expand(alias, %{env | function: {:beacon_live_admin, 2}})
```

Gets transformed into:

```elixir
   defp expand_alias({:__aliases__, _, _} = alias, env),
     do: alias(Macro . :expand, %{env | function: {:beacon_live_admin, 2}})
```

## Stacktrace / Current Behaviour

```
** (SyntaxError) invalid syntax found on lib/beacon/live_admin/router.ex:112:23:
     ┌─ error: lib/beacon/live_admin/router.ex:112:23
     │
 112 │     do: alias(Macro . :expand, %{env | function: {:beacon_live_admin, 2}})
     │                       ^
     │
     syntax error before: expand
```

Pull request description:
Fix module directives triggering on name colliding non-directives
Closes #57 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5869b7c..16cea3a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
 
 ## main
 
+### Fixes
+
+* ModuleDirectives doesn't mistake variables for directives (#57, h/t @leandrocp)
+
 ## v0.8.0
 
 ### Improvements (Bug Fix!?)
diff --git a/lib/style/module_directives.ex b/lib/style/module_directives.ex
index c789211..8395ef3 100644
--- a/lib/style/module_directives.ex
+++ b/lib/style/module_directives.ex
@@ -107,14 +107,14 @@ defmodule Styler.Style.ModuleDirectives do
     end
   end
 
-  def run({{def, _, _}, _} = zipper, ctx) when def in ~w(def defp defmacro defmacrop)a do
+  def run({{def, _, children}, _} = zipper, ctx) when def in ~w(def defp defmacro defmacrop)a and is_list(children) do
     # we don't want to look at import nodes like `def import(foo)`
     if def_body = zipper |> Zipper.down() |> Zipper.right(),
       do: {:cont, def_body, ctx},
       else: {:skip, zipper, ctx}
   end
 
-  def run({{directive, _, _}, _} = zipper, ctx) when directive in @directives do
+  def run({{directive, _, children}, _} = zipper, ctx) when directive in @directives and is_list(children) do
     parent = zipper |> Style.ensure_block_parent() |> Zipper.up()
     {:skip, organize_directives(parent), ctx}
   end


Test patch:
diff --git a/test/style/module_directives_test.exs b/test/style/module_directives_test.exs
index c015a6e..5be45a6 100644
--- a/test/style/module_directives_test.exs
+++ b/test/style/module_directives_test.exs
@@ -211,6 +211,10 @@ defmodule Styler.Style.ModuleDirectivesTest do
   end
 
   describe "strange parents!" do
+    test "regression: doesn't trigger on variables" do
+      assert_style("def foo(alias), do: Foo.bar(alias)")
+    end
+
     test "anon function" do
       assert_style("fn -> alias A.{C, B} end", """
       fn ->