Skip to content

Commit

Permalink
Add callers fn, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Sep 7, 2021
1 parent 269c05a commit 3eb4b67
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# ExFactor

**TODO: Add description**
ExFactor is a refactoring helper. Given a module, function name, and arity, it will locate all uses of
that function, change the callers to a new module and/or function name, and move the function from the original location
to a new file/module. At this time, ExFactor cannot change the function arity.


### Example
```elixir
mix exfactor -m MyApp.MyMod -f [my_func: 2] -mm MyApp.MyNewMod -mf :my_new_func
```

## Installation

Expand Down
5 changes: 5 additions & 0 deletions lib/ex_factor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ defmodule ExFactor do

def all_fns(input), do: P.all_functions(input)

def callers(mod) do
# System.cmd("mix", ["compile"], env: [{"MIX_ENV", "test"}])
System.cmd("mix", ["xref", "callers", "#{mod}"], env: [{"MIX_ENV", "test"}])
# |> IO.inspect(label: "")
# mix xref callers mod
end
end
47 changes: 39 additions & 8 deletions test/ex_factor_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule ExFactorTest do
use ExUnit.Case
alias ExFactor.Parser

test "it should report public fns" do
test "it should report public fns and their arity" do
{_ast, [f1]} = """
defmodule CredoSampleModule do
@somedoc "This is somedoc"
Expand All @@ -12,9 +13,10 @@ defmodule ExFactorTest do
end
"""
|> Code.string_to_quoted()
|> ExFactor.public_functions()
|> Parser.public_functions()

assert f1.name == :pub1
assert f1.arity == 1
end

test "it should report TWO public fns" do
Expand All @@ -36,25 +38,26 @@ defmodule ExFactorTest do
end
"""
|> Code.string_to_quoted()
|> ExFactor.public_functions()
|> Parser.public_functions()
assert f2.name == :pub1
assert f1.name == :pub2
end

test "it should report private fns" do
test "it should report private fns and arity" do
{_ast, [f1]} = """
defmodule CredoSampleModule do
@somedoc "This is somedoc"
# no aliases
defp priv1(arg1) do
defp priv1(arg1, arg2) do
:ok
end
end
"""
|> Code.string_to_quoted()
|> ExFactor.private_functions()
|> Parser.private_functions()

assert f1.name == :priv1
assert f1.arity == 2
assert f1.defn == :defp
end

test "it should report TWO private fns" do
Expand All @@ -80,8 +83,36 @@ defmodule ExFactorTest do
end
"""
|> Code.string_to_quoted()
|> ExFactor.private_functions()
|> Parser.private_functions()
assert f2.name == :priv3
assert f1.name == :priv4
end

test "it should report all fns" do
{_ast, [f1, f2, f3]} = """
defmodule CredoSampleModule do
@somedoc "This is somedoc"
# no aliases
def pub1(arg1) do
:ok
end
def pub2(arg2) do
:yes
end
defp priv3(arg3_1, arg3_2, arg3_3) do
:private
end
end
"""
|> Code.string_to_quoted()
|> Parser.all_functions()
assert f2.name == :pub1
assert f1.name == :pub2
assert f3.name == :priv3
assert f3.defn == :defp
assert f3.arity == 3
end

end
11 changes: 11 additions & 0 deletions test/support/support.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule ExFactor.Support do
@moduledoc """
Support moduel for `ExFactor` testing.
"""

alias ExFactor.Parser

def callers(mod), do: ExFactor.callers(mod)
def all_functions(input), do: Parser.all_functions(input)
end

0 comments on commit 3eb4b67

Please sign in to comment.