Skip to content

Commit

Permalink
refactor: ignore alias and aliases on some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
gp-pereira committed Nov 24, 2024
1 parent e0d0c80 commit 1d2ef10
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/refactorex/refactor/constant/extract_constant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/refactorex/refactor/function/extract_function.ex
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/refactorex/refactor/pipeline/introduce_io_inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/refactorex/refactor/variable/extract_variable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down
13 changes: 13 additions & 0 deletions test/refactor/constant/extract_constant_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions test/refactor/function/extract_function_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions test/refactor/pipeline/introduce_io_inspect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions test/refactor/variable/extract_variable_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1d2ef10

Please sign in to comment.