Skip to content

Commit

Permalink
Handle some edge cases.
Browse files Browse the repository at this point in the history
Fix all code so tests pass.
Remove unused code.
  • Loading branch information
ckoch-cars committed Jul 15, 2022
1 parent bafa887 commit 8c7e712
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 49 deletions.
2 changes: 2 additions & 0 deletions lib/ex_factor.ex
Expand Up @@ -63,6 +63,8 @@ defmodule ExFactor do
end)
end

defp format(%{state: [:unchanged]} = struct), do: struct

defp format(struct) do
Formatter.format([struct.path])
Map.get_and_update(struct, :state, fn val -> {val, [:formatted | val]} end)
Expand Down
7 changes: 3 additions & 4 deletions lib/ex_factor/changer.ex
Expand Up @@ -4,7 +4,6 @@ defmodule ExFactor.Changer do
"""

alias ExFactor.Callers
alias ExFactor.Util

@doc """
Given all the Callers to a module, find the instances of the target function and refactor the
Expand Down Expand Up @@ -56,7 +55,7 @@ defmodule ExFactor.Changer do
target_module = Keyword.fetch!(opts, :target_module)

# modified values
source_string = Util.module_to_string(source_module)
source_string = to_string(source_module)
source_modules = String.split(source_module, ".")
source_alias = Enum.at(source_modules, -1)
target_alias = preferred_alias(file_list, target_module)
Expand Down Expand Up @@ -140,7 +139,7 @@ defmodule ExFactor.Changer do

defp maybe_add_alias({state, contents_list}, opts) do
target_module = Keyword.fetch!(opts, :target_module)
target_string = Util.module_to_string(target_module)
target_string = to_string(target_module)

{state, contents_list}
|> change_alias(target_string)
Expand Down Expand Up @@ -193,7 +192,7 @@ defmodule ExFactor.Changer do
defp maybe_add_import({state, contents_list}, opts) do
source_module = Keyword.fetch!(opts, :source_module)
target_module = Keyword.fetch!(opts, :target_module)
target_string = Util.module_to_string(target_module)
target_string = to_string(target_module)
source_modules = String.split(source_module, ".")
source_alias = Enum.at(source_modules, -1)
source_alias_alt = find_alias_as(contents_list, source_module)
Expand Down
24 changes: 15 additions & 9 deletions lib/ex_factor/extractor.ex
Expand Up @@ -45,23 +45,21 @@ defmodule ExFactor.Extractor do
{ast, list} = Parser.read_file(target_path)
{:defmodule, [do: [line: _begin_line], end: [line: end_line], line: _], _} = ast

list
|> List.insert_at(end_line - 1, refactor_message())
|> List.insert_at(end_line, string_fns)
|> Enum.join("\n")
|> then(fn contents -> write_file(target_path, contents, target_module, dry_run) end)
insert_code(list, end_line, string_fns, target_path, target_module, dry_run)

_ ->
target_mod = Module.concat([target_module])

quote generated: true do
module_contents = quote generated: true do
defmodule unquote(target_mod) do
@moduledoc false
unquote(Macro.unescape_string(string_fns))
@moduledoc "This module created with ExFactor"
end
end
|> Macro.to_string()
|> then(fn contents -> write_file(target_path, contents, target_module, dry_run) end)
list = String.split(module_contents, "\n")
{:ok, ast} = Code.string_to_quoted(module_contents, token_metadata: true)
{:defmodule, [do: [line: _begin_line], end: [line: end_line], closing: _, line: _], _} = ast
insert_code(list, end_line, string_fns, target_path, target_module, dry_run)
end
end

Expand Down Expand Up @@ -90,4 +88,12 @@ defmodule ExFactor.Extractor do
file_contents: contents
}
end

defp insert_code(list, end_line, string_fns, target_path, target_module, dry_run) do
list
|> List.insert_at(end_line - 1, refactor_message())
|> List.insert_at(end_line, string_fns)
|> Enum.join("\n")
|> then(fn contents -> write_file(target_path, contents, target_module, dry_run) end)
end
end
2 changes: 2 additions & 0 deletions lib/ex_factor/formatter.ex
Expand Up @@ -4,6 +4,8 @@ defmodule ExFactor.Formatter do
Format a list of files
"""

def format([nil]), do: nil

def format(args) do
Mix.Tasks.Format.run(args)
end
Expand Down
12 changes: 6 additions & 6 deletions lib/ex_factor/neighbors.ex
Expand Up @@ -8,17 +8,17 @@ 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)
def walk(block, func_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)
def walk(block, func_name, arity) when is_binary(func_name) do
# func_name_atom = String.to_atom(func_name)
walk(block, String.to_atom(func_name), arity)
end

def walk(block, fn_name, arity) do
def walk(block, func_name, arity) do
block
|> Enum.reduce({[], []}, fn el, acc ->
eval_elem(el, acc, fn_name, arity)
eval_elem(el, acc, func_name, arity)
end)
|> elem(1)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/ex_factor/parser.ex
Expand Up @@ -145,6 +145,10 @@ defmodule ExFactor.Parser do
block_contents
end

defp ast_block([do: block_contents], _acc) do
[block_contents]
end

defp ast_block(_block, acc) do
acc
end
Expand Down
11 changes: 0 additions & 11 deletions lib/ex_factor/util.ex

This file was deleted.

19 changes: 0 additions & 19 deletions test/ex_factor/util_test.exs

This file was deleted.

0 comments on commit 8c7e712

Please sign in to comment.