Skip to content

Commit

Permalink
alias better
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Sep 20, 2021
1 parent b107316 commit 35f3d3b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/ex_factor/changer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ defmodule ExFactor.Changer do

Enum.reduce(list, {:unchanged, []}, fn elem, {state, acc} ->
cond do
# match full module name
String.match?(elem, ~r/#{source_string}\.#{source_function}/) ->
elem = String.replace(elem, source_module, target_module)
elem = String.replace(elem, source_module, target_alias)
{:changed, [elem | acc]}

# match aliased module name
String.match?(elem, ~r/#{source_alias}\.#{source_function}/) ->
elem = String.replace(elem, source_alias, target_alias)
{:changed, [elem | acc]}

# match module name aliased :as
String.match?(elem, ~r/#{source_alias_alt}\.#{source_function}/) ->
elem = String.replace(elem, source_alias_alt, target_alias)
{:changed, [elem | acc]}
Expand Down Expand Up @@ -135,24 +138,34 @@ defmodule ExFactor.Changer do
target_string = Util.module_to_string(target_module)

if Enum.find(contents_list, fn el -> match_alias?(el, target_string) end) do
# IO.puts "#{target_string} FOUND THE ALIAS"
{state, contents_list}
else
contents_list
|> Enum.reduce([], fn elem, acc ->
if match_alias?(elem, source_string) do
# if String.match?(elem, ~r/alias #{source_string}(\s|$|\,)/) do
new_alias = String.replace(elem, source_string, target_string)
[new_alias | [elem | acc]]
else
[elem | acc]
|> Enum.reduce({:none, []}, fn elem, {prev, acc} ->
cond do
match_alias?(elem, source_string) ->
new_alias = String.replace(elem, source_string, target_string)
{:alias, [new_alias | [elem | acc]]}
prev == :alias and not match_alias?(elem, "") ->
{:none, [ "alias #{target_string}" | [elem | acc]]}
match_alias?(elem, "") ->
{:alias, [elem | acc]}
true ->
{:none, [elem | acc]}
end
end)
|> elem(1)
|> Enum.reverse()
|> then(fn list -> {state, list} end)
end
end

defp match_alias?(string, "") do
String.match?(string, ~r/(^|\s)alias\s/)
end

defp match_alias?(string, module_string) do
String.match?(string, ~r/alias #{module_string}(\s|$|\,)/)
String.match?(string, ~r/(^|\s)alias #{module_string}(\s|$|\,)/)
end
end
3 changes: 3 additions & 0 deletions test/ex_factor/changer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ defmodule ExFactor.ChangerTest do
assert caller =~ "alias ExFactor.Tmp.SourceMod.Other"
refute caller =~ "alias ExFactor.Tmp.TargetModule.Other"
assert caller =~ "TargetModule.refactor1(arg_a)"
# asser the function uses the alias
refute caller =~ "ExFactor.Tmp.TargetModule.refactor1(arg_a)"
end

# update the annoying alias style: alias Foo.{Bar, Baz, Biz}
# find and update when the module is used but not aliased

test "only add alias entry if it's missing" do
content = """
Expand Down

0 comments on commit 35f3d3b

Please sign in to comment.