diff --git a/lib/ex_factor.ex b/lib/ex_factor.ex index ae87b1a..1ea38f9 100644 --- a/lib/ex_factor.ex +++ b/lib/ex_factor.ex @@ -63,6 +63,8 @@ defmodule ExFactor do end) end + defp format(%{state: [:unchanged]} = struct), do: struct + defp format(struct) do Formatter.format([struct.path]) Map.get_and_update(struct, :state, fn val -> {val, [:formatted | val]} end) diff --git a/lib/ex_factor/changer.ex b/lib/ex_factor/changer.ex index 961eeea..5124f45 100644 --- a/lib/ex_factor/changer.ex +++ b/lib/ex_factor/changer.ex @@ -4,7 +4,6 @@ defmodule ExFactor.Changer do """ alias ExFactor.Callers - alias ExFactor.Util @doc """ Given all the Callers to a module, find the instances of the target function and refactor the @@ -56,7 +55,7 @@ defmodule ExFactor.Changer do target_module = Keyword.fetch!(opts, :target_module) # modified values - source_string = Util.module_to_string(source_module) + source_string = to_string(source_module) source_modules = String.split(source_module, ".") source_alias = Enum.at(source_modules, -1) target_alias = preferred_alias(file_list, target_module) @@ -140,7 +139,7 @@ defmodule ExFactor.Changer do defp maybe_add_alias({state, contents_list}, opts) do target_module = Keyword.fetch!(opts, :target_module) - target_string = Util.module_to_string(target_module) + target_string = to_string(target_module) {state, contents_list} |> change_alias(target_string) @@ -193,7 +192,7 @@ defmodule ExFactor.Changer do defp maybe_add_import({state, contents_list}, opts) do source_module = Keyword.fetch!(opts, :source_module) target_module = Keyword.fetch!(opts, :target_module) - target_string = Util.module_to_string(target_module) + target_string = to_string(target_module) source_modules = String.split(source_module, ".") source_alias = Enum.at(source_modules, -1) source_alias_alt = find_alias_as(contents_list, source_module) diff --git a/lib/ex_factor/extractor.ex b/lib/ex_factor/extractor.ex index 7d36519..5b1203d 100644 --- a/lib/ex_factor/extractor.ex +++ b/lib/ex_factor/extractor.ex @@ -45,23 +45,21 @@ defmodule ExFactor.Extractor do {ast, list} = Parser.read_file(target_path) {:defmodule, [do: [line: _begin_line], end: [line: end_line], line: _], _} = ast - list - |> List.insert_at(end_line - 1, refactor_message()) - |> List.insert_at(end_line, string_fns) - |> Enum.join("\n") - |> then(fn contents -> write_file(target_path, contents, target_module, dry_run) end) + insert_code(list, end_line, string_fns, target_path, target_module, dry_run) _ -> target_mod = Module.concat([target_module]) - quote generated: true do + module_contents = quote generated: true do defmodule unquote(target_mod) do - @moduledoc false - unquote(Macro.unescape_string(string_fns)) + @moduledoc "This module created with ExFactor" end end |> Macro.to_string() - |> then(fn contents -> write_file(target_path, contents, target_module, dry_run) end) + list = String.split(module_contents, "\n") + {:ok, ast} = Code.string_to_quoted(module_contents, token_metadata: true) + {:defmodule, [do: [line: _begin_line], end: [line: end_line], closing: _, line: _], _} = ast + insert_code(list, end_line, string_fns, target_path, target_module, dry_run) end end @@ -90,4 +88,12 @@ defmodule ExFactor.Extractor do file_contents: contents } end + + defp insert_code(list, end_line, string_fns, target_path, target_module, dry_run) do + list + |> List.insert_at(end_line - 1, refactor_message()) + |> List.insert_at(end_line, string_fns) + |> Enum.join("\n") + |> then(fn contents -> write_file(target_path, contents, target_module, dry_run) end) + end end diff --git a/lib/ex_factor/formatter.ex b/lib/ex_factor/formatter.ex index 20c26b8..c1dc962 100644 --- a/lib/ex_factor/formatter.ex +++ b/lib/ex_factor/formatter.ex @@ -4,6 +4,8 @@ defmodule ExFactor.Formatter do Format a list of files """ + def format([nil]), do: nil + def format(args) do Mix.Tasks.Format.run(args) end diff --git a/lib/ex_factor/neighbors.ex b/lib/ex_factor/neighbors.ex index 87f3ed7..4540bfe 100644 --- a/lib/ex_factor/neighbors.ex +++ b/lib/ex_factor/neighbors.ex @@ -8,17 +8,17 @@ defmodule ExFactor.Neighbors do Find all the instances of the target function. Return after evaluating all the block-level AST elements. Ignore certain elements, such as :alias. """ - def walk(block, fn_name, arity \\ :unmatched) + def walk(block, func_name, arity \\ :unmatched) - def walk(block, fn_name, arity) when is_binary(fn_name) do - # fn_name_atom = String.to_atom(fn_name) - walk(block, String.to_atom(fn_name), arity) + def walk(block, func_name, arity) when is_binary(func_name) do + # func_name_atom = String.to_atom(func_name) + walk(block, String.to_atom(func_name), arity) end - def walk(block, fn_name, arity) do + def walk(block, func_name, arity) do block |> Enum.reduce({[], []}, fn el, acc -> - eval_elem(el, acc, fn_name, arity) + eval_elem(el, acc, func_name, arity) end) |> elem(1) end diff --git a/lib/ex_factor/parser.ex b/lib/ex_factor/parser.ex index 34816f6..b228105 100644 --- a/lib/ex_factor/parser.ex +++ b/lib/ex_factor/parser.ex @@ -145,6 +145,10 @@ defmodule ExFactor.Parser do block_contents end + defp ast_block([do: block_contents], _acc) do + [block_contents] + end + defp ast_block(_block, acc) do acc end diff --git a/lib/ex_factor/util.ex b/lib/ex_factor/util.ex deleted file mode 100644 index e3ecc52..0000000 --- a/lib/ex_factor/util.ex +++ /dev/null @@ -1,11 +0,0 @@ -defmodule ExFactor.Util do - @moduledoc false - - # TODO: we probably don't even need this function - # use this as a dead code finder - def module_to_string(module) when is_atom(module) do - to_string(module) - end - - def module_to_string(module), do: module -end diff --git a/test/ex_factor/util_test.exs b/test/ex_factor/util_test.exs deleted file mode 100644 index caebdf9..0000000 --- a/test/ex_factor/util_test.exs +++ /dev/null @@ -1,19 +0,0 @@ -defmodule ExFactor.UtilTest do - use ExUnit.Case - alias ExFactor.Util - - setup_all do - File.mkdir_p("test/tmp") - - on_exit(fn -> - File.rm_rf("test/tmp") - end) - end - - describe "module_to_string/1" do - test "given a module name, convert it to a string, ensure the Elixir. prefix is not included" do - refute Util.module_to_string(MyMod.SubMod.SubSubMod) =~ "Elixir." - assert Util.module_to_string(MyMod.SubMod.SubSubMod) == "MyMod.SubMod.SubSubMod" - end - end -end