From 50c381ccae835d50f73d6f353cc49d93a74ed474 Mon Sep 17 00:00:00 2001 From: Christian Koch Date: Thu, 16 Sep 2021 17:32:55 -0500 Subject: [PATCH] Fix a few tests, and continue to refactor --- .gitignore | 2 +- lib/ex_factor/extractor.ex | 8 +------ lib/ex_factor/parser.ex | 37 ++------------------------------- lib/ex_factor/remover.ex | 6 ------ test/ex_factor/callers_test.exs | 4 ++-- test/ex_factor/remover_test.exs | 21 ++++--------------- test/tmp/source_module.ex | 28 ------------------------- 7 files changed, 10 insertions(+), 96 deletions(-) delete mode 100644 test/tmp/source_module.ex diff --git a/.gitignore b/.gitignore index cb5a81b..36dcd93 100644 --- a/.gitignore +++ b/.gitignore @@ -22,8 +22,8 @@ erl_crash.dump # Ignore package tarball (built via "mix hex.build"). ex_factor-*.tar - # Temporary files for e.g. tests /tmp +test/tmp test/support/*_module.ex diff --git a/lib/ex_factor/extractor.ex b/lib/ex_factor/extractor.ex index fb23a10..2fe6c54 100644 --- a/lib/ex_factor/extractor.ex +++ b/lib/ex_factor/extractor.ex @@ -48,17 +48,11 @@ defmodule ExFactor.Extractor do def remove(opts) do source_module = Keyword.get(opts, :source_module) - # target_module = Keyword.get(opts, :target_module) source_function = Keyword.get(opts, :source_function) arity = Keyword.get(opts, :arity) - # target_path = Keyword.get(opts, :target_path, path(target_module)) source_path = Keyword.get(opts, :source_path, path(source_module)) - {_ast, functions} = Parser.all_functions(source_path) - functions |> IO.inspect(label: "") - # to_extract = Neighbors.walk(block_contents, source_function, arity) - # |> IO.inspect(label: "to_extract") - + ExFactor.Remover.remove(source_path, source_function, arity) end defp path(module) do diff --git a/lib/ex_factor/parser.ex b/lib/ex_factor/parser.ex index d782dcf..429d4e3 100644 --- a/lib/ex_factor/parser.ex +++ b/lib/ex_factor/parser.ex @@ -21,15 +21,9 @@ defmodule ExFactor.Parser do end def block_contents({:ok, ast}) do - # {_ast, block} = Macro.postwalk(ast, [], fn node, acc -> {node, ast_block(node, acc)} end) - - # Macro.postwalk(block, [], fn node, acc -> - # {node, walk_ast(node, acc, :def)} - # # |> IO.inspect(label: "walk AST") - # end) end @doc """ @@ -46,6 +40,8 @@ defmodule ExFactor.Parser do {_ast, public_functions} = public_functions(input) {ast, private_functions} = private_functions(input) {ast, public_functions ++ private_functions} + all_fns = public_functions ++ private_functions + {ast, Enum.uniq(all_fns)} end @doc """ @@ -64,14 +60,6 @@ defmodule ExFactor.Parser do end) end - # def public_functions({:ok, ast}) do - # ast |> IO.inspect(label: "") - # Macro.postwalk(ast, {:prev, []}, fn node, acc -> - # {node, walk_ast(node, acc, :def)} - # # |> IO.inspect(label: "walk AST") - # end) - # end - @doc """ Identify private functions from a module AST. """ @@ -96,34 +84,22 @@ defmodule ExFactor.Parser do defp walk_ast({:@, fn_meta, [{:spec, _meta, [{_, _, [{name, _, args} | _]} | _]} | _]} = node, acc, _token) do arity = length(args) map = merge_maps(%{name: name, ast: node, arity: arity, defn: "@spec"}, fn_meta) - # map = - # fn_meta - # |> IO.inspect(label: "spec meta") - # |> find_lines() - # |> Map.merge(%{name: :spec, ast: node, arity: arity, defn: "@spec"}) [map | acc] end defp walk_ast({tkn, fn_meta, [{:when, _when_meta, [{name, _meta, args} | _]} | _]} = node, acc, token) when tkn == token do arity = length(args) map = merge_maps(%{name: name, ast: node, arity: arity, defn: token}, fn_meta) - # fn_meta - # |> find_lines() - # |> Map.merge() [map | acc] end defp walk_ast({tkn, fn_meta, [{name, _meta, args} | _]} = node, acc, token) when tkn == token do arity = length(args) map = merge_maps(%{name: name, ast: node, arity: arity, defn: token}, fn_meta) - # fn_meta - # |> find_lines() - # |> Map.merge() [map | acc] end defp walk_ast(_node, acc, _token) do - # node |> IO.inspect(label: "") acc end @@ -162,12 +138,3 @@ defmodule ExFactor.Parser do acc end end - - -[{:"::", [line: 2], [ - {:priv1, [closing: [line: 2], line: 2], - [{:term, [closing: [line: 2], line: 2], []}]}, - {:term, [closing: [line: 2], line: 2], []} - ] - } -] \ No newline at end of file diff --git a/lib/ex_factor/remover.ex b/lib/ex_factor/remover.ex index 1530d91..ec259a3 100644 --- a/lib/ex_factor/remover.ex +++ b/lib/ex_factor/remover.ex @@ -3,17 +3,11 @@ defmodule ExFactor.Remover do Documentation for `ExFactor.Remover`. """ - # alias ExFactor.Neighbors alias ExFactor.Parser - def remove(source_path, fn_name, arity) do {_ast, block_contents} = Parser.all_functions(source_path) - # [function | _] = block_contents - # function = Enum.find(block_contents, &(&1.name == fn_name and &1.arity == arity && (&1.defn == :def || &1.defn == :defp))) fns_to_remove = Enum.filter(block_contents, & &1.name == fn_name) - |> IO.inspect(label: "fns_to_remove") - {_ast, line_list} = Parser.read_file(source_path) Enum.reduce(fns_to_remove, line_list, fn function, acc -> diff --git a/test/ex_factor/callers_test.exs b/test/ex_factor/callers_test.exs index e5ac9fe..1993bd1 100644 --- a/test/ex_factor/callers_test.exs +++ b/test/ex_factor/callers_test.exs @@ -4,11 +4,11 @@ defmodule ExFactor.CallersTest do describe "callers/1" do test "it should report callers of a module" do - [one, _two, _three, four] = Callers.callers(ExFactor.Parser) + [one, _two, _three, _four, five] = Callers.callers(ExFactor.Parser) assert one.dependency_type == "(runtime)" assert one.filepath == "lib/ex_factor/callers.ex" - assert four.filepath == "test/support/support.ex" + assert five.filepath == "test/support/support.ex" end end end diff --git a/test/ex_factor/remover_test.exs b/test/ex_factor/remover_test.exs index c64544e..c833660 100644 --- a/test/ex_factor/remover_test.exs +++ b/test/ex_factor/remover_test.exs @@ -6,9 +6,9 @@ defmodule ExFactor.RemoverTest do setup_all do File.mkdir_p("test/tmp") - # on_exit(fn -> - # File.rm_rf("test/tmp") - # end) + on_exit(fn -> + File.rm_rf("test/tmp") + end) end describe "remove/1" do @@ -38,29 +38,16 @@ defmodule ExFactor.RemoverTest do end """ - # |> Code.string_to_quoted() File.write("test/tmp/source_module.ex", module) - - # {_ast, [f1, f2]} = Parser.public_functions("test/tmp/source_module.ex") - # {_ast, [f1, f2]} = Parser.public_functions("test/tmp/source_module.ex") - - # {_ast, block} = Parser.block_contents("test/tmp/source_module.ex") Remover.remove("test/tmp/source_module.ex", :pub1, 1) - # assert [ - # {:@, _, [{:somedoc, _, ["This is somedoc"]}]}, - # {:@, _, [{:doc, _, ["\n multiline\n documentation for pub1\n "]}]}, - # {:def, _, [{:pub1, _, [{:arg1, _, nil}]}, _]} - # ] = Remover.walk(block, :pub1) updated_file = File.read!("test/tmp/source_module.ex") refute updated_file =~ "def pub1(arg1) do" assert updated_file =~ "Function: pub1/1 removed by ExFactor" + assert updated_file =~ "# @spec: pub1/1 removed by ExFactor" # it removes specs too refute updated_file =~ "@spec pub1(term()) :: term()" end - - # test "it removes specs too" do - # end end end diff --git a/test/tmp/source_module.ex b/test/tmp/source_module.ex deleted file mode 100644 index 35e447a..0000000 --- a/test/tmp/source_module.ex +++ /dev/null @@ -1,28 +0,0 @@ -defmodule ExFactorSampleModule do - @somedoc "This is somedoc" - # comments get dropped - @doc " - multiline - documentation for pub1 - " -# @spec: pub1/1 removed by ExFactor - -# @spec: pub1/1 removed by ExFactor - -# -# Function: pub1/1 removed by ExFactor -# ExFactor only removes the function itself -# Other artifacts, including docs and module-level comments -# may remain for you to remove manually. -# - - defp priv1(arg1) do - :ok - end - - def pub2(arg1) - do - :ok - end - -end