diff --git a/CHANGELOG.md b/CHANGELOG.md index d6acd66..351a1af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ they can and will change without that change being reflected in Styler's semanti ### Improvements +#### Ex1.17+ + +Replace `:timer.units(x)` with the new `to_timeout(unit: x)` for `hours|minutes|seconds` + +#### Alias Lifting + This release taught Styler to try just that little bit harder when doing alias lifting. - general improvements around conflict detection, lifting in more correct places and fewer incorrect places (#193, h/t @jsw800) diff --git a/lib/style/deprecations.ex b/lib/style/deprecations.ex index e536cbf..ff42277 100644 --- a/lib/style/deprecations.ex +++ b/lib/style/deprecations.ex @@ -58,6 +58,13 @@ defmodule Styler.Style.Deprecations do do: {:|>, m, [lhs, {f, fm, [lob, opts]}]} end + if Version.match?(System.version(), ">= 1.17.0-dev") do + for {erl, ex} <- [hours: :hour, minutes: :minute, seconds: :second] do + defp style({{:., _, [{:__block__, _, [:timer]}, unquote(erl)]}, fm, [x]}), + do: {:to_timeout, fm, [[{{:__block__, [format: :keyword, line: fm[:line]], [unquote(ex)]}, x}]]} + end + end + # For ranges where `start > stop`, you need to explicitly include the step # Enum.slice(enumerable, 1..-2) => Enum.slice(enumerable, 1..-2//1) # String.slice("elixir", 2..-1) => String.slice("elixir", 2..-1//1) @@ -98,7 +105,7 @@ 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({:.., 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 @@ -117,7 +124,7 @@ defmodule Styler.Style.Deprecations do {:ok, stop} <- extract_value_from_range(last), true <- start > stop do step = {:__block__, [token: "1", line: lm[:line]], [1]} - {:..//, rm, [first, last, step]} + {:"..//", rm, [first, last, step]} else _ -> range end diff --git a/test/style/deprecations_test.exs b/test/style/deprecations_test.exs index cb9396f..8b5211b 100644 --- a/test/style/deprecations_test.exs +++ b/test/style/deprecations_test.exs @@ -67,22 +67,6 @@ defmodule Styler.Style.DeprecationsTest do assert_style "foo |> List.zip", "Enum.zip(foo)" end - describe "1.16 deprecations" do - @describetag skip: Version.match?(System.version(), "< 1.16.0-dev") - - test "File.stream!(path, modes, line_or_bytes) to File.stream!(path, line_or_bytes, modes)" do - assert_style( - "File.stream!(path, [encoding: :utf8, trim_bom: true], :line)", - "File.stream!(path, :line, encoding: :utf8, trim_bom: true)" - ) - - assert_style( - "f |> File.stream!([encoding: :utf8, trim_bom: true], :line) |> Enum.take(2)", - "f |> File.stream!(:line, encoding: :utf8, trim_bom: true) |> Enum.take(2)" - ) - end - end - test "~R is deprecated in favor of ~r" do assert_style(~s|Regex.match?(~R/foo/, "foo")|, ~s|Regex.match?(~r/foo/, "foo")|) end @@ -131,4 +115,34 @@ defmodule Styler.Style.DeprecationsTest do assert_style("foo |> bar() |> #{mod}.slice(x..y)") end end + + describe "1.16+" do + @describetag skip: Version.match?(System.version(), "< 1.16.0-dev") + + test "File.stream!(path, modes, line_or_bytes) to File.stream!(path, line_or_bytes, modes)" do + assert_style( + "File.stream!(path, [encoding: :utf8, trim_bom: true], :line)", + "File.stream!(path, :line, encoding: :utf8, trim_bom: true)" + ) + + assert_style( + "f |> File.stream!([encoding: :utf8, trim_bom: true], :line) |> Enum.take(2)", + "f |> File.stream!(:line, encoding: :utf8, trim_bom: true) |> Enum.take(2)" + ) + end + end + + describe "1.17+" do + @describetag skip: Version.match?(System.version(), "< 1.17.0-dev") + + test "to_timeout/1 vs :timer.units(x)" do + assert_style ":timer.hours(x)", "to_timeout(hour: x)" + assert_style ":timer.minutes(x)", "to_timeout(minute: x)" + assert_style ":timer.seconds(x)", "to_timeout(second: x)" + + assert_style "a |> x() |> :timer.hours()" + assert_style "a |> x() |> :timer.minutes()" + assert_style "a |> x() |> :timer.seconds()" + end + end end