From 8bc9c525ebbacf34e018d5b61132c8a50ee64d23 Mon Sep 17 00:00:00 2001 From: Christian Koch Date: Tue, 7 Sep 2021 21:42:44 -0500 Subject: [PATCH] move to callers module --- lib/ex_factor.ex | 25 ------------- lib/ex_factor/callers.ex | 30 ++++++++++++++++ lib/ex_factor/evaluater.ex | 3 +- lib/ex_factor/extractor.ex | 19 +--------- lib/ex_factor/parser.ex | 2 +- test/ex_factor/callers_test.exs | 14 ++++++++ test/ex_factor/extractor_test.exs | 6 ++-- test/ex_factor/parser_test.exs | 59 ++++++++++++++++--------------- test/ex_factor_test.exs | 18 +++++----- test/support/support.ex | 3 +- 10 files changed, 92 insertions(+), 87 deletions(-) create mode 100644 lib/ex_factor/callers.ex create mode 100644 test/ex_factor/callers_test.exs diff --git a/lib/ex_factor.ex b/lib/ex_factor.ex index aad1691..0eb3a2d 100644 --- a/lib/ex_factor.ex +++ b/lib/ex_factor.ex @@ -2,29 +2,4 @@ defmodule ExFactor do @moduledoc """ Documentation for `ExFactor`. """ - - alias ExFactor.Parser - - def all_fns(input), do: Parser.all_functions(input) - - @doc """ - use `mix xref` list all the callers of a given module. - """ - # @spec callers(module()) :: list(map()) - def callers(mod) do - System.cmd("mix", ["xref", "callers", "#{mod}"], env: [{"MIX_ENV", "test"}]) - |> elem(0) - |> String.trim() - |> String.split("\n") - |> mangle_list() - end - - defp mangle_list(["Compiling" <> _ | tail]), do: mangle_list(tail) - - defp mangle_list(list) do - Enum.map(list, fn string -> - [path, type] = String.split(string, " ") - %{filepath: path, dependency_type: type} - end) - end end diff --git a/lib/ex_factor/callers.ex b/lib/ex_factor/callers.ex new file mode 100644 index 0000000..210bbf9 --- /dev/null +++ b/lib/ex_factor/callers.ex @@ -0,0 +1,30 @@ +defmodule ExFactor.Callers do + @moduledoc """ + Documentation for `ExFactor.Callers`. + """ + + alias ExFactor.Parser + + def all_fns(input), do: Parser.all_functions(input) + + @doc """ + use `mix xref` list all the callers of a given module. + """ + # @spec callers(module()) :: list(map()) + def callers(mod) do + System.cmd("mix", ["xref", "callers", "#{mod}"], env: [{"MIX_ENV", "test"}]) + |> elem(0) + |> String.trim() + |> String.split("\n") + |> mangle_list() + end + + defp mangle_list(["Compiling" <> _ | tail]), do: mangle_list(tail) + + defp mangle_list(list) do + Enum.map(list, fn string -> + [path, type] = String.split(string, " ") + %{filepath: path, dependency_type: type} + end) + end +end diff --git a/lib/ex_factor/evaluater.ex b/lib/ex_factor/evaluater.ex index 531a542..d2bcee9 100644 --- a/lib/ex_factor/evaluater.ex +++ b/lib/ex_factor/evaluater.ex @@ -3,11 +3,12 @@ defmodule ExFactor.Evaluater do Documentation for `ExFactor.Evaluater`. """ + alias ExFactor.Callers alias ExFactor.Parser def modules_to_refactor(module, func, arity) do module - |> ExFactor.callers() + |> Callers.callers() |> Enum.map(fn %{filepath: filepath} -> filepath |> Parser.public_functions() diff --git a/lib/ex_factor/extractor.ex b/lib/ex_factor/extractor.ex index 98e4ef8..2be9f1a 100644 --- a/lib/ex_factor/extractor.ex +++ b/lib/ex_factor/extractor.ex @@ -10,28 +10,11 @@ defmodule ExFactor.Extractor do source_function = Keyword.get(opts, :source_function) arity = Keyword.get(opts, :arity) target_function = Keyword.get(opts, :target_function, source_function) - target_path = Keyword.get(opts, :target_path, path(target_module)) - |> IO.inspect(label: "target_path") source_path = Keyword.get(opts, :source_path, path(source_module)) - # Macro.underscore(source_module) - - # source_path = Macro.underscore(source_module) <> ".ex" - # target_path = Macro.underscore(target_module) <> ".ex" - - # Path.join([Mix.Project.app_path(), target_path]) - # |> IO.inspect(label: "") - - File.exists?(source_path) |> IO.inspect(label: "exists") - {_ast, functions} = Parser.public_functions(source_path) - + # ast |> IO.inspect(label: "") map = Enum.find(functions, &(&1.name == source_function && &1.arity == arity)) - # |> IO.inspect(label: "source function") - - # map.ast - # |> Macro.to_string() - # |> IO.inspect(label: "source AST") case File.exists?(target_path) do true -> diff --git a/lib/ex_factor/parser.ex b/lib/ex_factor/parser.ex index bdf9c17..b35b10a 100644 --- a/lib/ex_factor/parser.ex +++ b/lib/ex_factor/parser.ex @@ -26,7 +26,7 @@ defmodule ExFactor.Parser do filepath |> File.read!() |> Code.string_to_quoted() - |> IO.inspect(label: "all funxs") + # |> IO.inspect(label: "all funxs") |> public_functions() end diff --git a/test/ex_factor/callers_test.exs b/test/ex_factor/callers_test.exs new file mode 100644 index 0000000..e5ac9fe --- /dev/null +++ b/test/ex_factor/callers_test.exs @@ -0,0 +1,14 @@ +defmodule ExFactor.CallersTest do + use ExUnit.Case + alias ExFactor.Callers + + describe "callers/1" do + test "it should report callers of a module" do + [one, _two, _three, four] = Callers.callers(ExFactor.Parser) + + assert one.dependency_type == "(runtime)" + assert one.filepath == "lib/ex_factor/callers.ex" + assert four.filepath == "test/support/support.ex" + end + end +end diff --git a/test/ex_factor/extractor_test.exs b/test/ex_factor/extractor_test.exs index 933a3c3..a3e637d 100644 --- a/test/ex_factor/extractor_test.exs +++ b/test/ex_factor/extractor_test.exs @@ -12,7 +12,8 @@ defmodule ExFactor.ExtractorTest do content = """ defmodule ExFactorSampleModule do @somedoc "This is somedoc" - # no aliases + # a comment and no aliases + _docp = "here's an arbitrary module underscore" def pub1(arg1) do :ok end @@ -20,8 +21,6 @@ defmodule ExFactor.ExtractorTest do """ File.write("test/support/source_module.ex", content) - - # target_path = Macro.underscore(target_module) target_path = "test/support/target_module.ex" File.rm(target_path) @@ -46,7 +45,6 @@ defmodule ExFactor.ExtractorTest do content = """ defmodule ExFactor.SourceModule do @somedoc "This is somedoc" - # no aliases def pub1(arg1) do :ok end diff --git a/test/ex_factor/parser_test.exs b/test/ex_factor/parser_test.exs index fce2056..d8a22ed 100644 --- a/test/ex_factor/parser_test.exs +++ b/test/ex_factor/parser_test.exs @@ -73,34 +73,37 @@ defmodule ExFactor.ParserTest do assert f1.name == :pub2 end - # test "ast references private fns and external fns called by a public fn" do - # content = """ - # defmodule ExFactorOtherModule do - # def other_pub1(arg1), do: arg1 - # end - # """ - # File.write("test/support/other_module.ex", content) - - # {ast, fns} = """ - # defmodule ExFactorSampleModule do - # def pub1(arg1) do - # arg1 - # |> ExFactorOtherModule.other_pub1() - # |> priv2() - # end - - # defp priv2(arg2) do - # :private - # end - # end - # """ - # |> Code.string_to_quoted() - # |> Parser.public_functions() - # |> IO.inspect(label: "") - # # assert f2.name == :pub1 - # # assert f1.name == :pub2 - # File.rm("test/support/other_module.ex") - # end + test "ast references private fns and external fns called by a public fn" do + content = """ + defmodule ExFactorOtherModule do + def other_pub1(arg1), do: arg1 + end + """ + File.write("test/support/other_module.ex", content) + + {ast, fns} = """ + defmodule ExFactorSampleModule do + def pub1(arg1) do + inter = ExFactorOtherModule.other_pub1(arg1) + priv2(inter) + end + + defp priv2(arg2) do + Map.get(args2, :key) + end + end + """ + |> Code.string_to_quoted() + |> Parser.public_functions() + |> IO.inspect(label: "") + + m = List.first(fns) + Macro.decompose_call(m.ast) + |> IO.inspect(label: "decompose_call") + # assert f2.name == :pub1 + # assert f1.name == :pub2 + File.rm("test/support/other_module.ex") + end end describe "private_functions/1" do diff --git a/test/ex_factor_test.exs b/test/ex_factor_test.exs index 11a281f..fc7d3c1 100644 --- a/test/ex_factor_test.exs +++ b/test/ex_factor_test.exs @@ -1,13 +1,13 @@ defmodule ExFactorTest do - use ExUnit.Case + # use ExUnit.Case - describe "callers/1" do - test "it should report callers of a module" do - [one, _three, two] = ExFactor.callers(ExFactor.Parser) + # describe "callers/1" do + # test "it should report callers of a module" do + # [one, _three, two] = ExFactor.callers(ExFactor.Parser) - assert one.dependency_type == "(runtime)" - assert one.filepath == "lib/ex_factor.ex" - assert two.filepath == "test/support/support.ex" - end - end + # assert one.dependency_type == "(runtime)" + # assert one.filepath == "lib/ex_factor.ex" + # assert two.filepath == "test/support/support.ex" + # end + # end end diff --git a/test/support/support.ex b/test/support/support.ex index f95d14a..be14393 100644 --- a/test/support/support.ex +++ b/test/support/support.ex @@ -5,7 +5,8 @@ defmodule ExFactor.Support do # use alias as: to verify the caller is found. alias ExFactor.Parser, as: P + alias ExFactor.Callers - def callers(mod), do: ExFactor.callers(mod) + def callers(mod), do: Callers.callers(mod) def all_functions(input), do: P.all_functions(input) end