Skip to content

Commit

Permalink
Fix a few tests, and continue to refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ckochx committed Sep 16, 2021
1 parent b319971 commit 50c381c
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 1 addition & 7 deletions lib/ex_factor/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 2 additions & 35 deletions lib/ex_factor/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand All @@ -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 """
Expand All @@ -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.
"""
Expand All @@ -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

Expand Down Expand Up @@ -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], []}
]
}
]
6 changes: 0 additions & 6 deletions lib/ex_factor/remover.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
4 changes: 2 additions & 2 deletions test/ex_factor/callers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 4 additions & 17 deletions test/ex_factor/remover_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
28 changes: 0 additions & 28 deletions test/tmp/source_module.ex

This file was deleted.

0 comments on commit 50c381c

Please sign in to comment.