From 3ae7f5fbd3f5b60960243ecfe216f232ccec61aa Mon Sep 17 00:00:00 2001 From: Matt Enlow Date: Mon, 18 Nov 2024 22:10:08 -0800 Subject: [PATCH 1/2] rewrite ranges --- lib/style/deprecations.ex | 11 +++++++++++ test/style/deprecations_test.exs | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/style/deprecations.ex b/lib/style/deprecations.ex index 68050083..7b948d7b 100644 --- a/lib/style/deprecations.ex +++ b/lib/style/deprecations.ex @@ -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}), @@ -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), diff --git a/test/style/deprecations_test.exs b/test/style/deprecations_test.exs index 3d5e8ed9..7d93f452 100644 --- a/test/style/deprecations_test.exs +++ b/test/style/deprecations_test.exs @@ -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") From 981b01e59b923f599ccf7ae46035d1ca937c3a57 Mon Sep 17 00:00:00 2001 From: Matt Enlow Date: Mon, 18 Nov 2024 22:40:48 -0800 Subject: [PATCH 2/2] List.zip for good measure --- lib/style/deprecations.ex | 6 ++++++ test/style/deprecations_test.exs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/lib/style/deprecations.ex b/lib/style/deprecations.ex index 7b948d7b..f810e5a3 100644 --- a/lib/style/deprecations.ex +++ b/lib/style/deprecations.ex @@ -17,6 +17,7 @@ defmodule Styler.Style.Deprecations do def run({node, meta}, ctx), do: {:cont, {style(node), meta}, ctx} + # Deprecated in 1.18 # 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]} @@ -25,6 +26,11 @@ defmodule Styler.Style.Deprecations do 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]} + # Deprecated in 1.18 + # List.zip => Enum.zip + defp style({{:., dm_, [{:__aliases__, am, [:List]}, :zip]}, fm, arg}), + do: {{:., dm_, [{:__aliases__, am, [:Enum]}, :zip]}, fm, arg} + # Logger.warn => Logger.warning # Started to emit warning after Elixir 1.15.0 defp style({{:., dm, [{:__aliases__, am, [:Logger]}, :warn]}, funm, args}), diff --git a/test/style/deprecations_test.exs b/test/style/deprecations_test.exs index 7d93f452..cb9396fd 100644 --- a/test/style/deprecations_test.exs +++ b/test/style/deprecations_test.exs @@ -61,6 +61,12 @@ defmodule Styler.Style.DeprecationsTest do ) end + test "List.zip/1" do + assert_style "List.zip(foo)", "Enum.zip(foo)" + assert_style "foo |> List.zip |> bar", "foo |> Enum.zip() |> bar()" + assert_style "foo |> List.zip", "Enum.zip(foo)" + end + describe "1.16 deprecations" do @describetag skip: Version.match?(System.version(), "< 1.16.0-dev")