From 1d2ef10493e369cf20d9fb0c39630e845181e9d8 Mon Sep 17 00:00:00 2001 From: gp-pereira Date: Sun, 24 Nov 2024 20:05:59 -0300 Subject: [PATCH] refactor: ignore alias and aliases on some refactors --- .../refactor/constant/extract_constant.ex | 2 +- .../refactor/function/extract_function.ex | 6 +++-- .../refactor/pipeline/introduce_io_inspect.ex | 4 ++- .../refactor/variable/extract_variable.ex | 4 +++ .../constant/extract_constant_test.exs | 13 ++++++++++ .../function/extract_function_test.exs | 13 ++++++++++ .../pipeline/introduce_io_inspect_test.exs | 13 ++++++++++ .../variable/extract_variable_test.exs | 26 +++++++++++++++++++ 8 files changed, 77 insertions(+), 4 deletions(-) diff --git a/lib/refactorex/refactor/constant/extract_constant.ex b/lib/refactorex/refactor/constant/extract_constant.ex index 167e006..60ad897 100644 --- a/lib/refactorex/refactor/constant/extract_constant.ex +++ b/lib/refactorex/refactor/constant/extract_constant.ex @@ -12,7 +12,7 @@ defmodule Refactorex.Refactor.Constant.ExtractConstant do @constant_name "extracted_constant" def can_refactor?(%{node: {id, _, _}}, _) - when id in ~w(@ &)a, + when id in ~w(@ & alias __aliases__)a, do: false def can_refactor?(%{node: node} = zipper, selection) do diff --git a/lib/refactorex/refactor/function/extract_function.ex b/lib/refactorex/refactor/function/extract_function.ex index 1187a61..3f85931 100644 --- a/lib/refactorex/refactor/function/extract_function.ex +++ b/lib/refactorex/refactor/function/extract_function.ex @@ -1,6 +1,6 @@ defmodule Refactorex.Refactor.Function.ExtractFunction do use Refactorex.Refactor, - title: "Extract private function", + title: "Extract function", kind: "refactor.extract", works_on: :selection @@ -13,7 +13,9 @@ defmodule Refactorex.Refactor.Function.ExtractFunction do @function_name "extracted_function" - def can_refactor?(_, {id, _, _}) when id in ~w(@ & <-)a, do: :skip + def can_refactor?(%{node: {id, _, _}}, _) + when id in ~w(@ & <- alias __aliases__)a, + do: :skip def can_refactor?(%{node: node} = zipper, selection) do cond do diff --git a/lib/refactorex/refactor/pipeline/introduce_io_inspect.ex b/lib/refactorex/refactor/pipeline/introduce_io_inspect.ex index 6e71c15..c758ed6 100644 --- a/lib/refactorex/refactor/pipeline/introduce_io_inspect.ex +++ b/lib/refactorex/refactor/pipeline/introduce_io_inspect.ex @@ -6,7 +6,9 @@ defmodule Refactorex.Refactor.Pipeline.IntroduceIOInspect do alias Refactorex.Refactor.Variable - def can_refactor?(_, {id, _, _}) when id in ~w(<- &)a, do: :skip + def can_refactor?(_, {id, _, _}) + when id in ~w(<- & alias __aliases__)a, + do: :skip def can_refactor?(%{node: node} = zipper, selection) do cond do diff --git a/lib/refactorex/refactor/variable/extract_variable.ex b/lib/refactorex/refactor/variable/extract_variable.ex index ba9515b..4a7bdcd 100644 --- a/lib/refactorex/refactor/variable/extract_variable.ex +++ b/lib/refactorex/refactor/variable/extract_variable.ex @@ -12,6 +12,10 @@ defmodule Refactorex.Refactor.Variable.ExtractVariable do @variable_name "extracted_variable" + def can_refactor?(%{node: {id, _, _}}, _) + when id in ~w(alias __aliases__)a, + do: false + def can_refactor?(%{node: node} = zipper, selection) do cond do not AST.equal?(node, selection) -> diff --git a/test/refactor/constant/extract_constant_test.exs b/test/refactor/constant/extract_constant_test.exs index ad80c21..1611ff2 100644 --- a/test/refactor/constant/extract_constant_test.exs +++ b/test/refactor/constant/extract_constant_test.exs @@ -215,4 +215,17 @@ defmodule Refactorex.Refactor.Constant.ExtractConstantTest do """ ) end + + test "ignores alias" do + assert_not_refactored( + ExtractConstant, + """ + defmodule Foo do + # v + alias Foo.Bar + # ^ + end + """ + ) + end end diff --git a/test/refactor/function/extract_function_test.exs b/test/refactor/function/extract_function_test.exs index 203ebc9..5c1d1d5 100644 --- a/test/refactor/function/extract_function_test.exs +++ b/test/refactor/function/extract_function_test.exs @@ -260,4 +260,17 @@ defmodule Refactorex.Refactor.Function.ExtractFunctionTest do """ ) end + + test "ignores alias" do + assert_not_refactored( + ExtractFunction, + """ + defmodule Foo do + # v + alias Foo.Bar + # ^ + end + """ + ) + end end diff --git a/test/refactor/pipeline/introduce_io_inspect_test.exs b/test/refactor/pipeline/introduce_io_inspect_test.exs index 297d6f1..a5fa731 100644 --- a/test/refactor/pipeline/introduce_io_inspect_test.exs +++ b/test/refactor/pipeline/introduce_io_inspect_test.exs @@ -58,4 +58,17 @@ defmodule Refactorex.Refactor.Pipeline.IntroduceIOInspectTest do """ ) end + + test "ignores alias" do + assert_not_refactored( + IntroduceIOInspect, + """ + defmodule Foo do + # v + alias Foo.Bar + # ^ + end + """ + ) + end end diff --git a/test/refactor/variable/extract_variable_test.exs b/test/refactor/variable/extract_variable_test.exs index 8493246..48a9fa0 100644 --- a/test/refactor/variable/extract_variable_test.exs +++ b/test/refactor/variable/extract_variable_test.exs @@ -298,4 +298,30 @@ defmodule Refactorex.Refactor.Variable.ExtractVariableTest do """ ) end + + test "ignores alias" do + assert_not_refactored( + ExtractVariable, + """ + defmodule Foo do + # v + alias Foo.Bar + # ^ + end + """ + ) + + assert_not_refactored( + ExtractVariable, + """ + defmodule Foo do + def foo() do + # v + Foo.Bar + # ^ + end + end + """ + ) + end end