diff --git a/lib/igniter.ex b/lib/igniter.ex index 4853988..ded74cb 100644 --- a/lib/igniter.ex +++ b/lib/igniter.ex @@ -102,7 +102,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 @@ -252,9 +252,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)) @@ -544,8 +544,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)} @@ -672,8 +672,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} @@ -1203,49 +1203,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, """ @@ -1325,86 +1298,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) @@ -1426,9 +1319,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)) @@ -1449,19 +1342,6 @@ 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 @@ -1469,7 +1349,7 @@ defmodule Igniter do 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." diff --git a/lib/igniter/test.ex b/lib/igniter/test.ex index 568589e..cf99b5d 100644 --- a/lib/igniter/test.ex +++ b/lib/igniter/test.ex @@ -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 diff --git a/mix.exs b/mix.exs index dd92d93..2fa14cf 100644 --- a/mix.exs +++ b/mix.exs @@ -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"}, diff --git a/mix.lock b/mix.lock index 8ab729f..a9e5b20 100644 --- a/mix.lock +++ b/mix.lock @@ -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"}, }