Skip to content

Commit

Permalink
feat: Igniter.Code.Module.move_to_attribute_definition (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp authored Jan 27, 2025
1 parent 0e1623d commit 4547901
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
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
20 changes: 20 additions & 0 deletions test/igniter/code/module_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,24 @@ 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 4547901

Please sign in to comment.