Skip to content

Commit

Permalink
move to Igniter.Code.Module.move_to_attribute_definition/2
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Jan 24, 2025
1 parent 3b002f7 commit 75032e6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
29 changes: 0 additions & 29 deletions lib/igniter/code/function.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,6 @@ defmodule Igniter.Code.Function do
end
end

@doc """
Move to a constant inside a module.
For example, given this module:
defmodule MyAppWeb.Endpoint do
@session_options [
store: :cookie,
...
]
end
You can move into that constant with:
Igniter.Code.Function.move_to_constant(zipper, :session_options)
"""
@spec move_to_constant(Zipper.t(), atom()) :: {:ok, Zipper.t()} | :error
def move_to_constant(zipper, name) when is_atom(name) do
with {:ok, zipper} <- Igniter.Code.Module.move_to_defmodule(zipper),
{:ok, zipper} <- Common.move_to_do_block(zipper),
{:ok, zipper} <- Common.move_to_pattern(zipper, {:@, _, [{^name, _, _}]}) do
{:ok, zipper}
else
_ ->
:error
end
end

@doc "Moves to a function call by the given name and arity, matching the given predicate, in the current scope"
@spec move_to_function_call_in_current_scope(
Zipper.t(),
Expand Down
37 changes: 37 additions & 0 deletions lib/igniter/code/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,43 @@ defmodule Igniter.Code.Module do
Igniter.Code.Function.move_to_def(zipper, fun, arity)
end

@doc """
Move to an attribute definition inside a module.
## Example
Given this module:
defmodule MyAppWeb.Endpoint do
@doc "My App Endpoint"
@session_options [
store: :cookie,
...
]
end
You can move into `@doc` attribute with:
Igniter.Code.Module.move_to_attribute_definition(zipper, :doc)
Or you can move into `@session_options` constant with:
Igniter.Code.Module.move_to_attribute_definition(zipper, :session_options)
"""
@spec move_to_attribute_definition(Zipper.t(), atom()) :: {:ok, Zipper.t()} | :error
def move_to_attribute_definition(zipper, name) when is_atom(name) do
with {:ok, zipper} <- Igniter.Code.Module.move_to_defmodule(zipper),
{:ok, zipper} <- Common.move_to_do_block(zipper),
{:ok, zipper} <- Common.move_to_pattern(zipper, {:@, _, [{^name, _, _}]}) do
{:ok, zipper}
else
_ ->
:error
end
end

def module?(zipper) do
Common.node_matches_pattern?(zipper, {:__aliases__, _, [_ | _]})
end
Expand Down
15 changes: 0 additions & 15 deletions test/igniter/code/function_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,4 @@ defmodule Igniter.Code.FunctionTest do
assert Igniter.Code.Function.argument_equals?(zipper, 1, :key) == false
assert Igniter.Code.Function.argument_equals?(zipper, 1, Test) == true
end

test "move_to_constant" do
zipper =
~s"""
defmodule MyApp.Foo do
@foo_key Application.compile_env!(:my_app, :key)
end
"""
|> Sourceror.parse_string!()
|> Sourceror.Zipper.zip()

assert {:ok, zipper} = Igniter.Code.Function.move_to_constant(zipper, :foo_key)

assert Igniter.Util.Debug.code_at_node(zipper) == "@foo_key Application.compile_env!(:my_app, :key)"
end
end
18 changes: 18 additions & 0 deletions test/igniter/code/module_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,22 @@ defmodule Igniter.Code.ModuleTest do
end)
end
end

test "move_to_attribute_definition" do
mod_zipper =
~s"""
defmodule MyApp.Foo do
@doc "My app module doc"
@foo_key Application.compile_env!(:my_app, :key)
end
"""
|> Sourceror.parse_string!()
|> Sourceror.Zipper.zip()

assert {:ok, zipper} = Igniter.Code.Module.move_to_attribute_definition(mod_zipper, :doc)
assert Igniter.Util.Debug.code_at_node(zipper) == ~s|@doc "My app module doc"|

assert {:ok, zipper} = Igniter.Code.Module.move_to_attribute_definition(mod_zipper, :foo_key)
assert Igniter.Util.Debug.code_at_node(zipper) == "@foo_key Application.compile_env!(:my_app, :key)"
end
end

0 comments on commit 75032e6

Please sign in to comment.