Skip to content

Commit c3f2011

Browse files
fix: prevent duplicate 'live' directories for modules with Live namespace (#330)
* fix: prevent duplicate 'live' directories for modules with Live namespace Fixes #329
1 parent 961b665 commit c3f2011

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

lib/igniter/extensions/phoenix.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ defmodule Igniter.Extensions.Phoenix do
2020
split = Module.split(module)
2121

2222
cond do
23-
String.ends_with?(Enum.at(split, 1) || "", "Live") ->
23+
# Check if this is a LiveView/LiveComponent module but NOT if "Live" is just a namespace
24+
String.ends_with?(Enum.at(split, 1) || "", "Live") &&
25+
# Exclude the case where element 1 is exactly "Live" (namespace usage)
26+
Enum.at(split, 1) != "Live" ->
2427
[base | rest] = split
2528

2629
{:ok,

test/igniter/extensions/phoenix_test.exs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,54 @@ defmodule Igniter.Extensions.PhoenixTest do
7878
{:ok, "test_web/controllers/foo_json.ex"}
7979
end
8080
end
81+
82+
describe "Live namespace handling" do
83+
test "does not duplicate 'live' directory for modules with Live namespace segment" do
84+
igniter =
85+
test_project()
86+
|> Igniter.Project.IgniterConfig.add_extension(Igniter.Extensions.Phoenix)
87+
88+
module_name = MyApp.Live.Dashboard.TestLive
89+
90+
igniter =
91+
Igniter.Project.Module.create_module(igniter, module_name, """
92+
@moduledoc "Test module"
93+
def hello, do: :world
94+
""")
95+
96+
{:ok, {_igniter, source, _zipper}} =
97+
Igniter.Project.Module.find_module(igniter, module_name)
98+
99+
actual_path = Rewrite.Source.get(source, :path)
100+
101+
refute actual_path =~ ~r/live\/live/,
102+
"Module path should not contain duplicate 'live/live' directories. Got: #{actual_path}"
103+
104+
assert actual_path == "lib/my_app/live/dashboard/test_live.ex"
105+
end
106+
107+
test "correctly handles LiveView modules with Web prefix" do
108+
igniter =
109+
test_project()
110+
|> Igniter.Project.IgniterConfig.add_extension(Igniter.Extensions.Phoenix)
111+
112+
module_name = TestWeb.DashboardLive
113+
114+
igniter =
115+
Igniter.Project.Module.create_module(igniter, module_name, """
116+
use TestWeb, :live_view
117+
118+
def render(assigns) do
119+
~H"<div>Test</div>"
120+
end
121+
""")
122+
123+
{:ok, {_igniter, source, _zipper}} =
124+
Igniter.Project.Module.find_module(igniter, module_name)
125+
126+
actual_path = Rewrite.Source.get(source, :path)
127+
128+
assert actual_path == "lib/test_web/live/dashboard_live.ex"
129+
end
130+
end
81131
end

0 commit comments

Comments
 (0)