From 35f3d3bdc9b820904027f052c55295b8cdf6e2d6 Mon Sep 17 00:00:00 2001 From: Christian Koch Date: Mon, 20 Sep 2021 15:09:13 -0500 Subject: [PATCH] alias better --- lib/ex_factor/changer.ex | 31 ++++++++++++++++++++++--------- test/ex_factor/changer_test.exs | 3 +++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/ex_factor/changer.ex b/lib/ex_factor/changer.ex index 8a14b9c..8a58ee5 100644 --- a/lib/ex_factor/changer.ex +++ b/lib/ex_factor/changer.ex @@ -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]} @@ -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 diff --git a/test/ex_factor/changer_test.exs b/test/ex_factor/changer_test.exs index 68ac9b6..c8bc794 100644 --- a/test/ex_factor/changer_test.exs +++ b/test/ex_factor/changer_test.exs @@ -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 = """