Skip to content

Commit

Permalink
chore: fix bugs/build
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jul 1, 2024
1 parent 922178e commit b878903
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/igniter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ defmodule Igniter do
* `:error_on_abort?` - If `true`, raises an error if the user aborts the operation. Returns the original igniter if not.
"""
# sobelow_skip ["RCE.CodeModule"]
def apply_and_fetch_dependencies(igniter, opts \\ []) do
if has_changes?(igniter, ["mix.exs"]) do
case Igniter.do_or_dry_run(igniter, ["--dry-run"],
Expand Down
47 changes: 39 additions & 8 deletions lib/igniter/code/common.ex
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ defmodule Igniter.Code.Common do
current_code = Zipper.root(current_code)

case current_code do
{:__block__, meta, stuff} ->
{:__block__, meta, stuff} when length(stuff) > 1 ->
new_stuff =
if placement == :after do
stuff ++ [new_code]
Expand Down Expand Up @@ -446,35 +446,66 @@ defmodule Igniter.Code.Common do
|> maybe_move_to_single_child_block()
end

defp multi_child_block?(zipper) do
node_matches_pattern?(zipper, {:__block__, _, [_, _ | _]})
end

@doc """
Moves right in the zipper, until the provided predicate returns `true`.
Moves rightwards, entering blocks (and exiting them if no match is found), until the provided predicate returns `true`.
Returns `:error` if the end is reached without finding a match.
"""
@spec move_right(Zipper.t(), (Zipper.t() -> boolean)) :: {:ok, Zipper.t()} | :error
def move_right(%Zipper{} = zipper, pred) do
zipper_in_block = maybe_move_to_single_child_block(zipper)
zipper_in_single_child_block = maybe_move_to_single_child_block(zipper)

cond do
pred.(zipper) ->
{:ok, zipper}

pred.(zipper_in_block) ->
{:ok, zipper_in_block}
pred.(zipper_in_single_child_block) ->
{:ok, zipper_in_single_child_block}

multi_child_block?(zipper) ->
zipper
|> Zipper.down()
|> case do
nil ->
case Zipper.right(zipper) do
nil ->
:error

zipper ->
move_right(zipper, pred)
end

zipper ->
case move_right(zipper, pred) do
{:ok, zipper} ->
{:ok, zipper}

:error ->
case Zipper.right(zipper) do
nil ->
:error

zipper ->
move_right(zipper, pred)
end
end
end

true ->
case Zipper.right(zipper) do
nil ->
:error

zipper ->
zipper
|> move_right(pred)
move_right(zipper, pred)
end
end
end

# keeping in mind that version returns `nil` on no match
@doc """
Matches and moves to the location of a `__cursor__` in provided source code.
Expand Down
2 changes: 1 addition & 1 deletion test/project/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ defmodule Igniter.Project.ConfigTest do
"""
end

test "we workaround trailing comments bug" do
test "we merge configs even in large config files" do
%{rewrite: rewrite} =
Igniter.new()
|> Igniter.create_new_elixir_file("config/fake.exs", """
Expand Down

0 comments on commit b878903

Please sign in to comment.