Skip to content

Commit

Permalink
fix: patch formatter fix, to be removed later when rewrite PR is merged
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jun 13, 2024
1 parent 2f2c1e3 commit 933876d
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions lib/igniter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ defmodule Igniter do
end
end

@spec exists?(t(), Path.t()) :: boolean()
def exists?(igniter, path) do
Rewrite.has_source?(igniter.rewrite, path) || File.exists?(path)
end

@doc "Updates or creates the given file in the project with the provided contents."
@spec create_or_update_elixir_file(t(), Path.t(), String.t(), zipper_updater()) :: Igniter.t()
def create_or_update_elixir_file(igniter, path, contents, func) do
Expand Down Expand Up @@ -501,7 +506,7 @@ defmodule Igniter do
dir = Path.dirname(path)

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

Expand Down Expand Up @@ -569,12 +574,12 @@ defmodule Igniter do
end

# sobelow_skip ["RCE.CodeModule"]
defp find_formatter_exs_file_options(path, formatter_exs_files) do
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, eval_deps(opts)}
{:ok, opts |> eval_deps() |> filter_plugins(ext)}

:error ->
if path in ["/", "."] do
Expand All @@ -585,7 +590,7 @@ defmodule Igniter do
|> Path.expand()
|> Path.relative_to_cwd()

find_formatter_exs_file_options(new_path, formatter_exs_files)
find_formatter_exs_file_options(new_path, formatter_exs_files, ext)
end
end
end
Expand Down Expand Up @@ -675,4 +680,17 @@ defmodule Igniter do
Rewrite.Source.add_issues(source, List.wrap(error))
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
end

0 comments on commit 933876d

Please sign in to comment.