Skip to content

Commit

Permalink
rewrite ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Nov 19, 2024
1 parent 60f79c7 commit 3ae7f5f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/style/deprecations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ defmodule Styler.Style.Deprecations do

def run({node, meta}, ctx), do: {:cont, {style(node), meta}, ctx}

# rewrite patterns of `first..last = ...` to `first..last//_ = ...`
defp style({:=, m, [{:.., _, [_first, _last]} = range, rhs]}), do: {:=, m, [rewrite_range_match(range), rhs]}
defp style({:->, m, [[{:.., _, [_first, _last]} = range], rhs]}), do: {:->, m, [[rewrite_range_match(range)], rhs]}
defp style({:<-, m, [{:.., _, [_first, _last]} = range, rhs]}), do: {:<-, m, [rewrite_range_match(range), rhs]}

defp style({def, dm, [{x, xm, params} | rest]}) when def in ~w(def defp)a and is_list(params),
do: {def, dm, [{x, xm, Enum.map(params, &rewrite_range_match/1)} | rest]}

# Logger.warn => Logger.warning
# Started to emit warning after Elixir 1.15.0
defp style({{:., dm, [{:__aliases__, am, [:Logger]}, :warn]}, funm, args}),
Expand Down Expand Up @@ -84,6 +92,9 @@ defmodule Styler.Style.Deprecations do

defp style(node), do: node

defp rewrite_range_match({:.., dm, [first, {_, m, _} = last]}), do: {:"..//", dm, [first, last, {:_, m, nil}]}
defp rewrite_range_match(x), do: x

defp add_step_to_date_range?(first, last) do
with {:ok, f} <- extract_date_value(first),
{:ok, l} <- extract_date_value(last),
Expand Down
28 changes: 28 additions & 0 deletions test/style/deprecations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ defmodule Styler.Style.DeprecationsTest do
)
end

test "matching ranges" do
assert_style "first..last = range", "first..last//_ = range"
assert_style "^first..^last = range", "^first..^last//_ = range"
assert_style "first..last = x = y", "first..last//_ = x = y"
assert_style "y = first..last = x", "y = first..last//_ = x"

assert_style "def foo(x..y), do: :ok", "def foo(x..y//_), do: :ok"
assert_style "def foo(a, x..y = z), do: :ok", "def foo(a, x..y//_ = z), do: :ok"
assert_style "def foo(%{a: x..y = z}), do: :ok", "def foo(%{a: x..y//_ = z}), do: :ok"

assert_style "with a..b = c <- :ok, d..e <- :better, do: :ok", "with a..b//_ = c <- :ok, d..e//_ <- :better, do: :ok"

assert_style(
"""
case x do
a..b = c -> :ok
d..e -> :better
end
""",
"""
case x do
a..b//_ = c -> :ok
d..e//_ -> :better
end
"""
)
end

describe "1.16 deprecations" do
@describetag skip: Version.match?(System.version(), "< 1.16.0-dev")

Expand Down

0 comments on commit 3ae7f5f

Please sign in to comment.