Skip to content

Commit

Permalink
Handle string function names
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Sep 20, 2021
1 parent 5e428ed commit b107316
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/ex_factor/neighbors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ 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) do
def walk(block, fn_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)
end

def walk(block, fn_name, arity) do
block
|> Enum.reduce({[], []}, fn el, acc ->
eval_elem(el, acc, fn_name, arity)
Expand Down
3 changes: 3 additions & 0 deletions test/ex_factor/extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ defmodule ExFactor.ExtractorTest do
end
end

test "noop when no matching fns found in source" do
end

test "write a new file with the function" do
content = """
defmodule ExFactorSampleModule do
Expand Down
25 changes: 25 additions & 0 deletions test/ex_factor/neighbors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ defmodule ExFactor.NeighborsTest do
] = Neighbors.walk(block, :pub1)
end

test "it finds the target when the last function and function is a string" do
module =
"""
defmodule ExFactorSampleModule do
def pub1(arg1) do
:ok
end
_docp = "arbitrary module-level elem"
defp priv1(arg1) do
:ok
end
def pub2(arg2) do
:ok
end
end
"""
|> Code.string_to_quoted()

{_ast, block} = Parser.block_contents(module)

assert [
{:def, _, [{:pub2, _, [{:arg2, _, nil}]}, _]}
] = Neighbors.walk(block, "pub2")
end

test "it should handle ignore the aliases" do
module =
"""
Expand Down

0 comments on commit b107316

Please sign in to comment.