Skip to content

Commit

Permalink
fix: web_module/1 duplicating Web
Browse files Browse the repository at this point in the history
Close #219
  • Loading branch information
leandrocp committed Feb 6, 2025
1 parent 8b351e1 commit 0dc4f85
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/igniter/libs/phoenix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@ defmodule Igniter.Libs.Phoenix do
@spec web_module_name() :: module()
@deprecated "Use `web_module/0` instead."
def web_module_name do
Module.concat([inspect(Igniter.Project.Module.module_name_prefix(Igniter.new())) <> "Web"])
web_module(Igniter.new())
end

@doc """
Returns the web module name for the current app
"""
@spec web_module(Igniter.t()) :: module()
def web_module(igniter) do
Module.concat([inspect(Igniter.Project.Module.module_name_prefix(igniter)) <> "Web"])
prefix =
igniter
|> Igniter.Project.Module.module_name_prefix()
|> inspect()

if String.ends_with?(prefix, "Web") do
Module.concat([prefix])
else
Module.concat([prefix <> "Web"])
end
end

@doc "Returns `true` if the module is a Phoenix HTML module"
Expand Down Expand Up @@ -66,18 +75,15 @@ defmodule Igniter.Libs.Phoenix do
@spec web_module_name(String.t()) :: module()
@deprecated "Use `web_module_name/2` instead."
def web_module_name(suffix) do
Module.concat(
inspect(Igniter.Project.Module.module_name_prefix(Igniter.new())) <> "Web",
suffix
)
Module.concat(web_module(Igniter.new()), suffix)
end

@doc """
Generates a module name that lives in the Web directory of the current app.
"""
@spec web_module_name(Igniter.t(), String.t()) :: module()
def web_module_name(igniter, suffix) do
Module.concat(inspect(Igniter.Project.Module.module_name_prefix(igniter)) <> "Web", suffix)
Module.concat(web_module(igniter), suffix)
end

@doc "Gets the list of endpoints that use a given router"
Expand Down Expand Up @@ -555,7 +561,7 @@ defmodule Igniter.Libs.Phoenix do
end)
end

@doc "Moves to the use statement in a module that matches `use TheirWebModule, :router`"
@doc "Moves to the use statement in a module that matches `use TheirAppWeb, :router`"
@spec move_to_router_use(Igniter.t(), Sourceror.Zipper.t()) ::
:error | {:ok, Sourceror.Zipper.t()}
def move_to_router_use(igniter, zipper) do
Expand All @@ -564,7 +570,7 @@ defmodule Igniter.Libs.Phoenix do
Igniter.Code.Function.argument_equals?(
zipper,
0,
router_using(igniter)
web_module(igniter)
) &&
Igniter.Code.Function.argument_equals?(
zipper,
Expand Down Expand Up @@ -628,10 +634,6 @@ defmodule Igniter.Libs.Phoenix do
end
end

defp router_using(igniter) do
Module.concat([to_string(Igniter.Project.Module.module_name_prefix(igniter)) <> "Web"])
end

defp using_a_webbish_module?(zipper) do
case Igniter.Code.Function.move_to_nth_argument(zipper, 0) do
{:ok, zipper} ->
Expand Down
42 changes: 42 additions & 0 deletions test/igniter/libs/phoenix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,46 @@ defmodule Igniter.Libs.PhoenixTest do
refute Igniter.Libs.Phoenix.controller?(igniter, TestWeb.ThingView)
end
end

describe "web_module/1" do
test "append Web" do
mix_exs = """
defmodule Test.MixProject do
end
"""

igniter =
assert test_project()
|> Igniter.create_or_update_elixir_file(
"mix.exs",
mix_exs,
&{:ok, Igniter.Code.Common.replace_code(&1, mix_exs)}
)
|> apply_igniter!()

assert Igniter.Libs.Phoenix.web_module(igniter) == TestWeb
end

test "do not append Web suffix if name already ends with Web" do
mix_exs = """
defmodule TestWeb.MixProject do
end
"""

igniter =
assert test_project()
|> Igniter.create_or_update_elixir_file(
"mix.exs",
mix_exs,
&{:ok, Igniter.Code.Common.replace_code(&1, mix_exs)}
)
|> apply_igniter!()

assert Igniter.Libs.Phoenix.web_module(igniter) == TestWeb
end
end

test "web_module_name/1" do
assert Igniter.Libs.Phoenix.web_module_name(test_project(), "Suffix") == TestWeb.Suffix
end
end
8 changes: 8 additions & 0 deletions test/igniter/project/module_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Igniter.Project.ModuleTest do
use ExUnit.Case
import Igniter.Test

test "module_name_prefix/1" do
assert Igniter.Project.Module.module_name_prefix(test_project()) == Test
end
end

0 comments on commit 0dc4f85

Please sign in to comment.