Repository: mhanberg/temple
Pull request #127 (2021-05-23 17:50:46)

Only collect slots in the root of a component


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5958719..bd0bd58 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,7 +2,9 @@
 
 ## Main
 
-Nothing Yet
+### Bug fixes
+
+- Only collect slots in the root of a component instance #127
 
 ## 0.6.0 The LiveView compatibility release!
 
diff --git a/lib/temple/parser/components.ex b/lib/temple/parser/components.ex
index 8f7166e..d5f1906 100644
--- a/lib/temple/parser/components.ex
+++ b/lib/temple/parser/components.ex
@@ -19,27 +19,31 @@ defmodule Temple.Parser.Components do
 
     {do_and_else, assigns} = Temple.Parser.Utils.consolidate_blocks(do_and_else, args)
 
-    {default_slot, named_slots} =
+    {default_slot, {_, named_slots}} =
       if children = do_and_else[:do] do
-        Macro.postwalk(
+        Macro.prewalk(
           children,
-          %{},
+          {component_module, %{}},
           fn
-            {:slot, _, [name | args]} = node, named_slots ->
+            {:c, _, [name | _]} = node, {_, named_slots} ->
+              {node, {name, named_slots}}
+
+            {:slot, _, [name | args]} = node, {^component_module, named_slots} ->
               {assigns, slot} = split_assigns_and_children(args, Macro.escape(%{}))
 
               if is_nil(slot) do
-                {node, named_slots}
+                {node, {component_module, named_slots}}
               else
-                {nil, Map.put(named_slots, name, %{assigns: assigns, slot: slot})}
+                {nil,
+                 {component_module, Map.put(named_slots, name, %{assigns: assigns, slot: slot})}}
               end
 
-            node, named_slots ->
-              {node, named_slots}
+            node, acc ->
+              {node, acc}
           end
         )
       else
-        {nil, %{}}
+        {nil, {nil, %{}}}
       end
 
     children =


Test patch:
diff --git a/test/parser/components_test.exs b/test/parser/components_test.exs
index 68a0305..c7c66fc 100644
--- a/test/parser/components_test.exs
+++ b/test/parser/components_test.exs
@@ -131,6 +131,45 @@ defmodule Temple.Parser.ComponentsTest do
                children: []
              } = ast
     end
+
+    test "slots should only be assigned to the component root" do
+      raw_ast =
+        quote do
+          c Card do
+            c Card.Footer do
+              c LinkList, socials: @user.socials do
+                "hello"
+
+                slot :default, %{text: text, url: url} do
+                  a class: "text-blue-500 hover:underline", href: url do
+                    text
+                  end
+                end
+              end
+            end
+          end
+        end
+
+      ast = Components.run(raw_ast)
+
+      assert Kernel.==(ast.slots, [])
+
+      assert %Components{
+               children: [
+                 %Components{
+                   children: [
+                     %Components{
+                       slots: [
+                         %Slottable{
+                           name: :default
+                         }
+                       ]
+                     }
+                   ]
+                 }
+               ]
+             } = ast
+    end
   end
 
   describe "Temple.Generator.to_eex/1" do