Skip to content

Commit

Permalink
fix: pass dot_formatter when updating rewrite sources (#144)
Browse files Browse the repository at this point in the history
Closes #143
  • Loading branch information
zachallaun authored Nov 6, 2024
1 parent beb327e commit b5df895
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
29 changes: 18 additions & 11 deletions lib/igniter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,9 @@ defmodule Igniter do
try do
Rewrite.update!(
igniter.rewrite,
Rewrite.Source.update(
update_source(
source,
igniter,
:quoted,
Sourceror.Zipper.topmost_root(zipper),
by: :configure
Expand Down Expand Up @@ -563,7 +564,7 @@ defmodule Igniter do
_ ->
""
|> Rewrite.Source.Ex.from_string(path: path)
|> Rewrite.Source.update(:content, contents, by: :file_creator)
|> update_source(igniter, :content, contents, by: :file_creator)
end

%{igniter | rewrite: Rewrite.put!(igniter.rewrite, source)}
Expand All @@ -586,7 +587,7 @@ defmodule Igniter do
{true,
""
|> Rewrite.Source.Ex.from_string(path)
|> Rewrite.Source.update(:file_creator, :content, contents)}
|> update_source(igniter, :content, contents, by: :file_creator)}
end

%{igniter | rewrite: Rewrite.put!(igniter.rewrite, source)}
Expand Down Expand Up @@ -617,7 +618,7 @@ defmodule Igniter do
{true,
""
|> source_handler.from_string(path)
|> Rewrite.Source.update(:file_creator, :content, contents)}
|> update_source(igniter, :content, contents, by: :file_creator)}
end

%{igniter | rewrite: Rewrite.put!(igniter.rewrite, source)}
Expand Down Expand Up @@ -679,8 +680,7 @@ defmodule Igniter do
try do
source = read_source!(igniter, path, source_handler)

source =
Rewrite.Source.update(source, :content, contents)
source = update_source(source, igniter, :content, contents)

{already_exists(igniter, path, Keyword.get(opts, :on_exists, :error)), source}
rescue
Expand All @@ -691,7 +691,7 @@ defmodule Igniter do
source =
""
|> source_handler.from_string(path: path)
|> Rewrite.Source.update(:content, contents, by: :file_creator)
|> update_source(igniter, :content, contents, by: :file_creator)

if has_source? do
{already_exists(igniter, path, Keyword.get(opts, :on_exists, :error)), source}
Expand Down Expand Up @@ -782,7 +782,7 @@ defmodule Igniter do
|> Zipper.topmost()
|> Zipper.node()

source = Rewrite.Source.update(source, :quoted, quoted_with_only_deps_change)
source = update_source(source, igniter, :quoted, quoted_with_only_deps_change)
rewrite = Rewrite.update!(igniter.rewrite, source)

if opts[:force?] || changed?(source) do
Expand All @@ -804,7 +804,7 @@ defmodule Igniter do
end

source = Rewrite.source!(rewrite, "mix.exs")
source = Rewrite.Source.update(source, :quoted, quoted)
source = update_source(source, igniter, :quoted, quoted)

igniter =
%{igniter | rewrite: Rewrite.update!(rewrite, source)}
Expand Down Expand Up @@ -1238,7 +1238,7 @@ defmodule Igniter do
|> Rewrite.Source.get(:content)
end)

Rewrite.Source.update(source, :content, formatted)
update_source(source, igniter, :content, formatted)
rescue
e ->
Rewrite.Source.add_issue(source, """
Expand Down Expand Up @@ -1339,8 +1339,9 @@ defmodule Igniter do
try do
Rewrite.update!(
igniter.rewrite,
Rewrite.Source.update(
update_source(
source,
igniter,
:quoted,
Sourceror.Zipper.root(zipper),
by: :configure
Expand Down Expand Up @@ -1497,6 +1498,12 @@ defmodule Igniter do
String.trim(diff) != ""
end

@doc false
def update_source(%Rewrite.Source{} = source, %Igniter{} = igniter, key, value, opts \\ []) do
opts = Keyword.put_new(opts, :dot_formatter, Rewrite.dot_formatter(igniter.rewrite))
Rewrite.Source.update(source, key, value, opts)
end

defp display_warnings(%{warnings: []}, _title), do: :ok

defp display_warnings(%{warnings: warnings}, title) do
Expand Down
2 changes: 1 addition & 1 deletion lib/igniter/project/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ defmodule Igniter.Project.Module do
|> Zipper.topmost()
|> Zipper.node()

new_source = Rewrite.Source.update(source, :quoted, new_quoted)
new_source = Igniter.update_source(source, igniter, :quoted, new_quoted)

{:ok,
%{igniter | rewrite: Rewrite.update!(igniter.rewrite, new_source)}
Expand Down
2 changes: 1 addition & 1 deletion lib/igniter/refactors/rename.ex
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ defmodule Igniter.Refactors.Rename do
end

defp write_source(igniter, source, zipper) do
new_source = Rewrite.Source.update(source, :quoted, Zipper.topmost_root(zipper))
new_source = Igniter.update_source(source, igniter, :quoted, Zipper.topmost_root(zipper))
%{igniter | rewrite: Rewrite.update!(igniter.rewrite, new_source)}
end

Expand Down
49 changes: 49 additions & 0 deletions test/igniter/project/formatter_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
defmodule Igniter.Project.FormatterTest do
use ExUnit.Case
import Igniter.Test

describe "import_dep/2" do
test "regression: causes formatting to respect imported locals_without_parens" do
test_project(
files: %{
"test/formatter_test.exs" => """
defmodule FormatterTest do
use ExUnit.Case
use Mimic.DSL
test "1" do
expect Foo.add(x, y), do: x + y
end
end
"""
}
)
|> Igniter.Project.Deps.add_dep({:mimic, "~> 1.7"})
|> Igniter.Project.Formatter.import_dep(:mimic)
|> Igniter.update_elixir_file("test/formatter_test.exs", fn zipper ->
with {:ok, zipper} <- Igniter.Code.Module.move_to_defmodule(zipper),
{:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper),
zipper <- Igniter.Code.Common.maybe_move_to_block(zipper) do
zipper =
zipper
|> Sourceror.Zipper.rightmost()
|> Igniter.Code.Common.add_code("""
test "2" do
expect Foo.subtract(x, y), do: x - y
end
""")

{:ok, zipper}
end
end)
|> assert_has_patch("test/formatter_test.exs", """
| end
+ |
+ | test "2" do
+ | expect Foo.subtract(x, y), do: x - y
+ | end
|end
""")
end
end
end

0 comments on commit b5df895

Please sign in to comment.