Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rewrite to 1.0.0 #135

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 15 additions & 135 deletions lib/igniter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ defmodule Igniter do
@doc "Returns a new igniter"
@spec new() :: t()
def new do
%__MODULE__{rewrite: Rewrite.new()}
%__MODULE__{rewrite: Rewrite.new(hooks: [Rewrite.Hook.DotFormatterUpdater])}
|> include_existing_elixir_file(".igniter.exs", required?: false)
|> parse_igniter_config()
end
Expand Down Expand Up @@ -242,9 +242,9 @@ defmodule Igniter do
igniter.rewrite,
Rewrite.Source.update(
source,
:configure,
:quoted,
Sourceror.Zipper.topmost_root(zipper)
Sourceror.Zipper.topmost_root(zipper),
by: :configure
)
)
|> then(&Map.put(igniter, :rewrite, &1))
Expand Down Expand Up @@ -481,8 +481,8 @@ defmodule Igniter do
rescue
_ ->
""
|> Rewrite.Source.Ex.from_string(path)
|> Rewrite.Source.update(:file_creator, :content, contents)
|> Rewrite.Source.Ex.from_string(path: path)
|> Rewrite.Source.update(:content, contents, by: :file_creator)
end

%{igniter | rewrite: Rewrite.put!(igniter.rewrite, source)}
Expand Down Expand Up @@ -609,8 +609,8 @@ defmodule Igniter do

source =
""
|> source_handler.from_string(path)
|> Rewrite.Source.update(:file_creator, :content, contents)
|> source_handler.from_string(path: path)
|> Rewrite.Source.update(:content, contents, by: :file_creator)

if has_source? do
{already_exists(igniter, path, Keyword.get(opts, :on_exists, :error)), source}
Expand Down Expand Up @@ -1140,49 +1140,22 @@ defmodule Igniter do
end

rewrite = igniter.rewrite

formatter_exs_files =
rewrite
|> Enum.filter(fn source ->
source
|> Rewrite.Source.get(:path)
|> Path.basename()
|> Kernel.==(".formatter.exs")
end)
|> Map.new(fn source ->
dir =
source
|> Rewrite.Source.get(:path)
|> Path.dirname()

{dir, source}
end)
dot_formatter = Rewrite.dot_formatter(rewrite)

rewrite =
Rewrite.map!(rewrite, fn source ->
path = source |> Rewrite.Source.get(:path)

if is_nil(adding_paths) || path in List.wrap(adding_paths) do
dir = Path.dirname(path)

opts =
case find_formatter_exs_file_options(dir, formatter_exs_files, Path.extname(path)) do
:error ->
[]

{:ok, opts} ->
opts
end

try do
formatted =
with_evaled_configs(rewrite, fn ->
Rewrite.Source.Ex.format(source, opts)
source
|> Rewrite.Source.format!(dot_formatter: dot_formatter)
|> Rewrite.Source.get(:content)
end)

source
|> Rewrite.Source.Ex.put_formatter_opts(opts)
|> Rewrite.Source.update(:content, formatted)
Rewrite.Source.update(source, :content, formatted)
rescue
e ->
Rewrite.Source.add_issue(source, """
Expand Down Expand Up @@ -1262,86 +1235,6 @@ defmodule Igniter do
end
end

defp find_formatter_exs_file_options(path, formatter_exs_files, ext) do
case Map.fetch(formatter_exs_files, path) do
{:ok, source} ->
{opts, _} = Rewrite.Source.get(source, :quoted) |> Code.eval_quoted()

{:ok, opts |> eval_deps() |> filter_plugins(ext)}

:error ->
if path in ["/", "."] do
:error
else
new_path =
Path.join(path, "..")
|> Path.expand()
|> Path.relative_to_cwd()

find_formatter_exs_file_options(new_path, formatter_exs_files, ext)
end
end
end

# This can be removed if/when this PR is merged: https://github.com/hrzndhrn/rewrite/pull/34
defp eval_deps(formatter_opts) do
deps = Keyword.get(formatter_opts, :import_deps, [])

locals_without_parens = eval_deps_opts(deps)

formatter_opts =
Keyword.update(
formatter_opts,
:locals_without_parens,
locals_without_parens,
&(locals_without_parens ++ &1)
)

formatter_opts
end

defp eval_deps_opts([]) do
[]
end

defp eval_deps_opts(deps) do
deps_paths = Mix.Project.deps_paths()

for dep <- deps,
dep_path = fetch_valid_dep_path(dep, deps_paths),
!is_nil(dep_path),
dep_dot_formatter = Path.join(dep_path, ".formatter.exs"),
File.regular?(dep_dot_formatter),
dep_opts = eval_file_with_keyword_list(dep_dot_formatter),
parenless_call <- dep_opts[:export][:locals_without_parens] || [],
uniq: true,
do: parenless_call
end

defp fetch_valid_dep_path(dep, deps_paths) when is_atom(dep) do
with %{^dep => path} <- deps_paths,
true <- File.dir?(path) do
path
else
_ ->
nil
end
end

defp fetch_valid_dep_path(_dep, _deps_paths) do
nil
end

defp eval_file_with_keyword_list(path) do
{opts, _} = Code.eval_file(path)

if !Keyword.keyword?(opts) do
raise "Expected #{inspect(path)} to return a keyword list, got: #{inspect(opts)}"
end

opts
end

defp apply_func_with_zipper(igniter, path, func) do
source = Rewrite.source!(igniter.rewrite, path)
quoted = Rewrite.Source.get(source, :quoted)
Expand All @@ -1363,9 +1256,9 @@ defmodule Igniter do
igniter.rewrite,
Rewrite.Source.update(
source,
:configure,
:quoted,
Sourceror.Zipper.root(zipper)
Sourceror.Zipper.root(zipper),
by: :configure
)
)
|> then(&Map.put(igniter, :rewrite, &1))
Expand All @@ -1386,27 +1279,14 @@ defmodule Igniter do
end
end

defp filter_plugins(opts, ext) do
Keyword.put(opts, :plugins, plugins_for_ext(opts, ext))
end

defp plugins_for_ext(formatter_opts, ext) do
formatter_opts
|> Keyword.get(:plugins, [])
|> Enum.filter(fn plugin ->
Code.ensure_loaded?(plugin) and function_exported?(plugin, :features, 1) and
ext in List.wrap(plugin.features(formatter_opts)[:extensions])
end)
end

defp read_ex_source!(igniter, path) do
read_source!(igniter, path, Rewrite.Source.Ex)
end

defp read_source!(igniter, path, source_handler) do
if igniter.assigns[:test_mode?] do
if content = igniter.assigns[:test_files][path] do
source_handler.from_string(content, path)
source_handler.from_string(content, path: path)
|> Map.put(:from, :file)
else
raise "File #{path} not found in test files."
Expand Down
2 changes: 1 addition & 1 deletion lib/igniter/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ defmodule Igniter.Test do

Diff, showing your assertion against the actual contents:

#{Rewrite.TextDiff.format(actual_content, content)}
#{TextDiff.format(actual_content, content)}
""")
end
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule Igniter.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:rewrite, "~> 0.9"},
{:rewrite, "~> 1.0"},
{:glob_ex, "~> 0.1.7"},
{:spitfire, "~> 0.1 and >= 0.1.3"},
{:sourceror, "~> 1.4"},
Expand Down
3 changes: 2 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
"mix_audit": {:hex, :mix_audit, "2.1.4", "0a23d5b07350cdd69001c13882a4f5fb9f90fbd4cbf2ebc190a2ee0d187ea3e9", [:make, :mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:yaml_elixir, "~> 2.11", [hex: :yaml_elixir, repo: "hexpm", optional: false]}], "hexpm", "fd807653cc8c1cada2911129c7eb9e985e3cc76ebf26f4dd628bb25bbcaa7099"},
"mix_test_watch": {:hex, :mix_test_watch, "1.2.0", "1f9acd9e1104f62f280e30fc2243ae5e6d8ddc2f7f4dc9bceb454b9a41c82b42", [:mix], [{:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm", "278dc955c20b3fb9a3168b5c2493c2e5cffad133548d307e0a50c7f2cfbf34f6"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"rewrite": {:hex, :rewrite, "0.10.5", "6afadeae0b9d843b27ac6225e88e165884875e0aed333ef4ad3bf36f9c101bed", [:mix], [{:glob_ex, "~> 0.1", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.0", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "51cc347a4269ad3a1e7a2c4122dbac9198302b082f5615964358b4635ebf3d4f"},
"rewrite": {:hex, :rewrite, "1.0.0", "fddda21eb62c2b9dbd6cf35b39984d02761a63c25de2947ceab7ffe2729fa2e5", [:mix], [{:glob_ex, "~> 0.1", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.0", [hex: :sourceror, repo: "hexpm", optional: false]}, {:text_diff, "~> 0.1", [hex: :text_diff, repo: "hexpm", optional: false]}], "hexpm", "701975d9d0c21b1f40a82881e489aca6b53893f095f318978c6d2899299a514b"},
"sourceror": {:hex, :sourceror, "1.7.0", "62c34f4e3a109d837edd652730219b6332745e0ec7db34d5d350a5cdf30b376a", [:mix], [], "hexpm", "3dd2b1bd780fd0df48089f48480a54fd065bf815b63ef8046219d7784e7435f3"},
"spitfire": {:hex, :spitfire, "0.1.3", "7ea0f544005dfbe48e615ed90250c9a271bfe126914012023fd5e4b6b82b7ec7", [:mix], [], "hexpm", "d53b5107bcff526a05c5bb54c95e77b36834550affd5830c9f58760e8c543657"},
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
"text_diff": {:hex, :text_diff, "0.1.0", "1caf3175e11a53a9a139bc9339bd607c47b9e376b073d4571c031913317fecaa", [:mix], [], "hexpm", "d1ffaaecab338e49357b6daa82e435f877e0649041ace7755583a0ea3362dbd7"},
"yamerl": {:hex, :yamerl, "0.10.0", "4ff81fee2f1f6a46f1700c0d880b24d193ddb74bd14ef42cb0bcf46e81ef2f8e", [:rebar3], [], "hexpm", "346adb2963f1051dc837a2364e4acf6eb7d80097c0f53cbdc3046ec8ec4b4e6e"},
"yaml_elixir": {:hex, :yaml_elixir, "2.11.0", "9e9ccd134e861c66b84825a3542a1c22ba33f338d82c07282f4f1f52d847bd50", [:mix], [{:yamerl, "~> 0.10", [hex: :yamerl, repo: "hexpm", optional: false]}], "hexpm", "53cc28357ee7eb952344995787f4bb8cc3cecbf189652236e9b163e8ce1bc242"},
}
Loading