Skip to content

Commit

Permalink
Handle edge cases and compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Sep 20, 2021
1 parent 5501458 commit c33f268
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
1 change: 1 addition & 0 deletions lib/ex_factor/callers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule ExFactor.Callers do
|> mangle_list()
end

defp mangle_list([""]), do: []
defp mangle_list(["Compiling" <> _ | tail]), do: mangle_list(tail)

defp mangle_list(list) do
Expand Down
9 changes: 8 additions & 1 deletion lib/ex_factor/changer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ defmodule ExFactor.Changer do
def change(opts) do
source_module = Keyword.fetch!(opts, :source_module)
Mix.Tasks.Compile.Elixir.run([])
callers = Callers.callers(source_module)

source_module
|> Callers.callers()
|> update_callers(opts)
end

defp update_callers([], _), do: []

defp update_callers(callers, opts) do
Enum.map(callers, fn caller ->
File.read!(caller.filepath)
|> String.split("\n")
Expand Down
8 changes: 3 additions & 5 deletions lib/ex_factor/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule ExFactor.Extractor do
"""

def emplace(opts) do
# modules as strings
source_module = Keyword.fetch!(opts, :source_module)
target_module = Keyword.fetch!(opts, :target_module)
source_function = Keyword.fetch!(opts, :source_function)
Expand All @@ -36,16 +37,12 @@ defmodule ExFactor.Extractor do
|> Neighbors.walk(source_function, arity)
|> Enum.map(&Macro.to_string(&1))

# |> IO.inspect(label: "to string")

string_fns = Enum.join(to_extract, "\n")

case File.exists?(target_path) do
true ->
{ast, list} = Parser.read_file(target_path)
{:defmodule, [do: [line: _begin_line], end: [line: end_line], line: _], _} = ast
# string_fns = Macro.to_string(to_extract)
# string_fns = Enum.join(to_extract, "\n")

list
|> List.insert_at(end_line - 1, refactor_message())
Expand All @@ -54,8 +51,9 @@ defmodule ExFactor.Extractor do
|> then(fn contents -> write_file(target_path, contents, target_module, dry_run) end)

_ ->
target_mod = Module.concat [target_module]
quote generated: true do
defmodule unquote(target_module) do
defmodule unquote(target_mod) do
@moduledoc false
unquote(Macro.unescape_string(string_fns))
end
Expand Down
4 changes: 4 additions & 0 deletions test/ex_factor/callers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ defmodule ExFactor.CallersTest do
assert one.filepath == "lib/ex_factor/callers.ex"
assert five.filepath == "test/support/support.ex"
end

test "when no callers" do
assert [] = Callers.callers(ExFactor.NotAModule)
end
end
end
20 changes: 10 additions & 10 deletions test/ex_factor/changer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ defmodule ExFactor.ChangerTest do
File.write("lib/ex_factor/tmp/caller_module.ex", content)

opts = [
target_module: ExFactor.Tmp.TargetModule,
source_module: ExFactor.Tmp.SourceMod,
target_module: "ExFactor.Tmp.TargetModule",
source_module: "ExFactor.Tmp.SourceMod",
source_function: :refactor1,
arity: 1
]
Expand Down Expand Up @@ -82,8 +82,8 @@ defmodule ExFactor.ChangerTest do
File.write("lib/ex_factor/tmp/caller_module.ex", content)

opts = [
target_module: ExFactor.Tmp.TargetModule,
source_module: ExFactor.Tmp.SourceMod,
target_module: "ExFactor.Tmp.TargetModule",
source_module: "ExFactor.Tmp.SourceMod",
source_function: :refactor1,
arity: 1
]
Expand Down Expand Up @@ -132,8 +132,8 @@ defmodule ExFactor.ChangerTest do
File.write("lib/ex_factor/tmp/caller_module.ex", content)

opts = [
target_module: ExFactor.Tmp.TargetModule,
source_module: ExFactor.Tmp.SourceMod,
target_module: "ExFactor.Tmp.TargetModule",
source_module: "ExFactor.Tmp.SourceMod",
source_function: :refactor1,
arity: 1
]
Expand Down Expand Up @@ -175,8 +175,8 @@ defmodule ExFactor.ChangerTest do
File.write("lib/ex_factor/tmp/caller_module.ex", content)

opts = [
target_module: ExFactor.Tmp.TargetModule,
source_module: ExFactor.Tmp.SourceMod,
target_module: "ExFactor.Tmp.TargetModule",
source_module: "ExFactor.Tmp.SourceMod",
source_function: :refactor1,
arity: 1
]
Expand Down Expand Up @@ -217,8 +217,8 @@ defmodule ExFactor.ChangerTest do
File.write("lib/ex_factor/tmp/caller_module.ex", content)

opts = [
target_module: ExFactor.Tmp.TargetModule,
source_module: ExFactor.Tmp.SourceMod,
target_module: "ExFactor.Tmp.TargetModule",
source_module: "ExFactor.Tmp.SourceMod",
source_function: :refactor1,
dry_run: true,
arity: 1
Expand Down
40 changes: 19 additions & 21 deletions test/ex_factor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,35 @@ defmodule ExFactorTest do

content = """
defmodule ExFactor.Tmp.SourceModule do
@somedoc "This is somedoc"
@doc "this is some documentation for refactor1/1"
def refactor1(arg1) do
:ok
end
def refactor1([]) do
:empty
end
def refactor1(arg1) do
arg1
end
end
"""

File.write("lib/ex_factor/tmp/source_module.ex", content)

content = """
defmodule ExFactor.Tmp.TargetModule do
@somedoc "This is somedoc TargetModule"
@doc "some docs"
def pub_exists(arg_exists) do
:ok
end
def pub_exists(:error) do
:error
end
def pub_exists(arg_exists) do
arg_exists
end
end
"""

File.write("lib/ex_factor/tmp/target_module.ex", content)

opts = [
target_module: ExFactor.Tmp.TargetModule,
source_module: ExFactor.Tmp.SourceModule,
target_module: "ExFactor.Tmp.TargetModule",
source_module: "ExFactor.Tmp.SourceModule",
source_function: :refactor1,
arity: 1
]
Expand All @@ -72,43 +70,43 @@ defmodule ExFactorTest do

content = """
defmodule ExFactor.Tmp.SourceModule do
@somedoc "This is somedoc"
@doc "this is some documentation for refactor1/1"
def refactor1(arg1) do
:ok
end
def refactor1([]) do
:empty
end
def refactor1(arg1) do
arg1
end
end
"""

File.write("lib/ex_factor/tmp/source_module.ex", content)

content = """
defmodule ExFactor.Tmp.TargetModule do
@somedoc "This is somedoc TargetModule"
@someattr "This is somedoc TargetModule"
@doc "some docs"
def pub_exists(arg_exists) do
:ok
end
def pub_exists(:error) do
:error
end
def pub_exists(arg_exists) do
_ = @someattr
arg_exists
end
end
"""

File.write("lib/ex_factor/tmp/target_module.ex", content)

opts = [
target_module: ExFactor.Tmp.TargetModule,
source_module: ExFactor.Tmp.SourceModule,
target_module: "ExFactor.Tmp.TargetModule",
source_module: "ExFactor.Tmp.SourceModule",
source_function: :refactor1,
arity: 1,
dry_run: true
]

{extract_contents, remove_contents} = ExFactor.refactor(opts)
{_extract_contents, _remove_contents} = ExFactor.refactor(opts)

# assert that the original files are unchanged
file = File.read!("lib/ex_factor/tmp/target_module.ex")
Expand Down

0 comments on commit c33f268

Please sign in to comment.